Skip to content

Latest commit

 

History

History
149 lines (111 loc) · 3.8 KB

File metadata and controls

149 lines (111 loc) · 3.8 KB

OpenRouter 集成说明

概述

本项目已成功集成了 OpenRouter API,支持多个免费的大语言模型。OpenRouter 是一个统一的 API 接口,可以访问多个不同的 AI 模型提供商。

支持的免费模型

以下模型已添加到 OpenRouter 配置中:

  1. deepseek/deepseek-r1-0528:free - DeepSeek R1 模型(免费版)
  2. deepseek/deepseek-chat-v3-0324:free - DeepSeek Chat V3 模型(免费版)
  3. openai/gpt-oss-20b:free - OpenAI GPT-OSS 20B 模型(免费版)
  4. z-ai/glm-4.5-air:free - GLM-4.5 Air 模型(免费版)
  5. moonshotai/kimi-k2:free - Kimi K2 模型(免费版)
  6. qwen/qwen3-235b-a22b:free - Qwen3 235B 模型(免费版)
  7. meta-llama/llama-3.3-70b-instruct:free - Llama 3.3 70B 模型(免费版)

使用方法

1. 获取 OpenRouter API 密钥

  1. 访问 OpenRouter 官网
  2. 注册账户并登录
  3. 在 API Keys 页面创建新的 API 密钥
  4. 复制生成的 API 密钥

2. 在游戏中配置

  1. 启动游戏
  2. 按 ESC 键打开设置界面
  3. 在 API 设置选项卡中:
    • 选择 "OpenRouter (免费模型)" 作为 API 类型
    • 选择你想要的免费模型
    • 输入你的 OpenRouter API 密钥
  4. 点击保存

3. 角色独立设置

每个角色都可以有独立的 AI 设置:

  1. 在角色设置中选择 "OpenRouter (免费模型)"
  2. 为角色选择特定的模型
  3. 输入 API 密钥(如果与全局设置不同)

技术实现

文件修改

  • script/ai/APIConfig.gd: 添加了 OpenRouter 配置
  • 在 API 类型枚举中添加了 OPENROUTER
  • 添加了 OpenRouter 提供商配置,包括所有免费模型

API 配置详情

# OpenRouter配置
_providers["OpenRouter"] = APIProvider.new(
    "OpenRouter",
    "OpenRouter (免费模型)",
    "https://openrouter.ai/api/v1/chat/completions",
    [
        "deepseek/deepseek-r1-0528:free",
        "deepseek/deepseek-chat-v3-0324:free",
        "openai/gpt-oss-20b:free",
        "z-ai/glm-4.5-air:free",
        "moonshotai/kimi-k2:free",
        "qwen/qwen3-235b-a22b:free",
        "meta-llama/llama-3.3-70b-instruct:free"
    ],
    true,  # 需要API密钥
    {
        "Content-Type": "application/json",
        "Authorization": "Bearer {api_key}",
        "HTTP-Referer": "https://github.com/your-repo",
        "X-Title": "Microverse Game"
    },
    "openai",  # 使用OpenAI格式的请求
    "openai"   # 使用OpenAI格式的响应解析
)

请求格式

OpenRouter 使用与 OpenAI 兼容的 API 格式:

{
  "model": "deepseek/deepseek-r1-0528:free",
  "messages": [
    {
      "role": "user",
      "content": "你的提示内容"
    }
  ]
}

响应格式

OpenRouter 返回标准的 OpenAI 格式响应:

{
  "choices": [
    {
      "message": {
        "content": "AI的回复内容"
      }
    }
  ]
}

注意事项

  1. API 密钥安全: 请妥善保管你的 OpenRouter API 密钥,不要分享给他人
  2. 使用限制: 免费模型可能有使用频率和数量限制
  3. 模型可用性: 免费模型的可用性可能会变化,建议定期检查
  4. 网络连接: 需要稳定的网络连接来访问 OpenRouter API

故障排除

常见问题

  1. API 密钥无效: 检查密钥是否正确复制,确保没有多余的空格
  2. 模型不可用: 尝试选择其他免费模型
  3. 网络错误: 检查网络连接,确保可以访问 openrouter.ai
  4. 请求超时: 某些模型可能需要更长的响应时间

调试信息

游戏会在控制台输出详细的调试信息,包括:

  • API 请求 URL
  • 请求头信息
  • 请求数据
  • 响应状态

更新日志

  • v1.0: 初始 OpenRouter 集成
    • 添加了 7 个免费模型支持
    • 实现了完整的 API 配置
    • 支持角色独立 AI 设置