[BUG] TypeError: "Could not resolve authentication method. Expected either api_key or auth_token to be set. Or for one of the `X-Api-Key` or `Authorization` headers to be explicitly omitted"
Required prerequisites
- [x] I have read the documentation https://camel-ai.github.io/camel/camel.html.
- [x] I have searched the Issue Tracker and Discussions that this hasn't already been reported. (+1 or comment there if it has.)
- [x] Consider asking first in a Discussion.
What version of camel are you using?
0.2.45
System information
3.10.9 (main, Apr 19 2023, 00:02:01) [Clang 14.0.0 (clang-1400.0.29.202)] darwin 0.2.45
Problem description
在使用 Camel 库的 ChatAgent 与 Anthropic Claude 模型交互时,遇到认证错误。当通过 ModelFactory 创建 ANTHROPIC 平台的模型并尝试使用时,系统抛出 TypeError 异常,提示无法解析认证方法。
错误信息:
Traceback (most recent call last):
File "/Users/zhangjianlin/.pyenv/versions/3.10.9/lib/python3.10/runpy.py", line 196, in _run_module_as_main
return _run_code(code, main_globals, None,
File "/Users/zhangjianlin/.pyenv/versions/3.10.9/lib/python3.10/runpy.py", line 86, in _run_code
exec(code, run_globals)
File "/Volumes/WD_BLACK/pythonpro/apps/svg_app/main.py", line 81, in X-Api-Key or Authorization headers to be explicitly omitted"
Reproducible example code
The Python snippets:
from camel.models import ModelFactory
from camel.types import ModelPlatformType, ModelType
from camel.agents import ChatAgent
from camel.toolkits import MathToolkit, SearchToolkit
# 创建 Claude 模型
claude_model = ModelFactory.create(
model_platform=ModelPlatformType.ANTHROPIC,
model_type=ModelType.CLAUDE_3_5_SONNET,
api_key=dependency_injector.open_router.key
url=dependency_injector.open_router.base_url
)
# 创建 Claude 代理
claude_agent = ChatAgent(
system_message="You are a helpful assistant.",
model=claude_model,
tools=[
*MathToolkit().get_tools(),
*SearchToolkit().get_tools(),
],
)
# 尝试使用代理
response = claude_agent.step("Hello, who are you?")
Command lines:
python your_script.py
Extra dependencies:
camel-ai
anthropic
dependency_injector
Steps to reproduce:
1.使用上述代码创建 Anthropic Claude 模型和代理 2.尝试调用 claude_agent.step() 方法 3.观察到抛出 TypeError 异常,提示无法解析认证方法
Traceback
Expected behavior
预期 Camel 库应该能够正确处理 Anthropic Claude API 的认证,使用提供的 API 密钥成功创建模型并进行交互,而不是抛出认证错误。
使用 dependency-injector==4.46.0 通过 json文件注入 key 和url。 env 没有设置ANTHROPIC_API_KEY。 通过以下代码初始化了一个claude_model。 使用时候报错
self.claude_model = ModelFactory.create(
model_platform=ModelPlatformType.ANTHROPIC,
model_type=ModelType.CLAUDE_3_5_SONNET,
api_key=self.api_keys.open_router.key,
url=self.api_keys.open_router.base_url,
)
源代码 camel/utils/token_counting.py, self.client = Anthropic() 导致业务崩溃
class AnthropicTokenCounter(BaseTokenCounter):
@dependencies_required('anthropic')
def __init__(self, model: str):
r"""Constructor for the token counter for Anthropic models.
Args:
model (str): The name of the Anthropic model being used.
"""
from anthropic import Anthropic
self.client = Anthropic() // 崩溃
self.model = model
Additional context
No response
ANTHROPIC_API_KEY is set to sk-or-v1-********** ANTHROPIC_API_BASE_URL is set to https://openrouter.ai/api/v1
当我设置了 env环境的 openrouter 的key 后报错下面错误, 应该是 openrouter 平台没有 /v1/messages/count_tokens api 导致。 至少不让client.messages.count_tokens 报错,或提供个降级方案
File "/Volumes/WD_BLACK/pythonpro/apps/svg_app/main.py", line 81, in
thanks @jacksonzhang348 for reporting this issue! We will look into this and resolve it ASAP
@jacksonzhang348 is this a region restriction? https://docs.anthropic.com/en/api/supported-regions
I would like to work on this issue
thanks @JINO-ROHIT , @zhjuzi , I think @jacksonzhang348 is trying to calling anthropic using open router, could you try using example code:https://github.com/camel-ai/camel/blob/master/examples/models/openrouter_llama3.1_example%20.py to take a try? I think maybe Anthropic client doesn't accept openrouter key and url?
thanks @JINO-ROHIT , @zhjuzi , I think @jacksonzhang348 is trying to calling anthropic using open router, could you try using example code:https://github.com/camel-ai/camel/blob/master/examples/models/openrouter_llama3.1_example%20.py to take a try? I think maybe
Anthropicclient doesn't accept openrouter key and url?
yes. I can only use the open_router platform API and baseURL https://openrouter.ai/api/v1, It will report an error。 You support the baseURL feature, so I think this is a bug
self.claude_model = ModelFactory.create(
model_platform=ModelPlatformType.ANTHROPIC,
model_type=ModelType.CLAUDE_3_5_SONNET,
api_key="sk-or-v1-*********",
url="https://openrouter.ai/api/v1",
)
thanks @JINO-ROHIT , @zhjuzi , I think @jacksonzhang348 is trying to calling anthropic using open router, could you try using example code:https://github.com/camel-ai/camel/blob/master/examples/models/openrouter_llama3.1_example%20.py to take a try? I think maybe
Anthropicclient doesn't accept openrouter key and url?
I am currently using ModelPlatformType OPENROUTER has created a model, it can work.
self.openrouter_model = ModelFactory.create(
model_platform=ModelPlatformType.OPENROUTER,
model_type="anthropic/claude-3.7-sonnet",
api_key=self.api_keys.open_router.key,
url=self.api_keys.open_router.base_url,
model_config_dict={"temperature": 0.1},
)
thanks @jacksonzhang348 's feedback, the url for ModelPlatformType.ANTHROPIC should from ANTHROPIC or be accepted by ANTHROPIC