Skip to content

Latest commit

 

History

History
132 lines (107 loc) · 5.84 KB

File metadata and controls

132 lines (107 loc) · 5.84 KB

通过LiteLLM调用其他大模型

如果传入的provider参数不在UnionLLM支持的厂商范围内(或者没有传入provider厂商),UnionLLM会将请求转发给LiteLLM,因此可以通过UnionLLM调用LiteLLM支持的任何大模型。

不过,UnionLLM推荐provider和model参数分别传入,而LiteLLM只有model参数,不支持provider参数。因此,推荐将LiteLLM支持的model名称转化为provider和model参数传入UnionLLM。

例如:

调用AWS bedrock上的claude2.1模型时,直接调用LiteLLM的方式是:

import litellm
response = litellm.completion(
    model="bedrock/anthropic.claude-instant-v1",
    model_id="provisioned-model-arn",
    messages=[{"content": "Hello, how are you?", "role": "user"}],
)

通过UnionLLM调用AWS bedrock上的claude2.1模型时,推荐将model转化为provider和model参数传入:

from unionllm import unionchat
response = unionchat(
    provider="bedrock",
    model="anthropic.claude-instant-v1",
    messages=[{"content": "Hello, how are you?", "role": "user"}],
)

之所以这样推荐是因为我们认为将provider与model分开更加清晰明了。不过,即使你使用LiteLLM的传参方式,不传入provider参数,UnionLLM也可以兼容,并不会导致出错。

以下以Mistral为例,展示如何通过UnionLLM调用LiteLLM支持的大模型。

通过环境变量设置鉴权参数

import os 
os.environ["MISTRAL_API_KEY"] = "your-mistral-api-key"

非流式调用

from unionllm import unionchat

# model call
response = unionchat(
    provider="mistral",
    model="mistral-tiny", 
    messages = [{ "content": "Hello, how are you?","role": "user"}],
    stream=False
)

print(response)

流式调用

from unionllm import unionchat

# model call
response = unionchat(
    provider="mistral",
    model="mistral-tiny", 
    messages = [{ "content": "Hello, how are you?","role": "user"}],
    stream=True
)

for chunk in response:
    print(chunk)

直接传入鉴权参数调用

# model call
response = unionchat(
    provider="mistral",
    model="mistral-tiny", 
    app_key="your-mistral-api-key",
    messages = [{ "content": "Hello, how are you?","role": "user"}]
)

兼容不传入provider参数的调用方式

from unionllm import unionchat

# model call
response = unionchat(
    model="mistral/mistral-tiny", 
    messages = [{ "content": "Hello, how are you?","role": "user"}]
)

如果你选择传入provider参数,以下是LiteLLM支持厂商的provider列表:

Provider Code
openai openai
azure azure
aws - sagemaker sagemaker
aws - bedrock bedrock
google - vertex_ai [Gemini] vertex_ai
google - palm palm
google AI Studio - gemini gemini
mistral ai api mistral
cloudflare AI Workers cloudflare
cohere cohere
anthropic anthropic
huggingface huggingface
replicate replicate
together_ai together_ai
openrouter openrouter
ai21 ai21
baseten baseten
vllm vllm
nlp_cloud nlp_cloud
aleph alpha aleph_alpha
petals petals
ollama ollama
deepinfra deepinfra
perplexity-ai perplexity
Groq AI groq
Deepseek deepseek
anyscale anyscale
IBM - watsonx.ai watsonx
voyage ai voyage
xinference [Xorbits Inference] xinference

你也可以对照LiteLLM文档查看支持的provider列表。