这将帮助您开始使用 LangChain 与 Ollama 文本补全模型(LLM)。有关详细文档 Ollama 功能和配置选项,请参阅 API 参考.
概述
集成详情
Ollama 允许您在本地运行开源大语言模型(LLM),例如 Llama 3。
Ollama 将模型权重、配置和数据打包到由 Modelfile 定义的单个包中。它优化了设置和配置细节,包括 GPU 使用。
本示例介绍如何使用 LangChain 与 Ollama 运行的 Llama 2 7b 实例进行交互。 有关支持模型和模型变体的完整列表,请参阅 Ollama 模型库.
| 类 | 包 | 本地 | 可序列化 | PY 支持 | 下载 | 版本 |
|---|---|---|---|---|---|---|
Ollama | @langchain/ollama | ✅ | ❌ | ✅ | !NPM - 下载 | !NPM - 版本 |
设置
要访问 Ollama LLM 模型,请按照 这些说明 安装 Ollama,然后安装 @langchain/ollama 集成包。
凭据
如果您想获取模型调用的自动跟踪,您还可以设置您的 LangSmith API 密钥,方法是取消注释以下内容:
# export LANGSMITH_TRACING="true"
# export LANGSMITH_API_KEY="your-api-key"
安装
LangChain Ollama 集成位于 @langchain/ollama package:
npm install @langchain/ollama @langchain/core
yarn add @langchain/ollama @langchain/core
pnpm add @langchain/ollama @langchain/core
实例化
现在我们可以实例化模型对象并生成聊天补全:
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!
多模态模型
Ollama 支持开源多模态模型,例如 LLaVA 在 0.1.15 及更高版本中。 您可以将 base64 编码的图像数据绑定到支持多模态的模型来作为上下文使用,如下所示:
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 参考