Skip to main content

Ollama

caution

您当前所在的页面记录的是如何将 Ollama 模型用作文本补全模型。Ollama 上许多流行的模型是聊天补全模型

您可能需要查看这个页面

这将帮助您使用 LangChain 开始使用 Ollama 文本补全模型 (LLMs)。有关 Ollama 功能和配置选项的详细文档,请参阅API 参考

概览

集成详情

Ollama 允许您本地运行开源大语言模型,例如 Llama 3。

Ollama 将模型权重、配置和数据打包成一个由 Modelfile 定义的单一包。它优化了包括 GPU 使用在内的设置和配置细节。

本示例介绍如何使用 LangChain 与运行中的 Ollama Llama 2 7b 实例进行交互。 有关支持的模型及模型变体的完整列表,请参阅 Ollama 模型库

本地可序列化Python 支持包下载量最新包
Ollama@langchain/ollamaNPM - 下载量NPM - 版本

安装准备

要访问 Ollama 嵌入模型,您需要按照 这些说明 安装 Ollama,并安装 @langchain/ollama 集成包。

凭据

如果您希望自动追踪您的模型调用,也可以取消下面的注释来设置您的 LangSmith API 密钥:

# export LANGSMITH_TRACING="true"
# export LANGSMITH_API_KEY="your-api-key"

安装

LangChain Ollama 集成位于 @langchain/ollama 包中:

:::提示 请参阅安装集成包的一般说明部分。 :::

yarn add @langchain/ollama @langchain/core

实例化

现在我们可以实例化我们的模型对象并生成聊天补全:

import { Ollama } from "@langchain/ollama";

const llm = new Ollama({
model: "llama3", // Default value
temperature: 0,
maxRetries: 2,
// other params...
});

调用

const inputText = "Ollama is an AI company that ";

const completion = await llm.invoke(inputText);
completion;
I think you meant to say "Olivia" instead of "Ollama". Olivia is not a well-known AI company, but there are several other AI companies with similar names. Here are a few examples:

* Oliva AI: A startup that uses artificial intelligence to help businesses optimize their operations and improve customer experiences.
* Olivia Technologies: A company that develops AI-powered solutions for industries such as healthcare, finance, and education.
* Olivia.ai: A platform that uses AI to help businesses automate their workflows and improve productivity.

If you meant something else by "Ollama", please let me know and I'll do my best to help!

链式调用

我们可以将补全模型与提示模板以如下方式进行链式调用

import { PromptTemplate } from "@langchain/core/prompts";

const prompt = PromptTemplate.fromTemplate(
"How to say {input} in {output_language}:\n"
);

const chain = prompt.pipe(llm);
await chain.invoke({
output_language: "German",
input: "I love programming.",
});
A programmer's passion!

In German, you can express your love for programming with the following phrases:

1. Ich liebe Programmieren: This is a direct translation of "I love programming."
2. Programmieren ist meine Leidenschaft: This means "Programming is my passion."
3. Ich bin total verliebt in Programmieren: This translates to "I'm totally in love with programming."
4. Programmieren macht mich glücklich: This phrase means "Programming makes me happy" or "I'm joyful when programming."

If you want to be more casual, you can use:

1. Ich bin ein Programmier-Fan: This is a playful way to say "I'm a fan of programming."
2. Programmieren ist mein Ding: This translates to "Programming is my thing" or "I'm all about programming."

Remember that German has different forms for formal and informal speech, so adjust the phrases according to your relationship with the person you're speaking to!

多模态模型

Ollama 支持开源的多模态模型,例如 LLaVA,适用于 0.1.15 及以上版本。 你可以将经过 base64 编码的图像数据绑定到具备多模态能力的模型中,作为上下文使用,示例如下:

import { Ollama } from "@langchain/ollama";
import * as fs from "node:fs/promises";

const imageData = await fs.readFile("../../../../../examples/hotdog.jpg");

const model = new Ollama({
model: "llava",
}).bind({
images: [imageData.toString("base64")],
});

const res = await model.invoke("What's in this image?");
console.log(res);
 The image shows a hot dog placed inside what appears to be a bun that has been specially prepared to resemble a hot dog bun. This is an example of a creative or novelty food item, where the bread used for the bun looks similar to a cooked hot dog itself, playing on the name "hot dog." The image also shows the typical garnishes like ketchup and mustard on the side.

相关内容

API 参考文档

如需了解所有 Ollama 特性和配置的详细文档,请访问 API 参考页面


Was this page helpful?


You can also leave detailed feedback on GitHub.