crewai 下 Litellm BadRequestError的解决方案
在CrewAI中使用Litellm调用国内模型时,若遇到BadRequestError错误,需在模型名称前添加"openai/"前缀以标识兼容OpenAI的端点。正确的写法应为model="openai/qwen3-235b-a22b-instruct-2507",同时保持base_url简洁,无需添加额外路径。Litellm会自动处理OpenAI兼容端点的
·
在crewai中写法,尝试下面这种写法:
from crewai import LLM
llm = LLM(
model="qwen3-235b-a22b-instruct-2507",
base_url="https://dashscope.aliyuncs.com/compatible-mode/v1/",
api_key="sk-213123123"
)
if __name__ == "__main__":
response = llm.call(
"Analyze the following messages and return the name, age, and breed. "
"Meet Kona! She is 3 years old and is a black german shepherd."
)
print(response)
报错了:
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
raise e
result = original_function(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
raise exception_type(
model, custom_llm_provider, dynamic_api_key, api_base = get_llm_provider(
^^^^^^^^^^^^^^^^^
raise e
.BadRequestError
debug了一下,我以为Litellm 不支持这种国内的这些接口商呢,还去改代码,其实只要下面这样就行
llm = LLM(
model="openai/qwen3-235b-a22b-instruct-2507",
base_url="https://dashscope.aliyuncs.com/compatible-mode/v1/",
api_key="sk-213123123"
)
加上openai标识这个是兼容OpenAI的端点
参考
https://www.aidoczh.com/litellm/docs/providers/openai_compatible/
要调用位于openai代理后面的模型,请进行以下两处更改:
-
对于
/chat/completions:在模型名称前加上openai/,这样litellm就知道你试图调用openai的/chat/completions端点。 -
对于
/completions:在模型名称前加上text-completion-openai/,这样litellm就知道你试图调用openai的/completions端点。[通过/v1/completions路由调用openai/端点时不需要此操作]。 -
不要在基础URL上添加任何额外内容,例如
/v1/embedding。LiteLLM使用openai-client来发起这些调用,并且会自动添加相关的端点。
更多推荐
所有评论(0)