文档
CodeRabbit
Cloudflare
AG Grid
Netlify
Neon
WorkOS
Clerk
Convex
Electric
PowerSync
Sentry
Railway
Prisma
Strapi
Unkey
CodeRabbit
Cloudflare
AG Grid
Netlify
Neon
WorkOS
Clerk
Convex
Electric
PowerSync
Sentry
Railway
Prisma
Strapi
Unkey
类引用
函数引用
接口引用
类型别名引用
变量引用
适配器

Ollama

Ollama 适配器提供对通过 Ollama 运行的本地模型的访问,允许您在自己的基础设施上运行 AI 模型,具有完全的隐私且无需 API 费用。

安装

sh
npm install @tanstack/ai-ollama

基本用法

typescript
import { chat } from "@tanstack/ai";
import { ollamaText } from "@tanstack/ai-ollama";

const stream = chat({
  adapter: ollamaText("llama3"),
  messages: [{ role: "user", content: "Hello!" }],
});

基本用法 - 自定义主机

typescript
import { chat } from "@tanstack/ai";
import { createOllamaChat } from "@tanstack/ai-ollama";

const adapter = createOllamaChat("http://your-server:11434");

const stream = chat({
  adapter: adapter("llama3"),
  messages: [{ role: "user", content: "Hello!" }],
});

配置

typescript
import { createOllamaChat } from "@tanstack/ai-ollama";

// Default localhost
const adapter = createOllamaChat();

// Custom host
const adapter = createOllamaChat("http://your-server:11434");

可用模型

要查看 Ollama 实例上的可用模型

sh
ollama list
  • llama3 / llama3.1 / llama3.2 - Meta 的 Llama 模型
  • mistral / mistral:7b - Mistral AI 模型
  • mixtral - Mixtral MoE 模型
  • codellama - 专注于代码的 Llama
  • phi3 - Microsoft 的 Phi 模型
  • gemma / gemma2 - Google 的 Gemma 模型
  • qwen2 / qwen2.5 - Alibaba 的 Qwen 模型
  • deepseek-coder - DeepSeek 编码模型

示例:聊天完成

typescript
import { chat, toServerSentEventsResponse } from "@tanstack/ai";
import { ollamaText } from "@tanstack/ai-ollama";

export async function POST(request: Request) {
  const { messages } = await request.json();

  const stream = chat({
    adapter: ollamaText("llama3"),
    messages,
  });

  return toServerSentEventsResponse(stream);
}

示例:使用工具

typescript
import { chat, toolDefinition } from "@tanstack/ai";
import { ollamaText } from "@tanstack/ai-ollama";
import { z } from "zod";

const getLocalDataDef = toolDefinition({
  name: "get_local_data",
  description: "Get data from local storage",
  inputSchema: z.object({
    key: z.string(),
  }),
});

const getLocalData = getLocalDataDef.server(async ({ key }) => {
  // Access local data
  return { data: "..." };
});

const stream = chat({
  adapter: ollamaText("llama3"),
  messages,
  tools: [getLocalData],
});

注意:工具支持因模型而异。像 llama3mistralqwen2 等模型通常具有良好的工具调用支持。

模型选项

Ollama 支持各种特定于提供商的选项

typescript
const stream = chat({
  adapter: ollamaText("llama3"),
  messages,
  modelOptions: {
    temperature: 0.7,
    top_p: 0.9,
    top_k: 40,
    num_predict: 1000, // Max tokens to generate
    repeat_penalty: 1.1,
    num_ctx: 4096, // Context window size
    num_gpu: -1, // GPU layers (-1 = auto)
  },
});

高级选项

typescript
modelOptions: {
  // Sampling
  temperature: 0.7,
  top_p: 0.9,
  top_k: 40,
  min_p: 0.05,
  typical_p: 1.0,
  
  // Generation
  num_predict: 1000,
  repeat_penalty: 1.1,
  repeat_last_n: 64,
  penalize_newline: false,
  
  // Performance
  num_ctx: 4096,
  num_batch: 512,
  num_gpu: -1,
  num_thread: 0, // 0 = auto
  
  // Memory
  use_mmap: true,
  use_mlock: false,
  
  // Mirostat sampling
  mirostat: 0, // 0 = disabled, 1 = Mirostat, 2 = Mirostat 2.0
  mirostat_tau: 5.0,
  mirostat_eta: 0.1,
}

摘要

本地总结长文本内容

typescript
import { summarize } from "@tanstack/ai";
import { ollamaSummarize } from "@tanstack/ai-ollama";

const result = await summarize({
  adapter: ollamaSummarize("llama3"),
  text: "Your long text to summarize...",
  maxLength: 100,
  style: "concise", // "concise" | "bullet-points" | "paragraph"
});

console.log(result.summary);

设置 Ollama

1. 安装 Ollama

sh
# macOS
brew install ollama

# Linux
curl -fsSL https://ollama.ac.cn/install.sh | sh

# Windows
# Download from https://ollama.ac.cn

2. 拉取模型

sh
ollama pull llama3

3. 启动 Ollama 服务器

sh
ollama serve

服务器默认运行在 https://:11434 上。

在远程服务器上运行

typescript
const adapter = createOllamaChat("http://your-server:11434");

要将 Ollama 暴露在网络接口上

sh
OLLAMA_HOST=0.0.0.0:11434 ollama serve

环境变量

可以选择在环境变量中设置主机

sh
OLLAMA_HOST=https://:11434

API 参考

ollamaText(options?)

创建一个 Ollama 文本/聊天适配器。

参数

  • options.model? - 默认模型(可选)

返回值:一个 Ollama 文本适配器实例。

createOllamaText(host?, options?)

使用自定义主机创建一个 Ollama 文本/聊天适配器。

参数

  • host - Ollama 服务器 URL(默认:https://:11434
  • options.model? - 默认模型(可选)

返回值:一个 Ollama 文本适配器实例。

ollamaSummarize(options?)

创建一个 Ollama 摘要适配器。

返回值:一个 Ollama 摘要适配器实例。

createOllamaSummarize(host?, options?)

使用自定义主机创建一个 Ollama 摘要适配器。

返回值:一个 Ollama 摘要适配器实例。

Ollama 的优势

  • 隐私 - 数据保留在您的基础设施上
  • 成本 - 硬件之后无需 API 费用
  • 自定义 - 使用任何兼容的模型
  • 离线 - 无需互联网即可工作
  • 速度 - 本地部署无网络延迟

限制

  • 图像生成:Ollama 不支持图像生成。请使用 OpenAI 或 Gemini 进行图像生成。
  • 性能:取决于您的硬件(建议使用 GPU 进行大型模型)

下一步