Ollama

使用 Ollama 和 Lingo.dev Compiler 进行本地 AI 翻译

什么是 Ollama?

Ollama 允许您在本地机器上运行大型语言模型。它提供完全的隐私、零 API 成本,并且可以离线工作。您可以选择使用 Llama、Mistral、Gemma 和 Qwen 等模型。Ollama 与 Lingo.dev Compiler 配合使用,允许您使用本地模型翻译应用程序内容。

入门指南

第 1 步:安装 Ollama

macOS

brew install ollama

Linux

curl -fsSL https://ollama.com/install.sh | sh

Windows

  1. 访问 ollama.com/download
  2. 下载并运行安装程序。

第 2 步:启动 Ollama

如果 Ollama 尚未运行,请使用以下命令启动:

ollama serve

第 3 步:下载模型

ollama pull llama3.1

使用 Ollama

要启用 Ollama,请在编译器选项中设置 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: {
    "*:*": "ollama:llama3.1",
  },
});

export default withLingo(viteConfig);

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

  • 键是源语言和目标语言的组合,* 表示任意语言
  • 值是模型标识符(例如,ollama:llama3.1

您可以使用 Ollama 翻译:

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

翻译所有语言

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

const withLingo = lingoCompiler.vite({
  sourceRoot: "src",
  lingoDir: "lingo",
  sourceLocale: "en",
  targetLocales: ["es", "fr", "de", "ja", "pt", "zh"],
  models: {
    // 为所有翻译对使用 Llama 3.1
    "*:*": "ollama:llama3.1",
  },
});

从特定源语言进行翻译

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

const withLingo = lingoCompiler.vite({
  sourceRoot: "src",
  lingoDir: "lingo",
  sourceLocale: "en",
  targetLocales: ["es", "fr", "de", "ja", "pt", "zh"],
  models: {
    // 为所有从英语翻译的内容使用 Llama 3.1 70B
    "en:*": "ollama:llama3.1:70b",
    // 为从西班牙语翻译到任意语言的内容使用 Mixtral
    "es:*": "ollama:mixtral",
    // 其他源语言的回退模型
    "*:*": "ollama:gemma2",
  },
});

翻译到特定目标语言

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

const withLingo = lingoCompiler.vite({
  sourceRoot: "src",
  lingoDir: "lingo",
  sourceLocale: "en",
  targetLocales: ["es", "fr", "de", "ja", "pt", "zh"],
  models: {
    // 为翻译到日语的内容使用专用模型
    "*:ja": "ollama:qwen2",
    // 为翻译到中文的内容使用 Mixtral
    "*:zh": "ollama:mixtral",
    // 为翻译到德语的内容使用 Mistral
    "*:de": "ollama:mistral",
    // 其他目标语言的默认模型
    "*:*": "ollama:llama3.1",
  },
});

在特定语言对之间进行翻译

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

const withLingo = lingoCompiler.vite({
  sourceRoot: "src",
  lingoDir: "lingo",
  sourceLocale: "en",
  targetLocales: ["es", "fr", "de", "ja", "pt", "zh"],
  models: {
    // 特定语言对使用最佳模型
    "en:es": "ollama:gemma2", // 英语到西班牙语
    "en:ja": "ollama:qwen2", // 英语到日语
    "en:zh": "ollama:qwen2", // 英语到中文
    "es:en": "ollama:llama3.1", // 西班牙语到英语
    "fr:en": "ollama:mistral", // 法语到英语
    "de:en": "ollama:phi3", // 德语到英语

    // 未指定语言对的回退模型
    "*:*": "ollama:llama3.1",
  },
});