跳转到主要内容

OCRTool

描述

从图像中提取文本(本地路径或 URL)。通过 CrewAI 的 LLM 接口使用具有视觉功能的 LLM。

安装

除了 crewai-tools 之外无需额外安装。请确保您选择的 LLM 支持视觉功能。

参数

运行参数

  • image_path_url (str, 必填): 本地图像路径或 HTTP(S) URL。

示例

直接使用

代码
from crewai_tools import OCRTool

print(OCRTool().run(image_path_url="/tmp/receipt.png"))

与代理结合使用

代码
from crewai import Agent, Task, Crew
from crewai_tools import OCRTool

ocr = OCRTool()

agent = Agent(
    role="OCR", 
    goal="Extract text", 
    tools=[ocr],
)

task = Task(
    description="Extract text from https://example.com/invoice.jpg", 
    expected_output="All detected text in plain text",
    agent=agent,
)

crew = Crew(agents=[agent], tasks=[task])
result = crew.kickoff()

注意

  • 确保所选的 LLM 支持图像输入。
  • 对于大型图像,请考虑缩小尺寸以减少令牌使用。
  • 如果需要,您可以将特定的 LLM 实例传递给工具(例如,LLM(model="gpt-4o")),与 README 指南一致。

示例

代码
from crewai import Agent, Task, Crew
from crewai_tools import OCRTool

tool = OCRTool()

agent = Agent(
    role="OCR Specialist",
    goal="Extract text from images",
    backstory="Vision‑enabled analyst",
    tools=[tool],
    verbose=True,
)

task = Task(
    description="Extract text from https://example.com/receipt.png",
    expected_output="All detected text in plain text",
    agent=agent,
)

crew = Crew(agents=[agent], tasks=[task])
result = crew.kickoff()