日常我们做 AI 程序的时候,不可避免的需要对多个大模型型进行调用。
目前多个大模型统一调用的方案有下面几种,分别是:
今天主要讲第三种方案,一些工具库的对比:
-
• LangChain -
• LlamaIndex -
• LiteLLM -
• UnionLLM -
• AISuite
LangChain
LangChain 集成了国内外几乎所有的模型服务,并对每个服务进行封装。 可以在下面页面查看完整的列表:https://python.langchain.com/docs/integrations/llms/[1]
LangChain 设计了 LLM 的基础类,并通过 社区实现了各厂家的模型,可以直接调用。
class LLM(BaseLLM):
"""Simple interface for implementing a custom LLM.
You should subclass this class and implement the following:
- `_call` method: Run the LLM on the given prompt and input (used by `invoke`).
- `_identifying_params` property: Return a dictionary of the identifying parameters
This is critical for caching and tracing purposes. Identifying parameters
is a dict that identifies the LLM.
It should mostly include a `model_name`.
...
这样可以较一致的接口调用不同的模型,比如 OpenAI:
from langchain_openai import OpenAI
llm = OpenAI()
llm.invoke("What NFL team won the Super Bowl in the year Justin Bieber was born?")
又或者通义千问:
from langchain_community.llms import Tongyi
llm = Tongyi()
llm.invoke("What NFL team won the Super Bowl in the year Justin Bieber was born?")
我们在切换模型的的时候只需要 引入相关的模型,配置,然后创建实例即可。
OpenAI Adapter
值得一提的是 LangChain 还有一个 OpenAI Adapter, 通过适配器来使 LangChain 模型适应 OpenAI API,从而尽可能轻松地探索其他模型。
我们不需要引入不同的包,只需要在适配器中通过字符串配置模型即可。
result = openai.chat.completions.create(
messages=messages,
model="gpt-3.5-turbo",
temperature=0
)
result = openai.chat.completions.create(
messages=messages,
model="claude-2",
temperature=0,
stream=True,
provider="ChatAnthropic",
)
LlamaIndex
LlamaIndex 也集成了市面上常见的工具,对每个模型进行封装,接口调用更简洁。
from llama_index.llms.openai import OpenAI
llm = OpenAI(model="gpt-3.5-turbo")
stream = llm.stream("Hi, write a short story")
for r in stream:
print(r.delta, end="")
下面页面可以查看所有支持的模型信息: https://docs.llamaindex.ai/en/stable/api_reference/llms/[2]
LiteLLM
LiteLLM[3] 同样集成了市面上常见的工具(主要是国外的模型和工具),接口调用更简洁,不用单独引入不同厂商的包, 配制好好环境变量之后,就可以直接调用了。
from litellm import completion
response = completion(
model = "gpt-4o",
messages=[{ "content": "Hello, how are you?","role": "user"}]
)
又或者调用本地的 Ollama:
response = completion(
model="ollama/llama2",
messages=[{ "content": "respond in 20 words. who are you?","role": "user"}],
api_base="http://localhost:11434"
)
UnionLLM
UnionLLM[4] 是一个通过与OpenAI兼容的统一方式调用各种国内外各种大语言模型和Agent编排工具的轻量级开源Python工具包。
在 LiteLLM 上,对国内常见的模型进行了封装:
from unionllm import unionchat
unionchat(
provider="zhipuai",
model="glm-4",
messages=[{"content": "你的开发者是谁?", "role": "user"}],
stream=False
)
AISuite
AI Suite[5] 是最近比较火的一个工具,只做了一件事就是集成模型调用。
也是避免了不同该模型的引用,使用 <provider>:<model> 的字符串形式直接配置调用:
import aisuite as ai
client = ai.Client()
models = ["openai:gpt-4o", "anthropic:claude-3-5-sonnet-20240620"]
messages = [
{"role": "system", "content": "Respond in Pirate English."},
{"role": "user", "content": "Tell me a joke."},
]
for model in models:
response = client.chat.completions.create(
model=model,
messages=messages,
temperature=0.75
)
print(response.choices[0].message.content)
对比
对这几个工具简单做个对比评价。
易用性
易用性上来说,几个工具都不复杂,最复杂也只是引入不同的包,然后创建实例即可。
而后三款只需要使用相同的接口,稍好一点。后两者显示指定 Provider,在应用中心更容易使用和维护。
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
支持模型数量
LangChain 和 LlamaIndex 都支持国内外常见的模型(以国外为主), LiteLLM 和 AI Suite 专注于国外的生态,对国内不太友好。
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
最后提一下 AI Suite 的代码极简单,大家如果需要自己封装的多模型调用库的话,可以拿来参考。
引用链接
[1]: https://python.langchain.com/docs/integrations/llms/[2]: https://docs.llamaindex.ai/en/stable/api_reference/llms/[3] LiteLLM: https://docs.litellm.ai/[4] UnionLLM: https://github.com/EvalsOne/UnionLLM[5] AI Suite: https://github.com/andrewyng/aisuite
--- END ---

