Google AI
使用 Google Gemini 和 Lingo.dev Compiler 进行 AI 翻译
什么是 Google AI?
Google AI Studio 是一个基于网络的开发环境,用于构建、测试和部署由 Google 的生成式 AI 模型(如 Gemini)驱动的应用程序。它允许开发者在一个易于使用的界面中,通过连接 Google Cloud,实验提示、微调响应,并通过 API 将模型集成到应用程序中。
设置 API 密钥
Google AI 需要一个 API 密钥来验证来自 Lingo.dev Compiler 的请求。选择最适合您工作流程的设置方法:
选项 1:环境变量(终端)
适合快速实验和测试,无需修改任何文件。
在您的终端会话中直接设置密钥:
export GOOGLE_API_KEY="your-api-key-here"
此密钥仅在您关闭终端窗口之前可用。
选项 2:项目配置(.env)
适合项目特定的配置和团队环境,每个项目可能使用不同的凭据。
在项目根目录中创建一个 .env
文件:
touch .env
将以下内容添加到文件中:
GOOGLE_API_KEY="your-api-key-here"
Lingo.dev Compiler 按以下优先顺序检查环境文件:
.env.development
(最高优先级).env.local
.env
(最低优先级)
优先级较高的文件中的值会覆盖优先级较低文件中的值。
选项 3:全局配置(用户设置)
适合希望在所有项目中使用相同 API 密钥的个人开发者。
在您的主目录中创建一个配置文件:
touch ~/.lingodotdevrc
将以下内容添加到文件中:
[llm]
googleApiKey="your-api-key-here"
此配置在您的所有终端会话和机器上的项目中持续有效。
配置优先级
当使用多种配置方法时,Lingo.dev 编译器会按以下顺序检查 API 密钥:
- 环境变量(最高优先级)
- 项目 .env 文件(按其自身的优先级顺序)
- 用户配置文件
~/.lingodotdevrc
(最低优先级)
使用第一个找到的有效 API 密钥。
使用 Google AI
要启用 Google AI,请在编译器选项中设置 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: {
"*:*": "google:gemini-1.5-flash",
},
});
export default withLingo(viteConfig);
该属性接受一个对象,其中:
- 键是源语言和目标语言的组合,
*
表示任意语言 - 值是模型标识符(例如,
google:gemini-1.5-flash
)
您可以使用 Google AI 翻译:
- 所有语言之间
- 从特定的源语言
- 到特定的目标语言
- 从特定的源语言到特定的目标语言
翻译所有语言
使用通配符模式 *:*
为所有翻译对应用相同的 Google 模型:
const withLingo = lingoCompiler.vite({
sourceRoot: "src",
lingoDir: "lingo",
sourceLocale: "en",
targetLocales: ["es", "fr", "de", "ja", "pt", "zh"],
models: {
// 为所有翻译对使用 Gemini 1.5 Flash
"*:*": "google:gemini-1.5-flash",
},
});
从特定的源语言翻译
使用特定的源语言和通配符目标语言,为从该源语言的所有翻译应用模型:
const withLingo = lingoCompiler.vite({
sourceRoot: "src",
lingoDir: "lingo",
sourceLocale: "en",
targetLocales: ["es", "fr", "de", "ja", "pt", "zh"],
models: {
// 为所有从英语翻译的内容使用 Gemini 1.5 Pro
"en:*": "google:gemini-1.5-pro",
// 为从西班牙语翻译到任何语言的内容使用 Gemini 1.5 Flash
"es:*": "google:gemini-1.5-flash",
// 其他源语言的回退选项
"*:*": "google:gemini-1.5-flash-latest",
},
});
翻译为特定目标语言区域
使用通配符源语言和特定目标语言区域,为所有翻译到该目标语言的内容应用模型:
const withLingo = lingoCompiler.vite({
sourceRoot: "src",
lingoDir: "lingo",
sourceLocale: "en",
targetLocales: ["es", "fr", "de", "ja", "pt", "zh"],
models: {
// 为翻译到日语使用专用模型
"*:ja": "google:gemini-1.5-pro",
// 为翻译到中文使用 Gemini 1.5 Flash
"*:zh": "google:gemini-1.5-flash",
// 为翻译到德语使用 Gemini 1.5 Pro
"*:de": "google:gemini-1.5-pro-latest",
// 其他目标语言的默认设置
"*:*": "google:gemini-1.5-flash",
},
});
在特定语言区域之间翻译
定义精确的源语言-目标语言对,以便对特定语言组合使用最佳模型进行细粒度控制:
const withLingo = lingoCompiler.vite({
sourceRoot: "src",
lingoDir: "lingo",
sourceLocale: "en",
targetLocales: ["es", "fr", "de", "ja", "pt", "zh"],
models: {
// 使用最佳模型的特定语言对
"en:es": "google:gemini-1.5-flash", // 英语到西班牙语
"en:ja": "google:gemini-1.5-pro", // 英语到日语
"en:zh": "google:gemini-1.5-pro", // 英语到中文
"es:en": "google:gemini-1.5-flash", // 西班牙语到英语
"fr:en": "google:gemini-1.5-flash-latest", // 法语到英语
"de:en": "google:gemini-1.0-pro", // 德语到英语
// 未指定语言对的回退设置
"*:*": "google:gemini-1.5-flash",
},
});