Giter Site home page Giter Site logo

tian-minghui / openai-style-api Goto Github PK

View Code? Open in Web Editor NEW
304.0 7.0 49.0 301 KB

支持将openai、claude、azure openai, gemini,kimi, 智谱AI,通义千问,讯飞星火API等模型服务方的调用转为openai方式调用。屏蔽不同大模型API的差异,统一用openai api标准格式使用大模型(Shield the differences between different large model APIs and use large models in a unified openai API standard format)

License: MIT License

Dockerfile 0.21% Python 99.79%
azure claude gpt open-ai openai-api xunfei-spark gemini qwen zhipuai kimi

openai-style-api's Introduction

openai-style-api

本人精力有限,某些模型更新可能无法及时更新,如果遇到问题请提issue,也欢迎有兴趣的大佬提PR

用途

屏蔽不同大模型API的差异,统一用openai api标准格式使用大模型, 也可以用来做api-key的二次分发管理; 配置化管理不同大模型调用参数,让你在使用大模型的时候只需关注 api-key 和 messages

功能

  • 支持多种大模型,当前已支持
    • openai
    • azure open ai
    • claude-api 【api申请在等待列表,暂未测试】
    • claude-web (将web端功能封装成openai api)
    • 智谱ai
    • kimi
    • bingchat(copilot)
    • 百度文心一言
    • 讯飞星火
    • gemini
    • 通义千问
    • ...
  • 支持stream方式调用
  • 支持open ai的第三方代理服务,比如openai-sb等
  • 支持在线更新配置 http://0.0.0.0:8090/(这个前端页面和交互完全是用gpt写的 哈哈)
  • 支持负载均衡,一个key可轮训/随机/并行等访问多个模型
  • 支持按照model_name进行路由

更新日志

2024-04-03

  • 支持通义千问
  • 优化异常处理

部署方式

项目的核心配置依赖model-config.json文件,若是没有model-config.json,默认会使用model-config-default.json启动,这时虽然能启动起来,但是因为api-key等没有配置,无法调用成功。

Docker

本地新建一个model-config.json文件,根据下边配置文件示例,进行配置, 然后运行以下命令

docker pull tianminghui/openai-style-api

docker run -d -p 8090:8090 --name openai-style-api\
-e ADMIN-TOKEN=admin \
-v /path/to/your/model-config.json:/app/model-config.json \
tianminghui/openai-style-api

/path/to/your/model-config.json 替换成你自己的本地路径

Docker compose

clone本项目,或者下载项目中的docker-compose.yml文件,修改其中的./model-config.json路径, 然后运行以下命令

docker-compose up -d

本地部署

  1. git clone https://github.com/tian-minghui/openai-style-api.git 拉取项目代码
  2. cp model-config-default.json model-config.json 并按需修改配置文件model-config.json
  3. pip install -r requirements.txt
  4. 运行 python open-api.py

配置说明

model-config.json 配置文件简单示例

    [
        {
            "token": "f2b7295fc440db7f",
            "type": "azure",  // azure openai 模型
            "config": {
                "api_base": "https://xxxx.openai.azure.com/",
                "deployment_id": "gpt-35-turbo",
                "api_version": "2023-05-15",
                "api_key": "xxxxxx",
                "temperature": 0.8
            }
        }
    ]
  • 整个文件是一个json list,可以配置多个模型,只要token不重复就行
  • token 自定义的token,后续在请求的时候拿着这个token来请求
  • type 类型,表示以下config中的配置是那个模型的,比如 openai,通义千问
  • config, 配置openai的api_base, api_key, model等, 针对不用模型有不同的配置(下边有配置示例,更详细配置可以看代码), 此处的配置优先于客户端请求中的配置,比如"temperature": 0.8, 会覆盖请求中的temperature(这里的想法是可以针对同一个模型,调整不同参数,映射成一个新模型)

使用方式

curl

curl http://localhost:8090/v1/chat/completions \
      -H "Content-Type: application/json" \
      -H "Authorization: Bearer f2b7295fc440db7f" \
      -d '{
        "messages": [
          {
            "role": "system",
            "content": "You are a helpful assistant."
          },
          {
            "role": "user",
            "content": "Hello!"
          }
        ]
      }'

openai库调用

openai<1.0.0 使用如下方式

import openai

openai.api_key = "f2b7295fc440db7f"
openai.api_base = "http://localhost:8090/v1"

completion = openai.ChatCompletion.create(
    model="gpt-3.5-turbo", messages=[{"role": "user", "content": "Hello world"}])
print(completion.choices[0].message.content)

openai>=1.0.0使用以下方式调用

import os
from openai import OpenAI

client = OpenAI(
    # This is the default and can be omitted
    api_key='kimi-GxqT3BlbkFJj',
    base_url = 'http://localhost:8090/v1'
)

chat_completion = client.chat.completions.create(
    messages=[
        {
            "role": "user",
            "content": "Say this is a test",
        }
    ],
    model="gpt-3.5-turbo",
)

print(chat_completion.choices[0].message.content)

第三方应用

ChatGPT Next Web Alt text

配置示例

[
{
    "token": "f2b7295fc440db7f",
    "type": "azure",  // azure openai 模型
    "config": {
        "api_base": "https://xxxx.openai.azure.com/",
        "deployment_id": "gpt-35-turbo",
        "api_version": "2023-05-15",
        "api_key": "xxxxxx",
        "temperature": 0.8
    }
},
{
    "token": "GxqT3BlbkFJj",
    "type": "openai", // openai 模型
    "config": {
        "api_base": "https://api.openai.com/v1/",
        "api_key": "sk-xxxxxx",
        "model": "gpt-3.5-turbo"
    }
},
{
    "token": "sb-ede1529390cc",
    "type": "proxy",  // openai 代理 
    "config": {
        "api_base": "https://api.openai-sb.com/v1/",
        "api_key": "sb-xxxxxx",
        "model": "gpt-3.5-turbo"
    }
},
{
    "token": "c115c8f5082",
    "type": "claude-web",  // claude-web 
    "config": {
        "cookie": "xxxxxx",
        "proxies": {
            "https": "http://localhost:7890"
        },
        "conversation_id": "xxxxxx",
        "prompt": "The information in [] is the context of the conversation. Please ignore the JSON format of the context during the conversation and answer the user's latest conversation: {newMessage} \n {history}",
        "single_conversation": true
    }
},
{
    "token": "7c7aa4a3549f5",
    "type": "zhipu-api",  // 智谱API
    "config": {
        "api_key": "xxxxxx",
        "model": "chatglm_lite",
        "temperature": 0.8,
        "top_p": 0.7
    }
},
{
    "token": "7c7aa4a3549f11",
    "type": "xunfei-spark-api", // 讯飞星火API
    "config": {
        "app_id": "xxxx",
        "api_key": "xxxx",
        "api_secret": "xxxxxx",
        "api_model_version": "v2.0",
        "top_k": 5
    }
},
{
    "token": "7c7aa4a3549f12",
    "type": "router", // 路由  可以包含多个模型进行负载均衡
    "config": {
        "router_strategy": "round-robin", // 路由策略  round-robin 轮询   random 随机
        "token_pool": [   // 路由的token池
            "7c7aa4a3549f11",
            "7c7aa4a3549f5"
        ]
    }
},
{
    "token": "7c7aa4a3549f13",
    "type": "model-name-router", //根据req中的modelname进行路由, 可以方便的结合ChatGPT-Next-Web
    "config": {
        "model-2-token": {   // 路由的token池
            "spark-api-v2.0":"7c7aa4a3549f11",
            "chatglm_lite": "7c7aa4a3549f5",
            "router-round-robin": "7c7aa4a3549f12"
        }
    }
},
{
    "token": "gemini-7c7aa4a3549f5",
    "type": "gemini",   // gemini
    "config": {
        "api_key": "xxxxx",
        "proxies": {
            "https": "http://localhost:7890"
        }
    }
},
{
    "token": "bing-7c7aa4a3549f5",  // 必应
    "type": "bing-sydney",
    "config": {
        "cookie": "xxxxx",
        "style": "balanced"
    }
},
{
    "token":"qwen-111111xxxx",  // 通义千问
    "type":"qwen",
    "config":{
        "api_key":"sk-xxxxxxxx",
        "model":"qwen-turbo"
    }
},
{
    "token": "kimi-GxqT3BlbkFJj1", // kimi
    "type": "openai",    // kimi api与openai相同,因此使用openai就可以
    "config": {
        "api_base": "https://api.moonshot.cn/v1/",
        "api_key": "sk-xxxxxx",
        "model": "moonshot-v1-8k"
    }
}
]

项目部分代码来自于以下开源项目,感谢🙏

openai-style-api's People

Contributors

iamsk avatar matrix42 avatar tian-minghui avatar w568w avatar xingwozhonghua126 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

openai-style-api's Issues

请教下如何配置ChatGPT-Next-Web

在同一服务器上启动了openai-style-apiChatGPT-Next-Web,openai-style-api没有问题,可以单独正常使用,但是到了ChatGPT-Next-Web上就一直报连接错误,原因是什么呢
image

docker-compose配置如下

version: '3'

services:
  chatgpt-web:
    image: yidadaa/chatgpt-next-web
    ports:
      - 3000:3000
    environment:
      OPENAI_API_KEY: 7c7aa4a3549f12
      BASE_URL: http://azure-openai:8080
      CODE: ""
      HIDE_USER_API_KEY: 1
      HIDE_BALANCE_QUERY: 1
    depends_on:
      - azure-openai
    links:
      - azure-openai
    networks:
      - chatgpt-ns

  azure-openai:
    image: tianminghui/openai-style-api
    ports:
      - 8080:8080
    environment:
      - ADMIN-TOKEN=admin
    volumes:
      - ./model-config.json:/app/model-config.json
    networks:
      - chatgpt-ns

networks:
  chatgpt-ns:
    driver: bridge

请问可以提供Docker 或者 安装环境/版本吗?

人都装麻了,一会aiohttp装不上,一会

ERROR: Failed building wheel for multidict
Failed to build multidict
ERROR: Could not build wheels for multidict, which is required to install pyproject.toml-based projects

python3.10

谢谢

期望添加或检查 OpenRouter 支持

当前类 OpenAI 类型的 API 比如 KIMI 是能正常使用的,根据 OpenRouter 的文档,理论上也可以使用 OpenAI 的接口,文档给出了使用 openai 包的示例。

根据示例也能正常使用:

openrouter_client = OpenAI(
    api_key='xxx',
    base_url = 'https://openrouter.ai/api/v1'
)

chat_completion = openrouter_client.chat.completions.create(
    messages=[
        {
            "role": "user",
            "content": "Say this is a test",
        }
    ],
    model="meta-llama/llama-3-8b-instruct:free",
)

print(chat_completion.choices[0].message.content)

但是以下配置,OpenRouter 会返回 404 页面:

[
    {
        "token": "xxx",
        "type": "openai",
        "config": {
            "api_base": "https://openrouter.ai/api/v1",
            "api_key": "xxx",
            "model": "meta-llama/llama-3-8b-instruct:free"
        }
    }
]

请问这是配置的问题,还是 API 规范的问题,希望添加或检查 OpenRouter 的支持,谢谢!

添加对 kimi 的支持

几时可以支持 KIMI 的模型?
我看了下 kimi 的接口和 openai 几乎是完全兼容的. 不知道现有是支持的

gemini 1.5

麻烦大佬支持一下让gemini 能输入模型型号

尝试调整"top_p",解决qwen模型api转换时的bug

使用qwen时出现了一个bug
本来的model-config.json

[
    {
        "token":"qwen-1111222211",
        "type":"qwen",
        "config":{
            "api_key":"sk-xxxxxxxxxxxxxx",
            "model":"qwen-turbo"
        }
    }
]

使用chatnext
image
结果

image

bug

{"code":"InvalidParameter","message":"Range of top_p should be (0.0, 1.0)","request_id":"7dc1df54-850d-95c8-bbdf-07112f62c90e"}

看到tian-mh其他的config里面设置了"top_p"
我也设置一下,像他一样"top_p": 0.7

[
    {
        "token":"qwen-1111222211",
        "type":"qwen",
        "config":{
            "api_key":"sk-xxxxxxxxxxxxxxxxxxxxx",
            "model":"qwen-turbo",
            "top_p": 0.7
        }
    }
]

结果
image

我尝试了0.1,0.2,...,0.9, 都可以, 也许是自动归一化吧, 当然就选0.7折中一点

最后十分感谢作者的这个项目, 帮了大忙, 可以用国内的api连接zotero gpt了🌸🌸🌸🌸🌸🌸🌸

更新后似乎出现了问题,无法正常流式输出

我看日志好像是连续发送了两次一样的请求,然后两次都结束之后才正常显示内容

此外,貌似项目没有能够自动更新版本的功能,我得删除了整个容器和对应的镜像之后才能获得更新

无法触发Langchain Agent插件

相同的代理接口,直接配置在chat-next-web-langchain项目可以用,但是配置在该项目无法触发?
初步怀疑是接受参数不同?有待进一步测试

请问腾讯混元模型是否支持

大神,目前部署,并已经可以接入国内的 智谱,星火等。
请问 腾讯的AI大模型是否能支持?如果不支持,请问有计划引入么?万分感谢

不能支持chrome sider插件

好像不能支持chrome sider插件。手机版本的也不支持。
我理解是stream 的处理上好像是有点问题的。

非常好的项目

目前发现的第二个统一闭源模型为openAI接口的方法的repo, 我来测试一下。

TypeError: can only concatenate str (not "bytes") to str

请求
curl http://localhost:8090/v1/chat/completions
-H "Content-Type: application/json"
-H "Authorization: Bearer 111111abc"
-d '{
"messages": [
{
"role": "user",
"content": "Hello!"
}
]
}'

服务报错:

ERROR: Exception in ASGI application
Traceback (most recent call last):
File "/usr/local/lib/python3.10/site-packages/uvicorn/protocols/http/httptools_impl.py", line 426, in run_asgi
result = await app( # type: ignore[func-returns-value]
File "/usr/local/lib/python3.10/site-packages/uvicorn/middleware/proxy_headers.py", line 84, in call
return await self.app(scope, receive, send)
File "/usr/local/lib/python3.10/site-packages/fastapi/applications.py", line 292, in call
await super().call(scope, receive, send)
File "/usr/local/lib/python3.10/site-packages/starlette/applications.py", line 122, in call
await self.middleware_stack(scope, receive, send)
File "/usr/local/lib/python3.10/site-packages/starlette/middleware/errors.py", line 184, in call
raise exc
File "/usr/local/lib/python3.10/site-packages/starlette/middleware/errors.py", line 162, in call
await self.app(scope, receive, _send)
File "/usr/local/lib/python3.10/site-packages/starlette/middleware/cors.py", line 83, in call
await self.app(scope, receive, send)
File "/usr/local/lib/python3.10/site-packages/starlette/middleware/exceptions.py", line 79, in call
raise exc
File "/usr/local/lib/python3.10/site-packages/starlette/middleware/exceptions.py", line 68, in call
await self.app(scope, receive, sender)
File "/usr/local/lib/python3.10/site-packages/fastapi/middleware/asyncexitstack.py", line 20, in call
raise e
File "/usr/local/lib/python3.10/site-packages/fastapi/middleware/asyncexitstack.py", line 17, in call
await self.app(scope, receive, send)
File "/usr/local/lib/python3.10/site-packages/starlette/routing.py", line 718, in call
await route.handle(scope, receive, send)
File "/usr/local/lib/python3.10/site-packages/starlette/routing.py", line 276, in handle
await self.app(scope, receive, send)
File "/usr/local/lib/python3.10/site-packages/starlette/routing.py", line 66, in app
response = await func(request)
File "/usr/local/lib/python3.10/site-packages/fastapi/routing.py", line 273, in app
raw_response = await run_endpoint_function(
File "/usr/local/lib/python3.10/site-packages/fastapi/routing.py", line 192, in run_endpoint_function
return await run_in_threadpool(dependant.call, **values)
File "/usr/local/lib/python3.10/site-packages/starlette/concurrency.py", line 41, in run_in_threadpool
return await anyio.to_thread.run_sync(func, *args)
File "/usr/local/lib/python3.10/site-packages/anyio/to_thread.py", line 33, in run_sync
return await get_asynclib().run_sync_in_worker_thread(
File "/usr/local/lib/python3.10/site-packages/anyio/_backends/_asyncio.py", line 877, in run_sync_in_worker_thread
return await future
File "/usr/local/lib/python3.10/site-packages/anyio/_backends/_asyncio.py", line 807, in run
result = context.run(func, *args)
File "/app/open-api.py", line 110, in create_chat_completion
openai_response = next(resp)
File "/app/adapters/azure.py", line 51, in chat_completions
response = post(url, self.headers, req_args)
File "/app/utils/http_util.py", line 20, in post
raise e
File "/app/utils/http_util.py", line 16, in post
raise Exception("响应异常:" + resp.content)
TypeError: can only concatenate str (not "bytes") to str

tool calls功能

可以加下tool calls功能,agent现在都需要function call相关的能力

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.