Skip to content

Repository files navigation

Agent Proxy

English | 中文

极简 LLM API 代理,翻译 API Key 并转发请求到上游 LLM 服务商。

兼容所有 OpenAI 接口规范的服务(OpenAI、DeepSeek、Ollama、华为云 TokenHub 等)。

特性

  • AKSK 配置:配置华为云永久 AK/SK,自动从 TokenHub 获取模型配置
  • Key 翻译:本地 Key 映射到上游真实 API Key
  • URL 重写:根据上游 base_url 自动推断路径重写规则
  • OpenAI 兼容 /v1/models:本地模型列表接口,遵循 OpenAI API 规范
  • 流式支持:完整 SSE(Server-Sent Events)透传
  • 文件配置:YAML 配置文件,无需数据库
  • 全透传:支持所有 /v1/* 端点(对话、嵌入、图像、音频)

快速开始

1. 编译

go build -o agent-proxy .

2. 配置

编辑 config.yaml — 只需填写华为云永久 AK/SK:

server:
  port: 8080

huawei:
  access_key: "AKxxxxxxxx"
  secret_key: "SKxxxxxxxx"
  # security_token: ""     # 可选,临时凭证时填写
  # domain: ""             # 可选,默认 devstation.myhuaweicloud.com

启动时,代理调用 TokenHub API 获取模型配置(api_key、base_url、models)并打印:

╔════════════════════════════════════════════════════════════════════╗
║           TokenHub Model Configuration Loaded                      ║
╠════════════════════════════════════════════════════════════════════╣
║  API Key:  AAAAAgAA...
║  Base URL: https://tokenhub.developer.huaweicloud.com/v2
╠════════════════════════════════════════════════════════════════════╣
║  Available Models:
║  1. glm-5.2 (GLM-5.2) [ctx: 1980000, max_tokens: 128000]
║  2. deepseek-v3.2 (DeepSeek-V3.2) [ctx: 1310720, max_tokens: 262144]
║  ...
╚════════════════════════════════════════════════════════════════════╝

3. 运行

./agent-proxy
# 或指定配置文件:
./agent-proxy -config /path/to/config.yaml

4. 使用

# 列出模型(OpenAI 兼容)
curl http://localhost:8080/v1/models \
  -H "Authorization: Bearer sk-local"

# 对话补全
curl http://localhost:8080/v1/chat/completions \
  -H "Authorization: Bearer sk-local" \
  -H "Content-Type: application/json" \
  -d '{"model":"glm-5.2","messages":[{"role":"user","content":"你好"}]}'

5. 接入 AI SDK

将 SDK 的 baseURL 配置为 http://localhost:8080/v1,使用本地 Key sk-local

{
  "baseURL": "http://localhost:8080/v1",
  "apiKey": "sk-local"
}

配置说明

AKSK 模式(推荐)

字段 说明
server.port 监听端口(默认 8080)
huawei.access_key 华为云永久 Access Key
huawei.secret_key 华为云永久 Secret Key
huawei.security_token (可选)临时凭证的 Security Token
huawei.domain (可选)TokenHub 域名,默认 devstation.myhuaweicloud.com,devstation.myhuaweicloud.cn
huawei.strip_prefix (可选)覆盖自动推断的路径剥离(如 /v1
huawei.rewrite_prefix (可选)覆盖自动推断的路径重写(如 /v2

AKSK 模式下,代理自动生成本地 Key sk-local,并自动推断路径重写:由于 TokenHub 返回的 base_url 已包含 /v2,客户端的 /v1 前缀会被自动剥离。

手动模式(后备)

也可以不使用 TokenHub,手动配置 Key 映射:

server:
  port: 8080

keys:
  - key: sk-local-key-1
    upstream_url: https://api.deepseek.com
    upstream_key: sk-your-real-key

  - key: sk-local-key-2
    upstream_url: https://api.openai.com
    upstream_key: sk-your-openai-key
    strip_prefix: /v1
    rewrite_prefix: /v2
字段 说明
keys[].key 客户端使用的本地 API Key
keys[].upstream_url 上游 LLM API 地址
keys[].upstream_key 上游服务商的真实 API Key
keys[].strip_prefix (可选)转发前剥离的路径前缀
keys[].rewrite_prefix (可选)替换的路径前缀

API 端点

方法 路径 说明
GET /v1/models 列出所有可用模型(OpenAI 兼容,本地响应)
GET /v1/models/{id} 获取指定模型(OpenAI 兼容,本地响应)
POST /v1/chat/completions 对话补全(转发到上游)
* /v1/* 其他 OpenAI 端点(转发到上游)

工作原理

客户端                          agent-proxy                  上游 LLM
  │                                 │                              │
  │  POST /v1/chat/completions      │                              │
  │  Authorization: Bearer sk-local │                              │
  │  ─────────────────────────────>│                              │
  │                                 │                              │
  │                                 │  1. 查表 sk-local            │
  │                                 │     → upstream_url + key     │
  │                                 │  2. 剥离 /v1 前缀            │
  │                                 │                              │
  │                                 │  POST base_url/              │
  │                                 │   chat/completions           │
  │                                 │  Authorization: Bearer real  │
  │                                 │  ──────────────────────────>│
  │                                 │                              │
  │                                 │  <──────────────────────────│
  │  <─────────────────────────────│    (透传响应)                  │
  │                                 │                              │

通过 hdspace 远程部署

在能访问华为云的远程机器上部署 agent-proxy,通过端口映射在本地使用:

┌─────────┐  端口映射         ┌───────────────┐    TokenHub API   ┌──────────┐
│  本地    │  localhost:8081 ─▶│  agent-proxy   │  AK/SK 签名      │  华为云  │
│  客户端  │◀── (隧道)        │  remote:8080   │◀─────────────────│ TokenHub │
└─────────┘                   └───────────────┘                    └──────────┘

远程 — 使用 AKSK 配置部署:

server:
  port: 8080

huawei:
  access_key: "AKxxxxxxxx"
  secret_key: "SKxxxxxxxx"

本地 — 将 localhost:8081 映射到 remote:8080

客户端 — 通过映射端口连接:

{
  "baseURL": "http://localhost:8081/v1",
  "apiKey": "sk-local"
}

许可证

MIT

About

A minimal LLM API proxy that translates API keys and forwards requests to upstream LLM providers.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages