在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代理后面的模型,请进行以下两处更改:

  1. 对于/chat/completions:在模型名称前加上openai/,这样litellm就知道你试图调用openai的/chat/completions端点。

  2. 对于/completions:在模型名称前加上text-completion-openai/,这样litellm就知道你试图调用openai的/completions端点。[通过/v1/completions路由调用openai/端点时不需要此操作]。

  3. 不要在基础URL上添加任何额外内容,例如/v1/embedding。LiteLLM使用openai-client来发起这些调用,并且会自动添加相关的端点。

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐