本文介绍了如何在 JavaScript 中使用 Ollama API 。这篇文档旨在帮助开发者快速上手并充分利用 Ollama 的能力。你可以在 Node.js 环境中使用,也可以在浏览器中直接导入对应的模块。通过学习本文档,你可以轻松集成 Ollama 到你的项目中。
npm i ollama
import ollama from 'ollama' const response = await ollama.chat({ model: 'llama3.1', messages: [{ role: 'user', content: '为什么天空是蓝色的?' }], }) console.log(response.message.content)
要在没有 Node.js 的情况下使用此库,请导入浏览器模块。
import ollama from 'ollama/browser'
可以通过设置 stream: true 启用响应流,使函数调用返回一个 AsyncGenerator ,其中每个部分都是流中的一个对象。
import ollama from 'ollama' const message = { role: 'user', content: '为什么天空是蓝色的?' } const response = await ollama.chat({ model: 'llama3.1', messages: [message], stream: true }) for await (const part of response) { process.stdout.write(part.message.content) }
import ollama from 'ollama' const modelfile = ` FROM llama3.1 SYSTEM "你是超级马里奥兄弟中的马里奥。" ` await ollama.create({ model: 'example', modelfile: modelfile })
Ollama JavaScript 库的 API 设计围绕 Ollama REST API 。如果你想了解更详细的底层实现和完整的 API 端点信息,建议参考 Ollama API 使用指南
ollama.chat(request)
request <Object>: 包含聊天参数的请求对象。
model <string> 要用于聊天的模型名称。messages <Message[]>: 表示聊天历史的消息对象数组。
role <string>: 消息发送者的角色('user','system'或'assistant')。content <string>: 消息的内容。images <Uint8Array[] | string[]>: (可选) 要包含在消息中的图像,可以是 Uint8Array 或 base64 编码的字符串。format <string>: (可选) 设置响应的预期格式 (json)。stream <boolean>: (可选) 如果为 true,则返回 AsyncGenerator。keep_alive <string | number>: (可选) 保持模型加载的时间长度。tools <Tool[]>: (可选) 模型可能调用的工具列表。options <Options>: (可选) 配置运行时的选项。返回: <ChatResponse>
ollama.generate(request)
request <Object>: 包含生成参数的请求对象。
model <string> 要用于聊天的模型名称。prompt <string>: 发送到模型的提示。suffix <string>: (可选) 后缀是插入文本后面的文本。system <string>: (可选) 覆盖模型系统提示。template <string>: (可选) 覆盖模型模板。raw <boolean>: (可选) 绕过提示模板并直接将提示传递给模型。images <Uint8Array[] | string[]>: (可选) 要包含的图像,可以是 Uint8Array 或 base64 编码的字符串。format <string>: (可选) 设置响应的预期格式 (json)。stream <boolean>: (可选) 如果为 true,则返回 AsyncGenerator。keep_alive <string | number>: (可选) 保持模型加载的时间长度。options <Options>: (可选) 配置运行时的选项。<GenerateResponse>ollama.pull(request)
request <Object>: 包含拉取参数的请求对象。
model <string> 要拉取的模型名称。insecure <boolean>: (可选) 从无法验证身份的服务器拉取。stream <boolean>: (可选) 如果为 true,则返回 AsyncGenerator。<ProgressResponse>ollama.push(request)
request <Object>: 包含推送参数的请求对象。
model <string> 要推送的模型名称。insecure <boolean>: (可选) 推送到无法验证身份的服务器。stream <boolean>: (可选) 如果为 true,则返回 AsyncGenerator。<ProgressResponse>ollama.create(request)
request <Object>: 包含创建参数的请求对象。
model <string> 要创建的模型名称。path <string>: (可选) 要创建的模型文件的路径。modelfile <string>: (可选) 要创建的模型文件的内容。stream <boolean>: (可选) 如果为 true,则返回 AsyncGenerator。<ProgressResponse>ollama.delete(request)
request <Object>: 包含删除参数的请求对象。
model <string> 要删除的模型名称。<StatusResponse>ollama.copy(request)
request <Object>: 包含复制参数的请求对象。
source <string> 要从中复制的模型名称。destination <string> 要复制到的模型名称。<StatusResponse>ollama.list()
<ListResponse>ollama.show(request)
request <Object>: 包含显示参数的请求对象。
model <string> 要显示的模型名称。system <string>: (可选) 覆盖模型系统提示返回值。template <string>: (可选) 覆盖模型模板返回值。options <Options>: (可选) 配置运行时的选项。<ShowResponse>ollama.embed(request)
request <Object>: 包含嵌入参数的请求对象。
model <string> 用于生成嵌入的模型名称。input <string> | <string[]>: 用于生成嵌入的输入。truncate <boolean>: (可选) 截断输入以适应模型支持的最大上下文长度。keep_alive <string | number>: (可选) 保持模型加载的时间长度。options <Options>: (可选) 配置运行时的选项。<EmbedResponse>ollama.ps()
<ListResponse>可以使用以下字段创建自定义客户端:
host <string>: (可选) Ollama 主机地址。默认: "http://127.0.0.1:11434"。fetch <Object>: (可选) 用于向 Ollama 主机发出请求的 fetch 库。import { Ollama } from 'ollama' const ollama = new Ollama({ host: 'http://127.0.0.1:11434' }) const response = await ollama.chat({ model: 'llama3.1', messages: [{ role: 'user', content: '为什么天空是蓝色的?' }], })
要构建项目文件,请运行:
npm run build
参考文档