首页
看点啥
插画图片
首页 科技看点 openai-forward:AI Agent 工具实践指南

openai-forward:AI Agent 工具实践指南

2026-09-11 0

实际评估openai-forward时,我先确认它解决的具体问题: 大语言模型高效转发服务 · An efficient forwarding service designed for LLMs. · OpenAI API等相关能力。实际做软件开发时,经常会碰到依赖、接口和异常处理往往比主路径更影响采用,所以功能列表并不能代替验证。落地前可以在隔离分支完成一个可回滚的小任务,用安装步骤、接口契约、测试结果和错误信息判断它是否真的省事。它对需要可检查开发流程而非单次演示的工程师更有价值,但正式采用前仍要复查许可证、近期提交和问题区回应。

简体中文 | English

OpenAI Forward

特点 | 部署指南 | 使用指南 | 配置 | 对话日志

[!IMPORTANT]

在v0.7.0以后在配置方面会有较大调整,并与之前版本不兼容。通过UI配置起来会更加方便,且提供了更强大的配置选项。

OpenAI-Forward 是为大型语言模型实现的高效转发服务。其核心功能包括 用户请求速率控制、Token速率限制、智能预测缓存、日志管理和API密钥管理等,旨在提供高效、便捷的模型转发服务。 无论是代理本地语言模型还是云端语言模型,如 LocalAI 或 OpenAI,都可以由 OpenAI Forward 轻松实现。 得益于 uvicorn, aiohttp, 和 asyncio 等库支持,OpenAI-Forward 实现了出色的异步性能。

News

主要特性

由本项目搭建的代理服务地址:

注:此处部署的代理服务仅供个人学习和研究目的使用,勿用于任何商业用途。

部署指南

部署文档

使用指南

快速入门

安装

pip install openai-forward 

# 或安装webui版本:
pip install openai-forward[webui]

启动服务

aifd run
# 或启动带webui的服务
aifd run --webui

如果读入了根路径的.env的配置, 将会看到以下启动信息

❯ 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

  from openai import OpenAI  # pip install openai>=1.0.0
  client = OpenAI(
+     base_url="https://api.openai-forward.com/v1", 
      api_key="sk-******"
  )

代理本地模型

(更多)

代理任意云端模型

代理gemini pro

配置环境变量或 .env 文件如下:

FORWARD_CONFIG=[{"base_url":"https://generativelanguage.googleapis.com","route":"/gemini","type":"general"}]

说明:aidf run启动后,即可通过访问 http://localhost:8000/gemini 使用gemini pro。

(更多)

配置

执行 aifd run --webui 进入配置页面 (默认服务地址 http://localhost:8001)

你可以在项目的运行目录下创建 .env 文件来定制各项配置。参考配置可见根目录下的 .env.example文件

智能缓存

开启缓存后,将会对指定路由的内容进行缓存,其中转发类型分别为openaigeneral两者行为略有不同, 使用general转发时,默认会将相同的请求一律使用缓存返回,
使用openai转发时,在开启缓存后,可以通过OpenAI 的extra_body参数来控制缓存的行为,如

Python

  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

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
  }'

自定义秘钥

Click for more details

见.env文件

用例:

  import openai
+ openai.api_base = "https://api.openai-forward.com/v1"
- openai.api_key = "sk-******"
+ openai.api_key = "fk-******"

多目标服务转发

支持转发不同地址的服务至同一端口的不同路由下 用例见 .env.example

对话日志

Click for more details

保存路径在当前目录下的Log/openai/chat/chat.log路径中。
记录格式为

{'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'}
{'助理':'您好!今天我能为您提供什么帮助?', 'is_tool_calls': False, 'uid': '2155fe1580e6aed626aa1ad74c1ce54e'}

转换为json格式:

aifd convert

得到chat_openai.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?"
    }
]
喜欢(0)

上一篇

yellowstone-grpc:实践指南

yellowstone-grpc:实践指南

下一篇

Lumina-Layers:实践指南

Lumina-Layers:实践指南
猜你喜欢