You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Azure OpenAI API时CrewAI PDFSearchTool抛出认证错误求助

Azure OpenAI + CrewAI PDFSearchTool 认证错误解决方法

问题概述

使用CrewAI的PDFSearchTool进行PDF RAG检索时,触发litellm.exceptions.AuthenticationError,错误提示需设置api_key参数或OPENAI_API_KEY环境变量。当前采用Azure OpenAI的gpt-4o和text-embedding-ada-002模型,通过Poetry管理依赖,已参照官方文档配置自定义config,但认证失败。

完整错误信息:

Agent: Research Agent
Task: Answer the customer's questions based on the home inspection PDF.
The research agent will search through the PDF to find the relevant answers. Your final answer MUST be clear and accurate, based on the content of the home inspection PDF.

Here is the customer's question:Exterior

LiteLLM.Info: If you need to debug this error, use litellm.set_verbose=True'.

raise AuthenticationError(
litellm.exceptions.AuthenticationError: litellm.AuthenticationError: AuthenticationError: OpenAIException - The api_key client option must be set either by passing api_key to the client or by setting the OPENAI_API_KEY environment variable

错误原因分析

  1. 无效环境变量干扰:代码中手动设置os.environ["OPENAI_API_KEY"] = "random key",覆盖了dotenv加载的有效密钥,导致认证失败
  2. Deployment Name配置错误:配置字典中deployment_name填入了完整的Azure endpoint URL,实际应为Azure OpenAI门户中创建的模型部署名称(而非完整URL)
  3. 缺少API版本配置:Azure OpenAI需要指定正确的api_version,原配置未在embedder部分添加该参数

修复步骤

  1. 移除无效的OPENAI_API_KEY环境变量手动设置,依赖dotenv加载真实密钥
  2. 将配置中的deployment_name替换为Azure门户中对应的模型部署名称
  3. 在嵌入模型的配置中添加正确的api_version参数
  4. 确保所有api_key使用Azure OpenAI门户中获取的有效密钥

完整修复代码

from crewai import Agent, Crew, Process, Task, LLM
from crewai_tools import PDFSearchTool
from dotenv import load_dotenv
import litellm
import os

# 启用LiteLLM调试模式
litellm.set_verbose = True

# 加载环境变量(确保.env文件中包含AZURE_OPENAI_API_KEY等参数)
load_dotenv()

# 初始化LLM
llm = LLM(
    model="azure/gpt-4o",
    base_url="https://<azure-openai-resource-name>.openai.azure.com",
    api_key=os.getenv("AZURE_OPENAI_API_KEY"),
    api_version="2024-02-15-preview"  # 使用兼容的API版本
)

# 修正后的配置
config = dict(
    llm=dict(
        provider="azure_openai",
        config=dict(
            model="gpt-4o",
            api_key=os.getenv("AZURE_OPENAI_API_KEY"),
            deployment_name="gpt-4o-deployment",  # 替换为你的Azure部署名称
            base_url="https://<azure-openai-resource-name>.openai.azure.com",
            api_version="2024-02-15-preview"
        ),
    ),
    embedder=dict(
        provider="azure_openai",
        config=dict(
            model="text-embedding-ada-002",
            deployment_name="text-embedding-ada-002-deployment",  # 替换为你的嵌入模型部署名称
            api_key=os.getenv("AZURE_OPENAI_API_KEY"),
            base_url="https://<azure-openai-resource-name>.openai.azure.com",
            api_version="2024-02-15-preview"
        ),
    )
)

# 初始化PDF工具
pdf_search_tool = PDFSearchTool(
   config=config,
    pdf='./example_home_inspection.pdf'
)

# 定义Agent
research_agent = Agent(
    role="Research Agent",
    goal="Search through the PDF to find relevant answers",
    allow_delegation=False,
    verbose=True,
    backstory="The research agent is adept at searching and extracting data from documents, ensuring accurate and prompt responses.",
    tools=[pdf_search_tool],
    llm=llm
)

professional_writer_agent = Agent(
    role="Professional Writer",
    goal="Write professional emails based on the research agent's findings",
    allow_delegation=False,
    verbose=True,
    backstory="The professional writer agent has excellent writing skills and is able to craft clear and concise emails based on the provided information.",
    tools=[]
)

# 定义Task
answer_customer_question_task = Task(
    description="Answer the customer's questions based on the home inspection PDF. The research agent will search through the PDF to find the relevant answers. Your final answer MUST be clear and accurate, based on the content of the home inspection PDF.\n\nHere is the customer's question:\n{customer_question}",
    expected_output="Provide clear and accurate answers to the customer's questions based on the content of the home inspection PDF.",
    tools=[pdf_search_tool],
    agent=research_agent,
    llm=llm
)

write_email_task = Task(
    description="- Write a professional email to a contractor based on the research agent's findings.\n- The email should clearly state the issues found in the specified section of the report and request a quote or action plan for fixing these issues.\n- Ensure the email is signed with the following details:\n    \n    Best regards,\n\n    AuthorName",
    expected_output="Write a clear and concise email that can be sent to a contractor to address the issues found in the home inspection report.",
    tools=[],
    agent=professional_writer_agent,
)

# 初始化并启动Crew
crew = Crew(
    agents=[research_agent],
    tasks=[answer_customer_question_task],
    process=Process.sequential
)

customer_question = input("Which section of the report would you like to generate a work order for?\n")
result = crew.kickoff(inputs={"customer_question": customer_question})
print(result)

.env文件示例

确保项目根目录下有.env文件,内容如下:

AZURE_OPENAI_API_KEY="your-azure-openai-api-key"

依赖配置

python = ">=3.10.0,<3.12"
python-dotenv = "1.0.0"
crewai-tools = ">=0.4.26"
crewai = ">=0.41.1"
langchain-anthropic = ">=0.1.20"
langchain-openai=">=0.1.7"

内容的提问来源于stack exchange,提问作者Chandan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 06:58:09