使用说明
项目简介
AI Gateway (原名 LangDB AI Gateway) 是一个用 Rust 构建的高性能开源企业级 AI 网关。它旨在为各种 LLM 应用提供统一的接口,支持 OpenAI API 格式,并能无缝集成 MCP 服务器,从而实现对 AI 流量的治理、安全和优化。
主要功能点
- 高性能: 基于 Rust 构建,性能和可靠性高。
- 企业级特性:
- 提供全面的使用分析和成本跟踪。
- 支持速率限制和成本控制。
- 具备高级路由、负载均衡和故障转移能力。
- 数据控制: 完全拥有 LLM 使用数据,提供详细的日志记录和追踪。
- MCP 支持: 可以与 MCP 服务器集成,利用 MCP 服务器提供的资源和工具扩展 LLM 应用的能力。
- 兼容 OpenAI API: 提供 OpenAI 兼容的 API 端点,方便现有 LLM 应用迁移。
- 支持多种 LLM 提供商: 支持 OpenAI, Google Gemini, Anthropic, DeepSeek 等主流 LLM 提供商。
安装步骤
-
使用 Docker (推荐)
docker run -it \ -p 8080:8080 \ -e LANGDB_KEY=your-langdb-key-here \ langdb/ai-gateway serve请将 'your-langdb-key-here' 替换为您的 LangDB API 密钥(如果需要)。
-
使用 Cargo
确保您已安装 Rust 和 Cargo。
export RUSTFLAGS="--cfg tracing_unstable --cfg aws_sdk_unstable" cargo install ai-gateway export LANGDB_KEY=your-langdb-key-here ai-gateway serve同样,请将 'your-langdb-key-here' 替换为您的 LangDB API 密钥(如果需要)。
服务器配置
AI Gateway 主要通过命令行参数和 'config.yaml' 文件进行配置。以下是一个基础的 'config.yaml' 示例,展示了如何配置 OpenAI 和 Anthropic 的 API 密钥:
providers: openai: api_key: "your-openai-key-here" anthropic: api_key: "your-anthropic-key-here"
MCP 客户端配置
要将 AI Gateway 作为 MCP 服务器配置到 MCP 客户端,您需要提供以下信息:
{ "serverName": "ai-gateway-mcp-server", "command": "ai-gateway", "args": ["serve"], "transport": "stdio", "capabilities": [ "tools", "prompts", "resources" ] }
- 'serverName': MCP 服务器的名称,可以自定义,例如 "ai-gateway-mcp-server"。
- 'command': 启动 AI Gateway 服务器的命令,通常为 'ai-gateway'。
- 'args': 启动命令的参数,这里使用 '["serve"]' 即可启动服务。您可以根据需要添加其他命令行参数,例如 '--host 0.0.0.0 --port 8080' 等。
- 'transport': 指定客户端与服务器通信的传输协议,这里使用 'stdio' (标准输入输出)。AI Gateway 也支持 'sse' 和 'ws',您可以根据客户端的需求选择。
- 'capabilities': 声明 MCP 服务器提供的能力,AI Gateway 作为 MCP 服务器通常可以提供 'tools', 'prompts', 'resources' 等能力。
基本使用方法
-
启动 AI Gateway 服务器 (根据上述安装步骤选择 Docker 或 Cargo 方式启动)。
-
使用 curl 测试 API 接口
例如,使用 curl 调用 Chat Completion 接口:
curl http://localhost:8080/v1/chat/completions \ -H "Content-Type: application/json" \ -d '{ "model": "gpt-4o-mini", "messages": [{"role": "user", "content": "What is the capital of France?"}] }' -
集成 MCP 工具
以下是如何在 Chat Completion 请求中使用 MCP 工具的示例:
curl http://localhost:8080/v1/chat/completions \ -H "Content-Type: application/json" \ -d '{ "model": "gpt-4o-mini", "messages": [{"role": "user", "content": "Ping the server using the tool and return the response"}], "mcp_servers": [{"server_url": "http://localhost:3004"}] }'请确保您已配置并运行了 'http://localhost:3004' 上的 MCP 服务器。
更多高级配置和使用方法,请参考仓库的 官方文档。
信息
分类
网页与API