OpenRouter 集成与实践

OpenRouter 集成与实践

Xcode

1 分钟阅读

Xcode

在 Xcode 的 Apple Intelligence 中使用 OpenRouter

将 Xcode 与 Apple Intelligence 配合使用

Xcode 26 中的 Apple Intelligence 为编程提供内置 AI 协助。通过集成 OpenRouter,你可以直接在 Xcode 开发环境中访问数百个 AI 模型,远超默认的 ChatGPT 集成。

此集成让你无需离开开发环境,即可使用来自 Anthropic、Google、Meta 以及许多其他提供商的模型。

前提条件

Xcode 上的 Apple Intelligence 目前处于 Beta,并要求:

设置说明

第 1 步:启用 Apple Intelligence

前往 macOS Settings > Apple Intelligence & Siri,并确保 Apple Intelligence 已开启。这是 Xcode 使用任何 AI 模型提供商之前的必要步骤。

Apple Intelligence 与 Siri 设置

第 2 步:打开 Xcode Intelligence 设置

打开 Xcode > Settings > Intelligence,并点击页面底部的 Add a Chat Provider(添加聊天提供商)。

Xcode Intelligence 设置

第 3 步:配置 OpenRouter 提供商

在出现的对话框中,输入以下详细信息:

  • URLhttps://openrouter.ai/api
    • 重要:不要像直接调用 API 时通常那样在端点末尾添加 /v1
  • API Key HeaderAuthorization
  • API KeyBearer YOUR_API_KEY_HERE(将 YOUR_API_KEY_HERE 替换为以 sk-or-v1- 开头的 OpenRouter API 密钥)
  • DescriptionOpenRouter(或你喜欢的任何名称)

点击 Add(添加)以保存配置。

OpenRouter 配置

第 4 步:浏览可用模型

配置完成后,点击 OpenRouter 即可查看所有可用模型。由于 OpenRouter 提供数百个模型,你应为常用模型添加书签以便快速访问。已添加书签的模型会出现在列表顶部,需要时即可从面板中轻松选取。

可用模型

你可以访问来自各家提供商的模型,包括:

  • Anthropic Claude 模型
  • Google Gemini 模型
  • Meta Llama 模型
  • OpenAI GPT 模型
  • 以及另外数百个模型
扩展模型列表

第 5 步:开始在 Xcode 中使用 AI

回到聊天界面(顶部图标),直接在 Xcode 中与所选模型对话。

Xcode 聊天界面

使用 Apple Intelligence 功能

配置完成后,你可以在 Xcode 中将 Apple Intelligence 功能与 OpenRouter 模型配合使用:

  • 代码补全:获取智能代码建议
  • 代码解释:就代码提问
  • 重构协助:获得改进代码结构的帮助
  • 文档生成:生成注释和文档
Apple Intelligence 界面

图片来源:Apple Developer Documentation

了解更多