aisuite:统一的大模型SDK,简化LLM开发流程

随着LLM提供商的不断增加,开发者面临的挑战也在增加:

● API碎片化:每个提供商的API设计和调用方式不同,增加了学习和维护成本。

● 配置复杂性:不同模型的配置参数和格式各异,导致集成工作量大。

● 模型切换困难:在多个模型之间切换需要大量的代码修改和测试。 为了解决这一问题,吴恩达团队推出了aisuite,一个开源的Python库,旨在对流行的LLM提供商进行集成,方便开发者快速接入不同的模型。

AISuite简介

aisuite是一个轻量级的封装层,提供统一的接口,使开发者能够通过简单的provider:model字符串(例如openai:gpt-4o或anthropic:claude-3-5)在不同的LLM之间切换,而无需重写大量代码。

核心特点

● 统一接口:简化了不同LLM之间的调用差异,降低了迁移成本。

● 灵活切换模型:只需修改模型名称即可切换不同提供商的模型。

● 扩展性强:支持多种LLM提供商,如OpenAI、Anthropic、Google、AWS、Azure、Groq、Mistral、HuggingFace和Ollama等。

支持的模型提供商

支持以下11种模型提供商:

● Anthropic

● AWS

● Azure

● Cerebras

● Google

● Groq

● HuggingFace Ollama

● Mistral

● OpenAI

● Sambanova

● Watsonx

快速上手

以下是使用aisuite进行多LLM调用的基本步骤:

安装依赖

bash 复制代码
uv venv
source .venv/bin/activate  # Linux/macOS
pip install aisuite[all] openai python-dotenv

配置API密钥

.env文件中添加各LLM提供商的API密钥:

bash 复制代码
OPENAI_API_KEY=your_openai_key
ANTHROPIC_API_KEY=your_anthropic_key

编写查询函数

javascript 复制代码
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)

通过上述代码,开发者可以轻松地在不同的LLM模型之间切换,比较它们的输出结果,从而选择最适合特定任务的模型。

工具调用

aisuite为跨支持提供商的工具/函数调用提供了一个简单的抽象层,工具调用抽象使得在不更改代码的情况下,轻松地将工具与不同的大语言模型结合使用。使用aisuite调用工具的方式有两种:

1. 手动工具处理

这是在未指定max_turns时的默认行为,以OpenAI兼容的格式传递工具。

javascript 复制代码
def will_it_rain(location: str, time_of_day: str):
    """Check if it will rain in a location at a given time today.
    
    Args:
        location (str): Name of the city
        time_of_day (str): Time of the day in HH:MM format.
    """
    return "YES"

tools = [{
    "type": "function",
    "function": {
        "name": "will_it_rain",
        "description": "Check if it will rain in a location at a given time today",
        "parameters": {
            "type": "object",
            "properties": {
                "location": {
                    "type": "string",
                    "description": "Name of the city"
                },
                "time_of_day": {
                    "type": "string",
                    "description": "Time of the day in HH:MM format."
                }
            },
            "required": ["location", "time_of_day"]
        }
    }
}]

response = client.chat.completions.create(
    model="openai:gpt-4o",
    messages=messages,
    tools=tools
)

2. 自动工具执行

当指定了max_turns时,可以将可调用的Python函数列表作为工具参数传递,aisuite将自动处理工具调用流程。

javascript 复制代码
def will_it_rain(location: str, time_of_day: str):
    """Check if it will rain in a location at a given time today.
    
    Args:
        location (str): Name of the city
        time_of_day (str): Time of the day in HH:MM format.
    """
    return "YES"

client = ai.Client()
messages = [{
    "role": "user",
    "content": "I live in San Francisco. Can you check for weather "
               "and plan an outdoor picnic for me at 2pm?"
}]


# Automatic tool execution with max_turns
response = client.chat.completions.create(
    model="openai:gpt-4o",
    messages=messages,
    tools=[will_it_rain],
    max_turns=2  # Maximum number of back-and-forth tool calls
)
print(response.choices[0].message.content)

当指定max_turns时,aisuite将:

  1. 将消息发送给大语言模型
  2. 执行大语言模型请求的任何工具调用
  3. 将工具结果发送回大语言模型
  4. 重复上述步骤,直到对话结束或达到max_turns

总结

aisuite作为一个开源的Python库,通过提供统一的接口和灵活的模型切换机制,简化了跨多个LLM提供商的集成过程。对于经常使用不同LLM的开发者来说,可以显著降低开发门槛,提高开发效率,是不容错过的选择。

相关推荐
极客老王说Agent13 小时前
2026实战指南:如何用智能体实现药品不良反应报告的自动录入?
人工智能·ai·chatgpt
叶子Talk15 小时前
GPT-Image-2正式发布:文字渲染99%,Image Arena三项第一,AI图像生成彻底变天了
人工智能·gpt·计算机视觉·ai·openai·图像生成·gpt-image-2
uncle_ll15 小时前
LangChain基础学习笔记
笔记·学习·langchain·llm·rag
Agent产品评测局18 小时前
如何搭建一个药品市场价格监控智能体来实现100%价格一致性? —— 2026全渠道价格均衡化架构实战指南
人工智能·ai·chatgpt·架构
程序员老赵18 小时前
Docker 部署 Open WebUI + Ollama 完整教程(Windows / Linux 通用)—— 打造自己的本地OpenAI
aigc·openai·ai编程
Agent产品评测局20 小时前
销售拓客全流程赋能:企业级销售智能体落地完整解决方案 —— 2026技术路径与选型实测指南
人工智能·ai·chatgpt
Agent产品评测局21 小时前
断网可用:企业级智能体全本地化离线部署完整方案 —— 2026年私有化AI架构实测与选型指南
人工智能·ai·chatgpt·架构
小兵张健1 天前
Codex 使用教程(1):基础页面操作
程序员·openai·ai编程
Irissgwe1 天前
LangChain 与 LangGraph 介绍(一)
人工智能·langchain·llm·langgraph
huisheng_qaq1 天前
【01-AI入门篇】深入理解AI感知智能和认知智能
人工智能·ai·chatgpt·认知智能·感知智能