本文档介绍了如何在 javascript 环境中使用 ollama 与 langchain 集成,以创建强大的 ai 应用。ollama 是一个开源的大语言模型部署工具,而 langchain 则是一个用于构建基于语言模型的应用的框架。通过结合这两者,我们可以在本地环境中快速部署和使用先进的 ai 模型。
注: 本文档包含核心代码片段和详细解释。完整代码可在 notebook/c5/ollama_langchain_javascript 中找到。
首先,确保你的系统已安装 Node.js。你可以从 Node.js官网 下载并安装最新版本。
cd notebook/C5/ollama_langchain_javascript npm init -y
npm install @langchain/ollama @langchain/core @langchain/community zod
{ "type": "module", // ... 其他配置 }
命令行执行完毕后如图所示:
立即学习“Java免费学习笔记(深入)”;
模型存储位置:
下载完毕后,需要确保 Ollama 服务已经启动:
ollama ps
可运行 base_chat.js 文件,具体代码如下:
import { Ollama } from "@langchain/community/llms/ollama"; const ollama = new Ollama({ baseUrl: "http://localhost:11434", // 确保Ollama服务已经启动 model: "llama3.1", // 替换为实际使用的模型 }); const stream = await ollama.stream( `你比GPT4厉害吗?` ); const chunks = []; for await (const chunk of stream) { chunks.push(chunk); } console.log(chunks.join(""));
运行代码:
node base_chat.js
这段代码做了以下几件事:
运行base_multimodal.js文件,具体代码如下:
import { Ollama } from "@langchain/community/llms/ollama"; import * as fs from "node:fs/promises"; const imageData = await fs.readFile("../../../docs/images/C5-1-4.png"); // 可以替换为你想询问的图片 const model = new Ollama({ model: "llava", baseUrl: "http://127.0.0.1:11434", }).bind({ images: [imageData.toString("base64")], }); const res = await model.invoke("图片里是什么动物呀?"); console.log({ res });
运行代码:
node base_multimodal.js
这段代码演示了如何使用多模态模型(如 llava)处理图像和文本输入:
运行 base_tool.js 文件,代码如下:
import { tool } from "@langchain/core/tools"; import { ChatOllama } from "@langchain/ollama"; import { z } from "zod"; // 定义简单计算器工具 const simpleCalculatorTool = tool((args) => { const { operation, x, y } = args; switch (operation) { case "add": return x + y; case "subtract": return x - y; case "multiply": return x * y; case "divide": if (y !== 0) { return x / y; } else { throw new Error("Cannot divide by zero"); } default: throw new Error("Invalid operation"); } }, { name: "simple_calculator", description: "Perform simple arithmetic operations", schema: z.object({ operation: z.enum(["add", "subtract", "multiply", "divide"]), x: z.number(), y: z.number(), }), }); // 定义模型 const llm = new ChatOllama({ model: "llama3.1", temperature: 0, }); // 将工具绑定到模型 const llmWithTools = llm.bindTools([simpleCalculatorTool]); // 使用模型进行工具调用 const result = await llmWithTools.invoke( "你知道一千万乘二是多少吗?请使用 'simple_calculator' 工具来计算。" ); console.log(result);
运行代码:
node base_tool.js
这段代码展示了如何定义和使用工具:
自定义提示模板不仅提高了内容生成的效率,还确保了输出的一致性和针对性。通过精心设计的模板,我们可以充分利用AI模型的能力,同时保持对输出内容的控制和指导:
import { ChatOllama } from "@langchain/ollama"; import { ChatPromptTemplate, SystemMessagePromptTemplate, HumanMessagePromptTemplate } from "@langchain/core/prompts"; // 初始化ChatOllama模型 const model = new ChatOllama({ model: "llama3.1", temperature: 0.7, }); const systemMessageContent = ` 你是一位经验丰富的电商文案撰写专家。你的任务是根据给定的产品信息创作吸引人的商品描述。 请确保你的描述简洁、有力,并且突出产品的核心优势。 `; const humanMessageTemplate = ` 请为以下产品创作一段吸引人的商品描述: 产品类型: {product_type} 核心特性: {key_feature} 目标受众: {target_audience} 价格区间: {price_range} 品牌定位: {brand_positioning} 请提供以下三种不同风格的描述,每种大约50字: 1. 理性分析型 2. 情感诉求型 3. 故事化营销型 `; const prompt = ChatPromptTemplate.fromMessages([ SystemMessagePromptTemplate.fromTemplate(systemMessageContent), HumanMessagePromptTemplate.fromTemplate(humanMessageTemplate), ]); const chain = prompt.pipe(model); async function generateProductDescriptions(productInfo) { const response = await chain.invoke(productInfo); return response.content; } // 示例使用 const productInfo = { product_type: "智能手表", key_feature: "心率监测和睡眠分析", target_audience: "注重健康的年轻专业人士", price_range: "中高端", brand_positioning: "科技与健康的完美结合" }; generateProductDescriptions(productInfo) .then((result) => console.log(result)) .catch((error) => console.error("Error:", error));
运行代码:
node advanced_prompt.js
这段代码展示了如何创建和使用自定义提示模板:
自定义提示模板在实际应用中有着广泛的用途,尤其是在需要生成特定格式或风格的内容时。以下是一些实际应用场景:
在这个例子中,我们展示了如何使用 Ollama 和 LangChain 生成结构化的 JSON 输出,特别是用于创建知识图谱。 这种方法与 Microsoft 的开源项目 GraphRAG 有着密切的联系,尤其是在自动化知识提取和三元组生成方面。
import { ChatOllama } from "@langchain/ollama"; import { PromptTemplate } from "@langchain/core/prompts"; import { HumanMessage, SystemMessage } from "@langchain/core/messages"; const systemTemplate = ` 你是一位医疗领域的专家,擅长创建知识图谱。请将所有响应格式化为具有以下结构的JSON对象: { "节点": [ {"id": "string", "标签": "string", "类型": "string"} ], "关系": [ {"源": "string", "目标": "string", "关系": "string"} ] } 确保所有节点id都是唯一的,并且关系引用的是已存在的节点id。 `; const humanTemplate = ` 请为医疗主题"{topic}"创建一个知识图谱。包括以下相关概念: {concepts}。 提供至少5个节点和5个关系。请确保使用中文回答。 `; const systemMessage = new SystemMessage(systemTemplate); const humanPrompt = PromptTemplate.fromTemplate(humanTemplate); const llmJsonMode = new ChatOllama({ baseUrl: "http://localhost:11434", // 默认值 model: "llama3.1", format: "json", }); async function generateMedicalKnowledgeGraph(topic, concepts) { try { const humanMessageContent = await humanPrompt.format({ topic: topic, concepts: concepts.join("、"), }); const humanMessage = new HumanMessage(humanMessageContent); const messages = [systemMessage, humanMessage]; const result = await llmJsonMode.call(messages); console.log(JSON.stringify(result, null, 2)); return result; } catch (error) { console.error("生成知识图谱时出错:", error); } } // 使用示例 const topic = "糖尿病"; const concepts = ["胰岛素", "血糖", "并发症", "饮食管理", "运动疗法"]; generateMedicalKnowledgeGraph(topic, concepts);
运行代码:
node advanced_json.js
这段代码演示了如何使用 Ollama 生成结构化的 JSON 输出:
我们可以窥见到通过 json 格式的特定输出,可以将其运用到许多方面:
通过这些示例,我们展示了如何使用 Ollama 和 LangChain 在 JavaScript 环境中构建各种 AI 应用,从简单的对话系统到复杂的知识图谱生成。这些工具和技术为开发强大的AI应用提供了坚实的基础。
Ollama 和 LangChain 的结合为开发者提供了极大的灵活性和可能性。你可以根据具体需求,选择合适的模型和组件,构建出适合你的应用场景的 AI 系统。
随着技术的不断发展,我们期待看到更多创新的应用出现。希望这个指南能够帮助你开始你的 AI 开发之旅,并激发你的创造力,去探索 AI 技术的无限可能。
以上就是Ollama 在 LangChain 中的使用 - JavaScript 集成的详细内容,更多请关注php中文网其它相关文章!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号