4. 在 JavaScript 中使用 Ollama API


在 JavaScript 中使用 Ollama API

本文介绍了如何在 JavaScript 中使用 Ollama API 。这篇文档旨在帮助开发者快速上手并充分利用 Ollama 的能力。你可以在 Node.js 环境中使用,也可以在浏览器中直接导入对应的模块。通过学习本文档,你可以轻松集成 Ollama 到你的项目中。

安装 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 }) ​

API

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 ​

参考文档

作者与出处
原作者:AXYZdong
整理人: 灏天文库整理
本站整理收录,版权归原作者/开源协议所有;欢迎通过原文链接访问源仓库。
发布者: 作者: AXYZdong 转发
评论区 (0)
U