Skip to main content

Hexin Proxy Server

一个 FastAPI 服务器,提供 OpenAI 和 Anthropic 兼容的 API 接口,通过代理 Hexin 后端服务来提供 AI 功能。

功能特性

  • Chat Completions API: 兼容 OpenAI 的聊天完成接口
  • Claude Messages API: 兼容 Anthropic Messages API,支持 Claude Code 和 Anthropic SDK
  • Responses API: 兼容 OpenAI 的推理响应接口 (支持 o3、o4-mini)
  • Embeddings API: 兼容 OpenAI 的文本嵌入接口
  • 模型列表: 支持列出可用的 AI 模型
  • 流式响应: 支持实时流式聊天响应和推理响应
  • 多模型支持: 支持多种大语言模型和嵌入模型

支持的接口

Chat Completions (OpenAI 格式)

  • POST /v1/chat/completions - 创建聊天完成
  • 支持流式和非流式响应
  • 支持工具调用和函数调用
  • 支持多种模型:GPT、Claude、Gemini、DeepSeek 等
  • 默认端口: 8777

Claude Messages API (Anthropic 格式)

  • POST /v1/messages - 创建消息(完全兼容 Anthropic SDK)
  • 支持流式和非流式响应
  • 支持多轮对话和 system 提示词
  • 可直接与 Claude Code 和 Anthropic SDK 集成
  • 默认端口: 8777
  • 📖 详细文档

Responses (推理响应)

  • POST /v1/responses - 创建推理响应 (专为 o3、o4-mini 等推理模型设计)
  • 支持流式和非流式响应
  • 支持推理配置 (effort: low/medium/high, summary: brief/detailed)
  • 返回详细的推理过程和结果

Embeddings

  • POST /v1/embeddings - 创建文本嵌入
  • 支持的模型:text-embedding-ada-002, text-embedding-3-small, text-embedding-3-large
  • 支持单个和批量文本处理

Models

  • GET /v1/models - 列出可用模型
  • 返回聊天、推理和嵌入模型列表

快速开始

1. 安装依赖

pip install hexin-server --upgrade

或者本地安装

git clone https://github.com/LinXueyuanStdio/hexin-proxy-server.git
cd hexin-proxy-server
pip install -e .

2. 配置环境变量

cp .env.example .env

创建 .env 文件:

HITHINK_APP_ID=your_app_id
HITHINK_APP_SECRET=your_app_secret
HITHINK_APP_URL=your_app_url

3. 启动服务器

OpenAI 兼容服务器(默认端口 8777)

# 直接运行
python -m hexin_server

# 或者指定参数
python -m hexin_server --host 0.0.0.0 --port 8777 --reload

4. 测试接口

Chat Completions 示例(OpenAI 格式)

curl -X POST "http://localhost:8777/v1/chat/completions" \
  -H "Authorization: Bearer sk-fastapi-proxy-key-12345" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-4o",
    "messages": [
      {"role": "user", "content": "Hello, how are you?"}
    ]
  }'

Claude Messages 示例(Anthropic 格式)

curl -X POST "http://localhost:8777/v1/messages" \
  -H "x-api-key: sk-fastapi-proxy-key-12345" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "claude-3-sonnet-20240229",
    "max_tokens": 1024,
    "messages": [
      {"role": "user", "content": "你好,请介绍一下你自己"}
    ]
  }'

Responses 推理示例

# 非流式推理响应
curl -X POST "http://localhost:8777/v1/responses" \
  -H "Authorization: Bearer sk-fastapi-proxy-key-12345" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "o3",
    "input": "估算下海水的总重量",
    "reasoning": {
      "effort": "medium",
      "summary": "detailed"
    }
  }'

# 流式推理响应
curl -X POST "http://localhost:8777/v1/responses" \
  -H "Authorization: Bearer sk-fastapi-proxy-key-12345" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "o3",
    "input": "估算下海水的总重量",
    "reasoning": {
      "effort": "medium",
      "summary": "detailed"
    },
    "stream": true
  }'

Embeddings 示例

curl -X POST "http://localhost:8777/v1/embeddings" \
  -H "Authorization: Bearer sk-fastapi-proxy-key-12345" \
  -H "Content-Type: application/json" \
  -d '{
    "input": "Hello, world!",
    "model": "text-embedding-ada-002"
  }'

使用 OpenAI 和 Anthropic 客户端库

OpenAI 格式 API

import openai

# 配置客户端
client = openai.OpenAI(
    api_key="sk-fastapi-proxy-key-12345",
    base_url="http://localhost:8777/v1"
)

# 聊天完成
response = client.chat.completions.create(
    model="gpt-4o",
    messages=[
        {"role": "user", "content": "Hello, how are you?"}
    ]
)

# 创建嵌入
embeddings = client.embeddings.create(
    model="text-embedding-ada-002",
    input="Hello, world!"
)

Anthropic Messages API

from anthropic import Anthropic

# 配置客户端
client = Anthropic(
    base_url="http://localhost:8777",
    api_key="sk-fastapi-proxy-key-12345",
)

# 创建消息
message = client.messages.create(
    model="claude-3-sonnet-20240229",
    max_tokens=1024,
    messages=[
        {"role": "user", "content": "你好,请介绍一下你自己。"}
    ]
)

print(message.content[0].text)

# 流式响应
with client.messages.stream(
    model="claude-3-sonnet-20240229",
    max_tokens=1024,
    messages=[
        {"role": "user", "content": "讲一个故事"}
    ]
) as stream:
    for text in stream.text_stream:
        print(text, end="", flush=True)

推理响应 API

推理响应需要使用 requests 库,因为 OpenAI 客户端暂不支持 responses API:

import requests

response = requests.post(
    "http://localhost:8777/v1/responses",
    headers={
        "Authorization": "Bearer sk-fastapi-proxy-key-12345",
        "Content-Type": "application/json"
    },
    json={
        "model": "o3",
        "input": "估算下海水的总重量",
        "reasoning": {
            "effort": "medium",
            "summary": "detailed"
        }
    }
)

详细文档

项目结构

hexin-proxy-server/
├── hexin_server/
│   ├── __init__.py
│   └── __main__.py          # 统一服务器 (端口 8777,支持 OpenAI 和 Anthropic API)
├── tests/
│   ├── test_embedding.py    # 嵌入接口测试
│   └── test_anthropic_sdk.py # Anthropic SDK 测试
├── CLAUDE_MESSAGES_API.md   # Claude API 使用指南
├── RESPONSES_API.md         # 推理 API 使用指南
├── EMBEDDING_API.md         # 嵌入 API 使用指南
├── README.md                # 项目总览
├── pyproject.toml           # 项目配置
└── .env.example             # 环境变量示例

测试

项目包含多种测试脚本来验证功能:

# 测试嵌入接口
python tests/test_embedding.py

# 测试 Anthropic SDK(Claude Messages API)
python tests/test_anthropic_sdk.py

所有测试都使用统一的 8777 端口服务器。

健康检查

检查服务器状态:

curl http://localhost:8777/health

响应:

{
  "status": "healthy",
  "authenticated": true
}

贡献

欢迎提交 Issue 和 Pull Request!

许可证

License file

Metadata

Release files for hexin-server 0.1.35

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for hexin-server 0.1.35
File Size Uploaded
hexin_server-0.1.35.tar.gz 31.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for hexin-server 0.1.35
File Interpreter ABI Platform
hexin_server-0.1.35-py3-none-any.whl Python 3 none any Details

Total release size: 62.4 kB

Release files / hexin_server-0.1.35.tar.gz

Download URL hexin_server-0.1.35.tar.gz
Size 31.6 kB
Tags Source
SHA-256 checksum
How to use checksums
597bce6ad81df9c91b83a56712d383203b9d7a03fb7174c7e77a846df63e59e3
BLAKE2b-256 checksum
How to use checksums
29bb8a9ac4feec0446aabfe4250ec813742048e9716e0fd4c29e7e33e36dbfe0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.2.1 CPython/3.11.5 Darwin/25.3.0

Release files / hexin_server-0.1.35-py3-none-any.whl

Download URL hexin_server-0.1.35-py3-none-any.whl
Size 30.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
13b16ab502595e9e89ff452ab2f49fea25147586061418580fb9a334428116c1
BLAKE2b-256 checksum
How to use checksums
d861e16c66f7f2c38e1e681e06d3a0faef745c2270ce3fb8748b8d2a9f98fda6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.2.1 CPython/3.11.5 Darwin/25.3.0
Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page