跳转到主要内容

OCR 工具

描述

从图片(本地路径或 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"))

与 Agent 一起使用

代码
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 支持图片输入。
  • 对于大尺寸图片,考虑缩小尺寸以减少 Token 使用量。
  • 如果需要,您可以将特定的 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()