OneAPI-通过OpenAI API访问所有大模型
OneAPI是OpenAI接口的管理、分发系统,支持如阿里、百度、智谱 ChatGLM、百度文心一言、讯飞星火认知、阿里通义千问、360 智脑以及腾讯混元等国内大部分的大模型。程序中一般使用oneapi表示OneAPI,这里沿用这种习惯也使用oneapi。
大部分Agent、RAG系统使用OpenAI接口访问大模型,然而受条件或成本限制,实践中走不通。
虽然其他大模型也兼容OpenAI接口,但限制较多,客观上需要一个能通过OpenAI接口访问所有大模型的工具。
OneAPI就是这种工具,通过标准的OpenAI API访问所有的大模型,且开箱即用。
https://github.com/songquanpeng/one-api
1 工具介绍
OneAPI是LLM OpenAI接口的管理、分发系统,通过标准的OpenAI API访问所有的大模型。
OneAPI目前(2025.9.10)支持Deepseek、阿里通义千问、百度千帆、火山豆包、腾讯元宝等大部分LLM。
程序中一般使用one-api表示OneAPI,这里沿用这种习惯也使用one-api。
2 安装部署
为简化分析和测试过程,这里采用docker安装,其他安装方式参考
https://github.com/songquanpeng/one-api
假设docker已经安装,centos7安装docker参考如下链接
https://blog.csdn.net/liliang199/article/details/150067330
docker 部署one-api,命令如下
docker run --name one-api -d --restart always -p 3000:3000 -e TZ=Asia/Shanghai -v /data/apps/llm/oneapi/data/one-api:/data justsong/one-api
docker ps查看one-api的运行状态,返回显示docker已经启动
[root@username oneapi]# docker ps |grep one-api
20618168b7f6 justsong/one-api "/one-api" 8 minutes ago Up 8 minutes 0.0.0.0:3000->3000/tcp one-api
浏览器打开如下地址进入one-api页面,默认账号密码为:root/123456
http://locolhost:3000/
示例如下
3 应用测试
OneAPI提供了OpenAPI接口,应用程序要成功访问需要完成以下两步:
1)渠道令牌申请,大模型平台申请令牌并配置在OneAPI中,使OneAPI具备访问权限。
2)访问令牌生成,在OneAPI生成访问令牌,使程序/工具具备访问OneAPI的权限。
3.1 渠道令牌配置
在LLM平台申请令牌APIKey,比如Deepseek、阿里通义千问、百度千帆、火山豆包、腾讯元宝。
在channel页的key项配置已申请令牌,示例如下。
3.2 访问令牌添加
Channel页配置已申请令牌APIKey后,在Token页面中新增访问令牌。
之后使用新增访问令牌访问OneAPI ,使用方式与OpenAI API一致。
用到OpenAI API位置设置API Base为One API部署地址,例如"https://localhost:3000/v1"。
API Key 则为OneAPI Token页面中新增的访问令牌,比如"sk-xxxxxxxxxxxxxx"。
3.3 应用程序测试
这里使用python程序测试OneAPI接口可用性,程序代码如下所示。
from openai import OpenAI
client = OpenAI(
base_url="http://localhost:3000/v1", # OneAPI部署地址
api_key="sk-xxxxxxxxxxxxxx" # OneAPI中生成的令牌
)
response = client.chat.completions.create(
model="deepseek-r1", # mac m1算力小使用4b小模型
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Who won the world series in 2020?"},
{"role": "assistant", "content": "The LA Dodgers won in 2020."},
{"role": "user", "content": "Where was it played?"}
],
temperature=0.7,
max_tokens=512
)
print(response.choices[0].message.content)
输出如下,可见程序通过OneAPI成功以OpenAI接口的方式调用了第三方LLM。
The 2020 World Series was played at **Globe Life Field** in Arlington, Texas. Due to the COVID-19 pandemic, MLB implemented a neutral-site bubble format for the postseason, and all games of the series were held there instead of the home ballparks of the participating teams (Los Angeles Dodgers and Tampa Bay Rays). This was the first time the World Series was played entirely at a single neutral location.
reference
---
OneAPI
https://github.com/songquanpeng/one-api
centos7 安装docker + ragflow示例
https://blog.csdn.net/liliang199/article/details/150067330
如何用OpenAI SDK调用Ollama LLM
更多推荐
所有评论(0)