General Introduction
No front-end , pure configuration file configuration API channel . Just write a file to run up an API station of their own , the document has a detailed configuration guide , white friendly .
uni-api is a project to unify the management of large model APIs, allowing multiple back-end services to be invoked through a single unified API interface and converted to OpenAI format with load balancing support. The project is particularly suitable for individual users who do not need a complex front-end interface, and supports a wide range of models and service providers, including OpenAI, Anthropic, Gemini, Vertex, and others.
Function List
- Unified API interface: Call multiple back-end services through a single unified API interface.
- format conversion: Convert APIs from different service providers to OpenAI format.
- load balancing: Supports a variety of load balancing strategies, including polling, weighted polling, and so on.
- Multi-service support: Support for OpenAI, Anthropic, Gemini, Vertex and many other service providers.
- Configuration file management: Manage API channels and models through configuration files.
- auto-retry: Automatically retry the next channel when an API request fails.
- privilege control: Supports fine-grained privilege control and flow-limiting settings.
Using Help
Lightweight Deployment
After clicking the One-Click Deploy button above, set the environment variables CONFIG_URL
for the direct chain of profiles. DISABLE_DATABASE
to true and click Create to create the project.
2.serv00 Remote Deployment
First, log in to the panel, click on the tab Run your own applications in Additional services to allow running your own applications, then go to the panel Port reservation to open a random port.
If you don't have your own domain name, go to the panel WWW websites and delete the default domain name, then create a new domain name Domain for the domain name you just deleted, click Advanced settings and set the Website type to Proxy domain name, the Proxy port pointing to the port you just opened, and don't check Use HTTPS.
ssh Login to the serv00 server and execute the following command:
git clone --depth 1 -b main --quiet https://github.com/yym68686/uni-api.git cd uni-api python -m venv uni-api tmux new -s uni-api source uni-api/bin/activate export CFLAGS="-I/usr/local/include" export CXXFLAGS="-I/usr/local/include" export CC=gcc export CXX=g++ export MAX_CONCURRENCY=1 export CPUCOUNT=1 export MAKEFLAGS="-j1" CMAKE_BUILD_PARALLEL_LEVEL=1 cpuset -l 0 pip install -vv -r requirements.txt cpuset -l 0 pip install -r -vv requirements.txt
ctrl+b d Exit tmux Wait a few hours for the installation to complete, and when it does, execute the following command:
tmux attach -t uni-api source uni-api/bin/activate export CONFIG_URL=http://file_url/api.yaml export DISABLE_DATABASE=true # 修改端口,xxx 为端口,自行修改,对应刚刚在面板 Port reservation 开的端口 sed -i '' 's/port=8000/port=xxx/' main.py sed -i '' 's/reload=True/reload=False/' main.py python main.py
Use ctrl+b d to exit tmux to allow the program to run in the background. At this point you can use uni-api in other chat clients. curl test script:
curl -X POST https://xxx.serv00.net/v1/chat/completions \ -H 'Content-Type: application/json' \ -H 'Authorization: Bearer sk-xxx' \ -d '{"model": "gpt-4o","messages": [{"role": "user","content": "你好"}]}'
3.Docker Local Deployment
Launch Container
docker run --user root -p 8001:8000 --name uni-api -dit \ -e CONFIG_URL=http://file_url/api.yaml \ # 如果已经挂载了本地配置文件,不需要设置 CONFIG_URL -v ./api.yaml:/home/api.yaml \ # 如果已经设置 CONFIG_URL,不需要挂载配置文件 -v ./uniapi_db:/home/data \ # 如果不想保存统计数据,不需要挂载该文件夹 yym68686/uni-api:latest
Or, if you want to use Docker Compose, here's a docker-compose.yml example:
services: uni-api: container_name: uni-api image: yym68686/uni-api:latest environment: - CONFIG_URL=http://file_url/api.yaml # 如果已经挂载了本地配置文件,不需要设置 CONFIG_URL ports: - 8001:8000 volumes: - ./api.yaml:/home/api.yaml # 如果已经设置 CONFIG_URL,不需要挂载配置文件 - ./uniapi_db:/home/data # 如果不想保存统计数据,不需要挂载该文件夹
CONFIG_URL is the configuration file that can be downloaded automatically from a remote location. For example, if you don't have the convenience of modifying the configuration file on a certain platform, you can pass the configuration file to a certain hosting service, which can provide a direct link for uni-api to download, and CONFIG_URL is this direct link. If you use locally mounted configuration files, you don't need to set CONFIG_URL. CONFIG_URL is used when it is inconvenient to mount configuration files.
Running the Docker Compose container in the background
docker-compose pull docker-compose up -d
Docker Build
docker build --no-cache -t uni-api:latest -f Dockerfile --platform linux/amd64 . docker tag uni-api:latest yym68686/uni-api:latest docker push yym68686/uni-api:latest
Restarting a Docker image with a single click
set -eu docker pull yym68686/uni-api:latest docker rm -f uni-api docker run --user root -p 8001:8000 -dit --name uni-api \ -e CONFIG_URL=http://file_url/api.yaml \ -v ./api.yaml:/home/api.yaml \ -v ./uniapi_db:/home/data \ yym68686/uni-api:latest docker logs -f uni-api
RESTful curl test
curl -X POST http://127.0.0.1:8000/v1/chat/completions \ -H "Content-Type: application/json" \ -H "Authorization: Bearer ${API}" \ -d '{"model": "gpt-4o","messages": [{"role": "user", "content": "Hello"}],"stream": true}'
Installation process
- Preparing the configuration file: Create a file named
api.yaml
configuration file, filling in the service provider information, API address, and API key. - Uploading configuration files: Upload the configuration file to a cloud drive for a direct link to the file.
- Starting a Docker Container::
- utilization
CONFIG_URL
The environment variable sets the URL of the configuration file. - set up
DISABLE_DATABASE
because oftrue
The - Start the container using the Docker command:
docker run -d --name uni-api -e CONFIG_URL=http://file_url/api.yaml -e DISABLE_DATABASE=true yym68686/uni-api:latest
- utilization
- Configure the port: Open a random port in the panel and point it to the Docker container.
Usage Process
- invoke an API: Calling back-end services using a unified API interface, supporting multiple models and service providers.
- load balancing: Automatically assigns requests to different channels based on the load balancing policy in the configuration file.
- privilege control: Controls the scope of API key usage and flow limiting based on the permission settings in the configuration file.
- auto-retry: Ensure high availability by automatically retrying the next available channel when a request fails.
Detailed steps
- Configuration File Example::
providers: - provider: provider_name # 服务提供商名称, 如 openai、anthropic、gemini、openrouter、deepbricks,随便取名字,必填 base_url: https://api.your.com/v1/chat/completions # 后端服务的API地址,必填 api: sk-YgS6GTi0b4bEabc4C # 提供商的API Key,必填 model: # 选填,如果不配置 model,会自动通过 base_url 和 api 通过 /v1/models 端点获取可用的所有模型。 - gpt-4o # 可以使用的模型名称,必填 - claude-3-5-sonnet-20240620: claude-3-5-sonnet # 重命名模型,claude-3-5-sonnet-20240620 是服务商的模型名称,claude-3-5-sonnet 是重命名后的名字,可以使用简洁的名字代替原来复杂的名称,选填 - dall-e-3 - provider: anthropic base_url: https://api.anthropic.com/v1/messages api: # 支持多个 API Key,多个 key 自动开启轮训负载均衡,至少一个 key,必填 - sk-ant-api03-bNnAOJyA-xQw_twAA - sk-ant-api02-bNnxxxx model: - claude-3-5-sonnet-20240620: claude-3-5-sonnet # 重命名模型,claude-3-5-sonnet-20240620 是服务商的模型名称,claude-3-5-sonnet 是重命名后的名字,可以使用简洁的名字代替原来复杂的名称,选填 tools: true # 是否支持工具,如生成代码、生成文档等,默认是 true,选填 - provider: gemini base_url: https://generativelanguage.googleapis.com/v1beta # base_url 支持 v1beta/v1, 仅供 Gemini 模型使用,必填 api: AIzaSyAN2k6IRdgw model: - gemini-1.5-pro - gemini-1.5-flash-exp-0827: gemini-1.5-flash # 重命名后,原来的模型名字 gemini-1.5-flash-exp-0827 无法使用,如果要使用原来的名字,可以在 model 中添加原来的名字,只要加上下面一行就可以使用原来的名字了 - gemini-1.5-flash-exp-0827 # 加上这一行,gemini-1.5-flash-exp-0827 和 gemini-1.5-flash 都可以被请求 tools: true - provider: vertex project_id: gen-lang-client-xxxxxxxxxxxxxx # 描述: 您的Google Cloud项目ID。格式: 字符串,通常由小写字母、数字和连字符组成。获取方式: 在Google Cloud Console的项目选择器中可以找到您的项目ID。 private_key: "-----BEGIN PRIVATE KEY-----\nxxxxx\n-----END PRIVATE" # 描述: Google Cloud Vertex AI服务账号的私钥。格式: 一个 JSON 格式的字符串,包含服务账号的私钥信息。获取方式: 在 Google Cloud Console 中创建服务账号,生成JSON格式的密钥文件,然后将其内容设置为此环境变量的值。 client_email: xxxxxxxxxx@xxxxxxx.gserviceaccount.com # 描述: Google Cloud Vertex AI 服务账号的电子邮件地址。格式: 通常是形如 "service-account-name@project-id.iam.gserviceaccount.com" 的字符串。获取方式: 在创建服务账号时生成,也可以在 Google Cloud Console 的"IAM与管理"部分查看服务账号详情获得。 model: - gemini-1.5-pro - gemini-1.5-flash - claude-3-5-sonnet@20240620: claude-3-5-sonnet - claude-3-opus@20240229: claude-3-opus - claude-3-sonnet@20240229: claude-3-sonnet - claude-3-haiku@20240307: claude-3-haiku tools: true notes: https://xxxxx.com/ # 可以放服务商的网址,备注信息,官方文档,选填 - provider: cloudflare api: f42b3xxxxxxxxxxq4aoGAh # Cloudflare API Key,必填 cf_account_id: 8ec0xxxxxxxxxxxxe721 # Cloudflare Account ID,必填 model: - '@cf/meta/llama-3.1-8b-instruct': llama-3.1-8b # 重命名模型,@cf/meta/llama-3.1-8b-instruct 是服务商的原始的模型名称,必须使用引号包裹模型名,否则yaml语法错误,llama-3.1-8b 是重命名后的名字,可以使用简洁的名字代替原来复杂的名称,选填 - '@cf/meta/llama-3.1-8b-instruct' # 必须使用引号包裹模型名,否则yaml语法错误 - provider: other-provider base_url: https://api.xxx.com/v1/messages api: sk-bNnAOJyA-xQw_twAA model: - causallm-35b-beta2ep-q6k: causallm-35b - anthropic/claude-3-5-sonnet tools: false engine: openrouter # 强制使用某个消息格式,目前支持 gpt,claude,gemini,openrouter 原生格式,选填 api_keys: - api: sk-KjjI60Yf0JFWxfgRmXqFWyGtWUd9GZnmi3KlvowmRWpWpQRo # API Key,用户使用本服务需要 API key,必填 model: # 该 API Key 可以使用的模型,必填。默认开启渠道级轮询负载均衡,每次请求模型按照 model 配置的顺序依次请求。与 providers 里面原始的渠道顺序无关。因此你可以设置每个 API key 请求顺序不一样。 - gpt-4o # 可以使用的模型名称,可以使用所有提供商提供的 gpt-4o 模型 - claude-3-5-sonnet # 可以使用的模型名称,可以使用所有提供商提供的 claude-3-5-sonnet 模型 - gemini/* # 可以使用的模型名称,仅可以使用名为 gemini 提供商提供的所有模型,其中 gemini 是 provider 名称,* 代表所有模型 role: admin - api: sk-pkhf60Yf0JGyJxgRmXqFQyTgWUd9GZnmi3KlvowmRWpWqrhy model: - anthropic/claude-3-5-sonnet # 可以使用的模型名称,仅可以使用名为 anthropic 提供商提供的 claude-3-5-sonnet 模型。其他提供商的 claude-3-5-sonnet 模型不可以使用。这种写法不会匹配到other-provider提供的名为anthropic/claude-3-5-sonnet的模型。 - <anthropic/claude-3-5-sonnet> # 通过在模型名两侧加上尖括号,这样就不会去名为anthropic的渠道下去寻找claude-3-5-sonnet模型,而是将整个 anthropic/claude-3-5-sonnet 作为模型名称。这种写法可以匹配到other-provider提供的名为 anthropic/claude-3-5-sonnet 的模型。但不会匹配到anthropic下面的claude-3-5-sonnet模型。 - openai-test/text-moderation-latest # 当开启消息道德审查后,可以使用名为 openai-test 渠道下的 text-moderation-latest 模型进行道德审查。 preferences: SCHEDULING_ALGORITHM: fixed_priority # 当 SCHEDULING_ALGORITHM 为 fixed_priority 时,使用固定优先级调度,永远执行第一个拥有请求的模型的渠道。默认开启,SCHEDULING_ALGORITHM 缺省值为 fixed_priority。SCHEDULING_ALGORITHM 可选值有:fixed_priority,round_robin,weighted_round_robin, lottery, random。 # 当 SCHEDULING_ALGORITHM 为 random 时,使用随机轮训负载均衡,随机请求拥有请求的模型的渠道。 # 当 SCHEDULING_ALGORITHM 为 round_robin 时,使用轮训负载均衡,按照顺序请求用户使用的模型的渠道。 AUTO_RETRY: true # 是否自动重试,自动重试下一个提供商,true 为自动重试,false 为不自动重试,默认为 true RATE_LIMIT: 2/min # 支持限流,每分钟最多请求次数,可以设置为整数,如 2/min,2 次每分钟、5/hour,5 次每小时、10/day,10 次每天,10/month,10 次每月,10/year,10 次每年。默认60/min,选填 ENABLE_MODERATION: true # 是否开启消息道德审查,true 为开启,false 为不开启,默认为 false,当开启后,会对用户的消息进行道德审查,如果发现不当的消息,会返回错误信息。 # 渠道级加权负载均衡配置示例 - api: sk-KjjI60Yd0JFWtxxxxxxxxxxxxxxwmRWpWpQRo model: - gcp1/*: 5 # 冒号后面就是权重,权重仅支持正整数。 - gcp2/*: 3 # 数字的大小代表权重,数字越大,请求的概率越大。 - gcp3/*: 2 # 在该示例中,所有渠道加起来一共有 10 个权重,及 10 个请求里面有 5 个请求会请求 gcp1/* 模型,2 个请求会请求 gcp2/* 模型,3 个请求会请求 gcp3/* 模型。 preferences: SCHEDULING_ALGORITHM: weighted_round_robin # 仅当 SCHEDULING_ALGORITHM 为 weighted_round_robin 并且上面的渠道如果有权重,会按照加权后的顺序请求。使用加权轮训负载均衡,按照权重顺序请求拥有请求的模型的渠道。当 SCHEDULING_ALGORITHM 为 lottery 时,使用抽奖轮训负载均衡,按照权重随机请求拥有请求的模型的渠道。没设置权重的渠道自动回退到 round_robin 轮训负载均衡。 AUTO_RETRY: true preferences: # 全局配置 model_timeout: # 模型超时时间,单位为秒,默认 100 秒,选填 gpt-4o: 10 # 模型 gpt-4o 的超时时间为 10 秒,gpt-4o 是模型名称,当请求 gpt-4o-2024-08-06 等模型时,超时时间也是 10 秒 claude-3-5-sonnet: 10 # 模型 claude-3-5-sonnet 的超时时间为 10 秒,当请求 claude-3-5-sonnet-20240620 等模型时,超时时间也是 10 秒 default: 10 # 模型没有设置超时时间,使用默认的超时时间 10 秒,当请求的不在 model_timeout 里面的模型时,超时时间默认是 10 秒,不设置 default,uni-api 会使用 环境变量 TIMEOUT 设置的默认超时时间,默认超时时间是 100 秒 o1-mini: 30 # 模型 o1-mini 的超时时间为 30 秒,当请求名字是 o1-mini 开头的模型时,超时时间是 30 秒 o1-preview: 100 # 模型 o1-preview 的超时时间为 100 秒,当请求名字是 o1-preview 开头的模型时,超时时间是 100 秒 cooldown_period: 300 # 渠道冷却时间,单位为秒,默认 300 秒,选填。当模型请求失败时,会自动将该渠道排除冷却一段时间,不再请求该渠道,冷却时间结束后,会自动将该模型恢复,直到再次请求失败,会重新冷却。当 cooldown_period 设置为 0 时,不启用冷却机制。
- Launch Container::
docker run -d --name uni-api -e CONFIG_URL=http://file_url/api.yaml -e DISABLE_DATABASE=true yym68686/uni-api:latest
- invoke an API::
curl -X POST https://api.your.com/v1/chat/completions -H "Authorization: Bearer sk-Pkj60Yf8JFWxfgRmXQFWyGtWUddGZnmi3KlvowmRWpWpQxx" -d '{"model": "gpt-4o", "messages": [{"role": "user", "content": "Hello!"}]}'