跳至主要内容
前往文档
⌘U
Weaviate 数据库

使用 Weaviate 的 APIs 和工具开发 AI 应用

部署

部署、配置和维护 Weaviate 数据库

Weaviate Agents

使用 Weaviate 构建和部署智能代理

Weaviate Cloud

在云端管理和扩展 Weaviate

更多资源

集成
贡献者指南
活动 & 工作坊
Weaviate Academy

需要帮助?

Weaviate Logo询问 AI 助手⌘K
社区论坛

JavaScript 和 TypeScript

JavaScript/TypeScript 客户端 (SDK)

最新的 TypeScript 客户端版本是 v3.9.0

TypeScript 客户端支持用 TypeScript 或 JavaScript 编写的代码。

v3 客户端是当前的 TypeScript 客户端。如果您有为 v2 客户端编写的代码,您应该将其迁移到 v3,因为 v2 客户端不再维护。

注意

v3 客户端支持服务器端开发(Node.js 托管)。如果您的应用程序是基于浏览器的,您可能需要考虑使用 TypeScript 客户端 v2。请记住,v2 客户端已过时且不再官方维护。

安装

本节详细介绍了如何安装和配置 v3 TypeScript 客户端。

安装包

v3 客户端包有一个新的名称,weaviate-client。使用 npm 安装 TypeScript 客户端库包

npm install weaviate-client

导入客户端

v3 客户端使用 ES Modules。文档中的大部分示例代码也使用 ES Module 样式。

如果您的代码需要 CommonJS 兼容性,请使用 CommonJS 导入样式

import weaviate from "weaviate-client";

TypeScript 设置

编辑您的项目配置文件以进行这些更改

  • package.json 中添加 "type": "module"
  • 将以下代码添加到 tsconfig.json
tsconfig.json 文件
{
"compilerOptions": {
"target": "ESNext",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"esModuleInterop": true,
"skipLibCheck": true,
"allowSyntheticDefaultImports": true,
"strict": true
},
"include": ["src/index.ts"] // this compiles only src/.index.ts, to compile all .ts files, use ["*.ts"]
}

入门

先决条件

如果您尚未这样做,我们建议您首先完成 快速入门教程,以便充分利用本节。

以下代码演示了如何

  1. 连接到本地 Weaviate 实例。
  2. 创建一个新的集合。
  3. 使用批量导入填充数据库并向量化数据。
  4. 执行向量搜索。
import weaviate, { WeaviateClient, vectors } from 'weaviate-client';

const client: WeaviateClient = await weaviate.connectToLocal();

await client.collections.create({
name: 'Question',
vectorizers: vectors.text2VecOllama({ // Configure the Ollama embedding integration
apiEndpoint: 'http://ollama:11434', // If using Docker you might need: http://host.docker.internal:11434
model: 'nomic-embed-text', // The model to use
}),
});

// Load data
async function getJsonData() {
const file = await fetch(
'https://raw.githubusercontent.com/weaviate-tutorials/quickstart/main/data/jeopardy_tiny.json'
);
return file.json();
}

// Note: The TS client does not have a `batch` method yet
// We use `insertMany` instead, which sends all of the data in one request
async function importQuestions() {
const questions = client.collections.use('Question');
const data = await getJsonData();
const result = await questions.data.insertMany(data);
console.log('Insertion response: ', result);
}

await importQuestions();

const questions = client.collections.use('Question');

const result = await questions.query.nearText('biology', {
limit: 2,
});

result.objects.forEach((item) => {
console.log(JSON.stringify(item.properties, null, 2));
});

client.close(); // Close the client connection

异步使用

所有客户端 v3 方法,除了 collection.use(),都使用 ES6 Promises 和异步代码。这意味着您必须在函数调用后使用 .then(),或者将您的代码包装在 async/await 块中。

当存在异步代码错误时,promise 会返回特定的错误消息。如果您使用 asyncawait,则拒绝的 promise 就像抛出异常一样。

发布版本

转到 GitHub 发布页面,以查看 TypeScript 客户端库发布的历史记录和变更日志。

点击此处查看 Weaviate 和相应客户端版本的表格

此表列出了最新的五个 Weaviate 数据库版本和相应的客户端库版本。

Weaviate 数据库
(GitHub)
首次
发布日期
Python
(GitHub)
TypeScript/
JavaScript
(GitHub)
Go
(GitHub)
Java
(GitHub)
1.35.x2025-12-174.19.xN/AN/AN/A
1.34.x2025-11-054.18.x3.10.x5.6.x6.0.0
1.33.x2025-09-254.17.x3.9.x5.5.x5.5.x
1.32.x2025-07-144.16.x3.8.x5.3.x5.4.x
1.31.x2025-05-304.15.x3.6.x5.2.x5.3.x
1.30.x2025-04-034.12.x3.5.x5.1.x5.2.x
1.29.x2025-02-174.11.x3.4.x5.0.x5.1.x
旧版本发布
Weaviate 数据库
(GitHub)
首次
发布日期
Python
(GitHub)
TypeScript/
JavaScript
(GitHub)
Go
(GitHub)
Java
(GitHub)
1.28.x2024-12-114.10.x3.3.x4.16.x5.0.x
1.27.x2024-10-164.9.x3.2.x4.16.x5.0.x
4.9.x
1.26.x2024-07-224.7.x3.1.x4.15.x4.8.x
1.25.x2024-05-104.6.x2.1.x4.13.x4.6.x
1.24.x2024-02-274.5.x2.0.x4.10.x4.4.x
1.23.x2023-12-183.26.x1.5.x4.10.x4.4.x
1.22.x2023-10-273.25.x1.5.x4.10.x4.3.x
1.21.x2023-08-173.22.x1.4.x4.9.x4.2.x
1.20.x2023-07-063.22.x1.1.x4.7.x4.2.x
1.19.x2023-05-043.17.x1.1.x14.7.x4.0.x
1.18.x2023-03-073.13.x2.14.x4.6.x3.6.x
1.17.x2022-12-203.9.x2.14.x4.5.x3.5.x
1.16.x2022-10-313.8.x2.13.x4.4.x3.4.x
1.15.x2022-09-073.6.x2.12.x4.3.x3.3.x
1.14.x2022-07-073.6.x2.11.x4.2.x3.2.x
1.13.x2022-05-033.4.x2.9.x4.0.x2.4.x
1.12.x2022-04-053.4.x2.8.x3.0.x2.3.x
1.11.x2022-03-143.2.x2.7.x2.6.x2.3.x
1.10.x2022-01-273.1.x2.5.x2.4.x2.1.x
1.9.x2021-12-103.1.x2.4.x2.4.x2.1.x
1.8.x2021-11-303.1.x2.4.x2.3.x1.1.x
1.7.x2021-09-013.1.x2.4.x2.3.x1.1.x
1.6.x2021-08-112.4.x2.3.x2.2.x1.0.x
1.5.x2021-07-132.2.x2.1.x2.1.x1.0.x
1.4.x2021-06-092.2.x2.1.x2.1.x1.0.x
1.3.x2021-04-232.2.x2.1.x2.1.x1.0.x
1.2.x2021-03-152.2.x2.0.x1.1.x-
1.1.x2021-02-102.1.x---
1.0.x2021-01-142.0.x---

TypeScript 客户端变更

TypeScript 客户端 于 2023-03-17 替换了 JavaScript 客户端

Vectorizer API 更改 v3.8.0

从 Weaviate JS/TS 客户端 v3.8.0 开始,在创建集合时对 vectorizer 配置 API 进行了多项更改

  • configure.vectorizer 已被 configure.vectors 替换
  • 添加了 configure.multiVectors 以使用户能够使用 多向量
  • configure.vectorizer.none 已被 configure.vectors.selfProvided 替换

JavaScript/TypeScript 客户端 v2 弃用

Weaviate JavaScript/TypeScript 客户端 v2 已被弃用,不应再使用。如果您需要 v2 客户端的文档,请参阅 文档存档。如果您正在从 JavaScript/TypeScript v2 客户端迁移到 v3 客户端,请参阅此 迁移指南

代码示例和更多资源

可以在 Weaviate 文档中找到有关各种操作和功能的用法信息。



Weaviate API 参考页面中的 搜索REST 也可能是很好的起点。

问题和反馈

如果您有任何问题或反馈,请在 用户论坛 中告诉我们。