## 使用指南
### 快速入门
**安装**
```bash
pip install openai-forward
# 或安装webui版本:
pip install openai-forward[webui]
```
**启动服务**
```bash
aifd run
# 或启动带webui的服务
aifd run --webui
```
如果读入了根路径的`.env`的配置, 将会看到以下启动信息
```bash
❯ aifd run
╭────── 🤗 openai-forward is ready to serve! ───────╮
│ │
│ base url https://api.openai.com │
│ route prefix / │
│ api keys False │
│ forward keys False │
│ cache_backend MEMORY │
╰────────────────────────────────────────────────────╯
╭──────────── ⏱️ Rate Limit configuration ───────────╮
│ │
│ backend memory │
│ strategy moving-window │
│ global rate limit 100/minute (req) │
│ /v1/chat/completions 100/2minutes (req) │
│ /v1/completions 60/minute;600/hour (req) │
│ /v1/chat/completions 60/second (token) │
│ /v1/completions 60/second (token) │
╰────────────────────────────────────────────────────╯
INFO: Started server process [191471]
INFO: Waiting for application startup.
INFO: Application startup complete.
INFO: Uvicorn running on http://0.0.0.0:8000 (Press CTRL+C to quit)
```
### 代理OpenAI模型:
`aifd run`的默认选项便是代理`https://api.openai.com`
下面以搭建好的服务地址`https://api.openai-forward.com` 为例
**Python**
```diff
from openai import OpenAI # pip install openai>=1.0.0
client = OpenAI(
+ base_url="https://api.openai-forward.com/v1",
api_key="sk-******"
)
```
<details >
<summary> 更多</summary>
#### 在三方应用中使用
基于开源项目[ChatGPT-Next-Web](https://github.com/Yidadaa/ChatGPT-Next-Web)中接入:
替换docker启动命令中的 `BASE_URL`为自己搭建的代理服务地址
```bash
docker run -d \
-p 3000:3000 \
-e OPENAI_API_KEY="sk-******" \
-e BASE_URL="https://api.openai-forward.com" \
-e CODE="******" \
yidadaa/chatgpt-next-web
```
**Image Generation (DALL-E)**
```bash
curl --location 'https://api.openai-forward.com/v1/images/generations' \
--header 'Authorization: Bearer sk-******' \
--header 'Content-Type: application/json' \
--data '{
"prompt": "A photo of a cat",
"n": 1,
"size": "512x512"
}'
```
</details>
### 代理本地模型
- **适用场景:** 与 [LocalAI](https://github.com/go-skynet/LocalAI),
[api-for-open-llm](https://github.com/xusenlinzy/api-for-open-llm)等项目一起使用
- **如何操作:**
以LocalAI为例,如果已在 http://localhost:8080 部署了LocalAI服务,仅需在环境变量或 .env
文件中设置 `FORWARD_CONFIG=[{"base_url":"http://localhost:8080","route":"/localai","type":"openai"}]`。
然后即可通过访问 http://localhost:8000/localai 使用LocalAI。
(更多)
### 代理其它云端模型
- **场景1:**
使用通用转发,可对任意来源服务进行转发,
可获得请求速率控制与token速率控制;但通用转发不支持自定义秘钥.
- **场景2:**
可通过 [LiteLLM](https://github.com/BerriAI/litellm) 可以将 众多云模型的 API 格式转换为 openai
的api格式,然后使用openai风格转发
(更多)
<a>
<img src="https://raw.githubusercontent.com/KenyonY/openai-forward/main/.github/images/separators/aqua.png" height=8px width="100%">
</a>
## 配置
执行 `aifd run --webui` 进入配置页面 (默认服务地址 http://localhost:8001)
你可以在项目的运行目录下创建 .env 文件来定制各项配置。参考配置可见根目录下的
[.env.example](.env.example)文件
### 智能缓存
开启缓存后,将会对指定路由的内容进行缓存,其中转发类型分别为`openai`与`general`两者行为略有不同,
使用`general`转发时,默认会将相同的请求一律使用缓存返回,
使用`openai`转发时,在开启缓存后,可以通过OpenAI 的`extra_body`参数来控制缓存的行为,如
**Python**
```diff
from openai import OpenAI
client = OpenAI(
+ base_url="https://smart.openai-forward.com/v1",
api_key="sk-******"
)
completion = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{"role": "user", "content": "Hello!"}
],
+ extra_body={"caching": True}
)
```
**Curl**
```bash
curl https://smart.openai.com/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-******" \
-d '{
"model": "gpt-3.5-turbo",
"messages": [{"role": "user", "content": "Hello!"}],
"caching": true
}'
```
### 自定义秘钥
<details open>
<summary>Click for more details</summary>
见.env文件
**用例:**
```diff
import openai
+ openai.api_base = "https://api.openai-forward.com/v1"
- openai.api_key = "sk-******"
+ openai.api_key = "fk-******"
```
</details>
### 多目标服务转发
支持转发不同地址的服务至同一端口的不同路由下
用例见 `.env.example`
### 对话日志
<details open>
<summary>Click for more details</summary>
保存路径在当前目录下的`Log/openai/chat/chat.log`路径中。
记录格式为
```text
{'messages': [{'role': 'user', 'content': 'hi'}], 'model': 'gpt-3.5-turbo', 'stream': True, 'max_tokens': None, 'n': 1, 'temperature': 1, 'top_p': 1, 'logit_bias': None, 'frequency_penalty': 0, 'presence_penalty': 0, 'stop': None, 'user': None, 'ip': '127.0.0.1', 'uid': '2155fe1580e6aed626aa1ad74c1ce54e', 'datetime': '2023-10-17 15:27:12'}
{'assistant': 'Hello! How can I assist you today?', 'is_tool_calls': False, 'uid': '2155fe1580e6aed626aa1ad74c1ce54e'}
```
转换为`json`格式:
```bash
aifd convert
```
得到`chat_openai.json`:
```json
[
{
"datetime": "2023-10-17 15:27:12",
"ip": "127.0.0.1",
"model": "gpt-3.5-turbo",
"temperature": 1,
"messages": [
{
"user": "hi"
}
],
"tools": null,
"is_tool_calls": false,
"assistant": "Hello! How can I assist you today?"
}
]
```
没有合适的资源?快使用搜索试试~ 我知道了~
大模型应用-大语言模型高效转发服务-优质项目实战.zip
共82个文件
py:40个
png:18个
yaml:4个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 5 浏览量
2024-03-26
17:35:38
上传
评论
收藏 338KB ZIP 举报
温馨提示
大模型应用_大语言模型高效转发服务_优质项目实战
资源推荐
资源详情
资源评论
收起资源包目录
大模型应用_大语言模型高效转发服务_优质项目实战.zip (82个子文件)
大模型应用_大语言模型高效转发服务_优质项目实战
deploy.md 4KB
Examples
embedding.py 400B
gemini_pro.py 577B
benchmark
.env 2KB
post.lua 267B
run.py 2KB
.gitignore 25B
images
img_16.png 12KB
img_3.png 14KB
img_6.png 14KB
img_14.png 15KB
img_8.png 15KB
img_12.png 15KB
img_17.png 15KB
img_13.png 14KB
img_4.png 16KB
img_9.png 15KB
img_7.png 15KB
img_5.png 15KB
img_11.png 15KB
img.png 15KB
img_1.png 15KB
img_10.png 14KB
img_2.png 15KB
img_15.png 14KB
README.md 2KB
tts.py 432B
config.example.yaml 54B
chat.sh 248B
.gitignore 27B
beta.py 505B
demo.py 2KB
chat_completion.py 4KB
whisper.py 343B
completion.py 1KB
webui.Dockerfile 865B
render.yaml 98B
.env 2KB
pytest.ini 343B
.env.example 2KB
Makefile 2KB
docker-compose.yaml 469B
openai_forward
forward
__init__.py 2KB
core.py 21KB
__init__.py 91B
helper.py 9KB
app.py 2KB
webui
__init__.py 0B
helper.py 486B
interface.py 7KB
chat.py 1KB
run.py 15KB
cache
__init__.py 3KB
embedding
__init__.py 0B
response.py 1KB
chat
__init__.py 0B
tokenizer.py 2KB
response.py 4KB
chat_completions.py 11KB
database.py 654B
custom_slowapi.py 2KB
__main__.py 7KB
console.py 3KB
decorators.py 7KB
settings.py 6KB
content
__init__.py 0B
helper.py 1KB
openai.py 14KB
config.py 4KB
tests
test_settings.py 917B
test_forwarding.py 3KB
.gitignore 44B
conftest.py 85B
.pre-commit-config.yaml 351B
deploy_en.md 5KB
Dockerfile 792B
pyproject.toml 2KB
.dockerignore 56B
README.md 7KB
scripts
keep_render_alive.py 465B
ai-forward-monitor.sh 1KB
black.sh 412B
共 82 条
- 1
资源评论
极智视界
- 粉丝: 2w+
- 资源: 1407
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功