OpenRouter
使用 OpenRouter 和 Lingo.dev Compiler 进行 AI 翻译
什么是 OpenRouter?
OpenRouter 是一个平台,通过统一的 API 提供来自不同提供商的多种 AI 模型的访问权限,使开发者能够轻松比较、切换和路由请求。它专注于灵活性、透明性,并赋予用户更多控制权,决定使用哪些模型以及如何计费。
设置 API 密钥
OpenRouter 需要一个 API 密钥来验证来自 Lingo.dev Compiler 的请求。选择最适合您工作流程的设置方法:
选项 1:环境变量(终端)
适合快速实验和测试,无需修改任何文件。
在终端会话中直接设置密钥:
export OPENROUTER_API_KEY="your-api-key-here"
此密钥仅在关闭终端窗口之前有效。
选项 2:项目配置(.env 文件)
适合项目特定配置和团队环境,每个项目可能使用不同的凭据。
在项目根目录创建一个 .env
文件:
touch .env
在文件中添加以下内容:
OPENROUTER_API_KEY="your-api-key-here"
Lingo.dev Compiler 按以下优先顺序检查环境文件:
.env.development
(最高优先级).env.local
.env
(最低优先级)
优先级较高的文件中的值会覆盖优先级较低文件中的值。
选项 3:全局配置(用户设置)
适合希望在所有项目中使用相同 API 密钥的个人开发者。
在主目录中创建一个配置文件:
touch ~/.lingodotdevrc
在文件中添加以下内容:
[llm]
openrouterApiKey="your-api-key-here"
此配置在您的所有终端会话和机器上的项目中持续有效。
配置优先级
当使用多种配置方法时,Lingo.dev Compiler 按以下顺序检查 API 密钥:
- 环境变量(最高优先级)
- 项目 .env 文件(按其自身的优先顺序)
- 用户配置文件
~/.lingodotdevrc
(最低优先级)
使用找到的第一个有效 API 密钥。
使用 OpenRouter
要启用 OpenRouter,请在编译器选项中设置 models
属性:
import react from "@vitejs/plugin-react";
import lingoCompiler from "lingo.dev/compiler";
import { type UserConfig } from "vite";
// https://vite.dev/config/
const viteConfig: UserConfig = {
plugins: [react()],
};
const withLingo = lingoCompiler.vite({
sourceRoot: "src",
lingoDir: "lingo",
sourceLocale: "en",
targetLocales: ["es", "fr", "de", "ja"],
rsc: false,
useDirective: false,
debug: false,
models: {
"*:*": "openrouter:anthropic/claude-3.5-sonnet",
},
});
export default withLingo(viteConfig);
该属性接受一个对象,其中:
- 键是源语言和目标语言的配对,
*
表示任意语言 - 值是模型标识符(例如,
openrouter:anthropic/claude-3.5-sonnet
)
您可以使用 OpenRouter 进行以下翻译:
- 在所有语言之间
- 从特定的源语言
- 到特定的目标语言
- 在特定的源语言和目标语言之间
翻译所有语言
使用通配符模式 *:*
为所有翻译对应用相同的 OpenRouter 模型:
const withLingo = lingoCompiler.vite({
sourceRoot: "src",
lingoDir: "lingo",
sourceLocale: "en",
targetLocales: ["es", "fr", "de", "ja", "pt", "zh"],
models: {
// 为所有翻译对使用 Claude 3.5 Sonnet
"*:*": "openrouter:anthropic/claude-3.5-sonnet",
},
});
从特定的源语言进行翻译
使用特定的源语言和通配符目标语言,为从该源语言的所有翻译应用一个模型:
const withLingo = lingoCompiler.vite({
sourceRoot: "src",
lingoDir: "lingo",
sourceLocale: "en",
targetLocales: ["es", "fr", "de", "ja", "pt", "zh"],
models: {
// 为所有从英语翻译的内容使用 Claude 3.5 Sonnet
"en:*": "openrouter:anthropic/claude-3.5-sonnet",
// 为从西班牙语翻译到任何语言的内容使用 GPT-4 Turbo
"es:*": "openrouter:openai/gpt-4-turbo",
// 其他源语言的回退选项
"*:*": "openrouter:meta-llama/llama-3.1-70b",
},
});
翻译为特定目标语言区域
使用通配符源语言和特定目标语言区域,为所有翻译到该目标语言的内容应用模型:
const withLingo = lingoCompiler.vite({
sourceRoot: "src",
lingoDir: "lingo",
sourceLocale: "en",
targetLocales: ["es", "fr", "de", "ja", "pt", "zh"],
models: {
// 为翻译到日语使用专用模型
"*:ja": "openrouter:anthropic/claude-3-opus",
// 为翻译到中文使用 Gemini Pro
"*:zh": "openrouter:google/gemini-pro-1.5",
// 为翻译到法语使用 Mistral Large
"*:fr": "openrouter:mistralai/mistral-large",
// 其他目标语言的默认模型
"*:*": "openrouter:openai/gpt-3.5-turbo",
},
});
在特定语言区域之间翻译
定义精确的源语言-目标语言对,以便对特定语言组合使用最佳模型进行细粒度控制:
const withLingo = lingoCompiler.vite({
sourceRoot: "src",
lingoDir: "lingo",
sourceLocale: "en",
targetLocales: ["es", "fr", "de", "ja", "pt", "zh"],
models: {
// 使用最佳模型的特定语言对
"en:es": "openrouter:openai/gpt-3.5-turbo", // 英语到西班牙语
"en:ja": "openrouter:anthropic/claude-3.5-sonnet", // 英语到日语
"en:zh": "openrouter:google/gemini-pro-1.5", // 英语到中文
"en:fr": "openrouter:mistralai/mistral-large", // 英语到法语
"es:en": "openrouter:meta-llama/llama-3.1-8b", // 西班牙语到英语
"fr:en": "openrouter:cohere/command-r-plus", // 法语到英语
"de:en": "openrouter:perplexity/llama-3.1-sonar-large", // 德语到英语
// 未指定语言对的回退模型
"*:*": "openrouter:openai/gpt-3.5-turbo",
},
});