1 Star 0 Fork 0

forest/amazing-openai-api

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
Apache-2.0

Amazing OpenAI API

Convert different model APIs into the OpenAI API format out of the box.

10MB+的小工具,能够将各种不同的模型 API 转换为开箱即用的 OpenAI API 格式。

当前支持模型:

  • Azure OpenAI API (GPT 3.5/4), GPT4 Vision (GPT4v)
  • YI 34B API
  • Google Gemini Pro

下载 📦

访问 GitHub Release 页面,下载适合你的操作系统的执行文件。

或者使用 Docker Pull,下载指定版本的镜像文件:

docker pull soulteary/amazing-openai-api:v0.7.0

快速上手

AOA 不需要编写任何配置文件,通过指定环境变量就能够完成应用行为的调整,包括“选择工作模型”、“设置模型运行需要的参数”、“设置模型兼容别名”。

默认执行 ./aoa ,程序会将工作模型设置为 azure,此时我们设置环境变量 AZURE_ENDPOINT=https://你的部署名称.openai.azure.com/ 然后就可以正常使用服务啦。

AZURE_ENDPOINT=https://你的部署名称.openai.azure.com/ ./aoa

如果你更喜欢 Docker,可以用下面的命令:

docker run --rm -it -e AZURE_ENDPOINT=https://你的部署名称.openai.azure.com/ -p 8080:8080 soulteary/amazing-openai-api:v0.7.0

当服务启动之后,我们就可以通过访问 http://localhost:8080/v1 来访问和 OpenAI 一样的 API 服务啦。

你可以使用 curl 来进行一个快速测试:

curl -v http://127.0.0.1:8080/v1/chat/completions \
    -H "Content-Type: application/json" \
    -H "Authorization: Bearer 123" \
    -d '{
        "model": "gpt-4",
        "messages": [
        {
            "role": "system",
            "content": "You are a poetic assistant, skilled in explaining complex programming concepts with creative flair."
        },
        {
            "role": "user",
            "content": "Compose a poem that explains the concept of recursion in programming."
        }
        ]
    }'

也可以使用 OpenAI 官方 SDK 进行调用,或者任意兼容 OpenAI 的开源软件进行使用(更多例子,参考 example):

from openai import OpenAI

client = OpenAI(
    api_key="your-key-or-input-something-as-you-like",
    base_url="http://127.0.0.1:8080/v1"
)

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

print(chat_completion)

你如果你希望不要将 API Key 暴露给应用,或者不放心各种复杂的开源软件是否有 API Key 泄漏风险,我们可以多配置一个 AZURE_API_KEY=你的 API Key 环境变量,然后各种开源软件在请求的时候就无需再填写 API key 了(或者随便填写也行)。

当然,因为 Azure 的一些限制,以及一些开源软件中的模型调用名称不好调整,我们可以通过下面的方式,来将原始请求中的模型,映射为我们真实的模型名称。比如,将 GPT 3.5/4 都替换为 yi-34b-chat

gpt-3.5-turbo:yi-34b-chat,gpt-4:yi-34b-chat

如果你希望使用 yi-34b-chat,或者 gemini-pro,我们需要设置 AOA_TYPE=yi 或者 AOA_TYPE=gemini,除此之外,没有任何差别。

容器快速上手

项目中包含当前支持的三种模型接口的 docker compose 示例文件,我们将 example 目录中的不同的文件,按需选择使用,将必填的信息填写完毕后,将文件修改为 docker-compose.yml

然后使用 docker compose up 启动服务,就能够快速使用啦。

详细配置使用

调整工作模型 AOA_TYPE,可选参数,默认为 azure

# 选择一个服务, "azure", "yi", "gemini"
AOA_TYPE: "azure"

程序服务地址,可选参数,默认为 80800.0.0.0

# 服务端口,默认 `8080`
AOA_PORT: 8080
# 服务地址,默认 `0.0.0.0`
AOA_HOST: "0.0.0.0"

Azure 使用

如果我们想将 Azure 上部署的 OpenAI 服务转换为标准的 OpenAI 调用,可以用下面的命令:

AZURE_ENDPOINT=https://<你的 Endpoint 地址>.openai.azure.com/ AZURE_API_KEY=<你的 API KEY> AZURE_MODEL_ALIAS=gpt-3.5-turbo:gpt-35 ./amazing-openai-api 

在上面的命令中 AZURE_ENDPOINTAZURE_API_KEY 包含了 Azure OpenAI 服务中的核心要素,因为 Azure 部署 GPT 3.5 / GPT 4 的部署名称不允许包含 .,所以我们使用 AZURE_MODEL_ALIAS 将我们请求内容中的模型名称替换为真实的 Azure 部署名称。甚至可以使用这个技巧将各种开源、闭源软件使用的模型自动映射为我们希望的模型:

# 比如不论是 3.5 还是 4 都映射为 `gpt-35`
AZURE_MODEL_ALIAS=gpt-3.5-turbo:gpt-35,gpt-4:gpt-35

因为我们已经配置了 AZURE_API_KEY,所以开源软件也好,使用 curl 调用也罢,都不需要添加 Authorization: Bearer <你的 API Key> (也可以随便写),这样就起到了严格的 API Key 隔离,提升了 API Key 的安全性。

如果你还是习惯在请求头参数中添加认证内容,可以使用下面的不包含 AZURE_API_KEY 的命令,程序将透传验证到 Azure 服务:

AZURE_ENDPOINT=https://<你的 Endpoint 地址>.openai.azure.com/ AZURE_MODEL_ALIAS=gpt-3.5-turbo:gpt-35 ./amazing-openai-api 

GPT4 Vision

如果你已经拥有了 Azure GPT Vision,除了使用 SDK 调用之外,你也可以参考这篇文档,使用 curl 进行调用:GPT Vision

模型参数设置

# (必选) Azure Deployment Endpoint URL
AZURE_ENDPOINT
# (必选) Azure API Key
AZURE_API_KEY
# (可选) 模型名称,默认 GPT-4
AZURE_MODEL
# (可选) API Version
AZURE_API_VER
# (可选) 是否是 Vision 实例
ENV_AZURE_VISION
# (可选) 模型映射别名
AZURE_MODEL_ALIAS
# (可选) Azure 网络代理
AZURE_HTTP_PROXY
AZURE_SOCKS_PROXY

YI (零一万物)

如果我们想将 YI 官方的 API 转换为标准的 OpenAI 调用,可以用下面的命令:

AOA_TYPE=yi YI_API_KEY=<你的 API KEY> ./amazing-openai-api 

和使用 Azure 服务类似,我们可以使用一个技巧将各种开源、闭源软件使用的模型自动映射为我们希望的模型:

# 比如不论是 3.5 还是 4 都映射为 `gpt-35`
YI_MODEL_ALIAS=gpt-3.5-turbo:yi-34b-chat,gpt-4:yi-34b-chat

如果我们在启动服务的时候配置了 YI_API_KEY 的话,不论是开源软件也好,使用 curl 调用也罢,我们都不需要添加 Authorization: Bearer <你的 API Key> (也可以随便写),这样就起到了严格的 API Key 隔离,提升了 API Key 的安全性。

如果你还是习惯在请求头参数中添加认证内容,可以使用下面的不包含 YI_API_KEY 的命令,程序将透传验证到 Yi API 服务:

./amazing-openai-api 

模型参数设置

# (必选) YI API Key
YI_API_KEY
# (可选) 模型名称,默认 yi-34b-chat
YI_MODEL
# (可选) YI Deployment Endpoint URL
YI_ENDPOINT
# (可选) API Version,默认 v1beta,可选 v1
YI_API_VER
# (可选) 模型映射别名
YI_MODEL_ALIAS
# (可选) Azure 网络代理
YI_HTTP_PROXY
YI_SOCKS_PROXY

Gemini PRO

如果我们想将 Google 官方的 Gemini API 转换为标准的 OpenAI 调用,可以用下面的命令:

AOA_TYPE=gemini GEMINI_API_KEY=<你的 API KEY> ./amazing-openai-api 

和使用 Azure 服务类似,我们可以使用一个技巧将各种开源、闭源软件使用的模型自动映射为我们希望的模型:

# 比如不论是 3.5 还是 4 都映射为 `gpt-35`
GEMINI_MODEL_ALIAS=gpt-3.5-turbo:gemini-pro,gpt-4:gemini-pro

如果我们在启动服务的时候配置了 GEMINI_API_KEY 的话,不论是开源软件也好,使用 curl 调用也罢,我们都不需要添加 Authorization: Bearer <你的 API Key> (也可以随便写),这样就起到了严格的 API Key 隔离,提升了 API Key 的安全性。

如果你还是习惯在请求头参数中添加认证内容,可以使用下面的不包含 GEMINI_API_KEY 的命令,程序将透传验证到 Google AI 服务:

./amazing-openai-api

模型参数设置

# (必选) Gemini API Key
GEMINI_API_KEY

# (可选) Gemini 安全设置,可选 `BLOCK_NONE` / `BLOCK_ONLY_HIGH` / `BLOCK_MEDIUM_AND_ABOVE` / `BLOCK_LOW_AND_ABOVE` / `HARM_BLOCK_THRESHOLD_UNSPECIFIED`
GEMINI_SAFETY
# (可选) Gemini 模型 版本,默认 `gemini-pro`
GEMINI_MODEL
# (可选) Gemini API 版本,默认 `v1beta`
GEMINI_API_VER
# (可选) Gemini API 接口地址
GEMINI_ENDPOINT
# (可选) 模型映射别名
GEMINI_MODEL_ALIAS
# (可选) Gemini 网络代理
GEMINI_HTTP_PROXY
GEMINI_SOCKS_PROXY
Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright [yyyy] [name of copyright owner] Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

简介

将不同的模型API转换为开箱即用的OpenAI API格式。 支持的模型有: - Azure OpenAI API (GPT 3.5/4), GPT4 Vision (GPT4v) - YI 34B API - Google Gemini Pro 展开 收起
README
Apache-2.0
取消

发行版

暂无发行版

贡献者 (2)

全部

近期动态

1年多前创建了仓库
不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/forestcore/amazing-openai-api.git
git@gitee.com:forestcore/amazing-openai-api.git
forestcore
amazing-openai-api
amazing-openai-api
main

搜索帮助