Mistral AI

使用 Mistral 和 Lingo.dev Compiler 的 AI 翻译

什么是 Mistral AI?

Mistral AI 是一家法国人工智能公司,专注于开发开放权重的大型语言模型,旨在实现高效、可定制,并适用于广泛的应用场景。他们注重透明性和性能,发布的模型相比许多竞争对手可以在较小的硬件上高效运行。

设置 API 密钥

Mistral AI 需要一个 API 密钥来验证来自 Lingo.dev Compiler 的请求。选择最适合您工作流程的设置方法:

选项 1:环境变量(终端)

适用于快速实验和测试,无需修改任何文件。

在终端会话中直接设置密钥:

export MISTRAL_API_KEY="your-api-key-here"

此密钥仅在您关闭终端窗口之前可用。

选项 2:项目配置(.env 文件)

适用于项目特定的配置和团队环境,每个项目可能使用不同的凭据。

在项目根目录创建一个 .env 文件:

touch .env

将以下内容添加到文件中:

MISTRAL_API_KEY="your-api-key-here"

Lingo.dev Compiler 按以下优先顺序检查环境文件:

  1. .env.development(最高优先级)
  2. .env.local
  3. .env(最低优先级)

优先级较高的文件中的值会覆盖优先级较低文件中的值。

选项 3:全局配置(用户设置)

适用于希望在所有项目中使用相同 API 密钥的个人开发者。

在您的主目录中创建一个配置文件:

touch ~/.lingodotdevrc

将以下内容添加到文件中:

[llm]
mistralApiKey="your-api-key-here"

此配置在您的所有终端会话和项目中持久有效。

配置优先级

当使用多种配置方法时,Lingo.dev Compiler 按以下顺序检查 API 密钥:

  1. 环境变量(最高优先级)
  2. 项目 .env 文件(按其自身的优先顺序)
  3. 用户配置文件 ~/.lingodotdevrc(最低优先级)

使用找到的第一个有效 API 密钥。

使用 Mistral AI

要启用 Mistral 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: {
    "*:*": "mistral:mistral-large-latest",
  },
});

export default withLingo(viteConfig);

该属性接受一个对象,其中:

  • 键是源语言和目标语言的配对,* 表示任意语言
  • 值是模型标识符(例如,mistral:mistral-large-latest

您可以使用 Mistral AI 进行以下翻译:

  • 在所有语言之间
  • 从特定的源语言
  • 到特定的目标语言
  • 在特定的源语言和目标语言之间

翻译所有语言

使用通配符模式 *:* 为所有翻译对应用相同的 Mistral 模型:

const withLingo = lingoCompiler.vite({
  sourceRoot: "src",
  lingoDir: "lingo",
  sourceLocale: "en",
  targetLocales: ["es", "fr", "de", "ja", "pt", "zh"],
  models: {
    // 为所有翻译对使用 Mistral Large
    "*:*": "mistral:mistral-large-latest",
  },
});

从特定的源语言进行翻译

使用特定的源语言和通配符目标语言,为从该源语言的所有翻译应用模型:

const withLingo = lingoCompiler.vite({
  sourceRoot: "src",
  lingoDir: "lingo",
  sourceLocale: "en",
  targetLocales: ["es", "fr", "de", "ja", "pt", "zh"],
  models: {
    // 为所有从英语的翻译使用 Mistral Large
    "en:*": "mistral:mistral-large-latest",
    // 为从西班牙语到任意语言的翻译使用 Mixtral
    "es:*": "mistral:mixtral-8x7b",
    // 其他源语言的回退选项
    "*:*": "mistral:mistral-small-latest",
  },
});

翻译为特定目标语言区域

使用通配符源语言和特定目标语言区域,为所有翻译到该目标语言的内容应用模型:

const withLingo = lingoCompiler.vite({
  sourceRoot: "src",
  lingoDir: "lingo",
  sourceLocale: "en",
  targetLocales: ["es", "fr", "de", "ja", "pt", "zh"],
  models: {
    // 为翻译成日语使用专门模型
    "*:ja": "mistral:mistral-large-latest",
    // 为翻译成中文使用 Mixtral
    "*:zh": "mistral:mixtral-8x22b",
    // 为翻译成法语使用 Mistral Large(Mistral 的强项)
    "*:fr": "mistral:mistral-large-latest",
    // 其他目标语言的默认模型
    "*:*": "mistral:mistral-small-latest",
  },
});

在特定语言区域之间翻译

定义精确的源语言-目标语言对,以便对特定语言组合使用最佳模型进行细粒度控制:

const withLingo = lingoCompiler.vite({
  sourceRoot: "src",
  lingoDir: "lingo",
  sourceLocale: "en",
  targetLocales: ["es", "fr", "de", "ja", "pt", "zh"],
  models: {
    // 使用最佳模型的特定语言对
    "en:es": "mistral:mistral-small-latest", // 英语到西班牙语
    "en:ja": "mistral:mistral-large-latest", // 英语到日语
    "en:zh": "mistral:mixtral-8x22b", // 英语到中文
    "en:fr": "mistral:mistral-large-latest", // 英语到法语(卓越)
    "es:en": "mistral:mistral-nemo", // 西班牙语到英语
    "fr:en": "mistral:mistral-medium-latest", // 法语到英语
    "de:en": "mistral:codestral", // 德语到英语(技术)

    // 未指定语言对的回退模型
    "*:*": "mistral:mistral-small-latest",
  },
});