Couchbase
Couchbase 是一个屡获殊荣的分布式 NoSQL 云数据库,可为你的所有云、移动、AI 和边缘计算应用提供无与伦比的多功能性、性能、可扩展性和经济价值。
¥Couchbase is an award-winning distributed NoSQL cloud database that delivers unmatched versatility, performance, scalability, and financial value for all of your cloud, mobile, AI, and edge computing applications.
本指南介绍如何从 CouchBase 数据库加载文档。
¥This guide shows how to use load documents from couchbase database.
安装
¥Installation
- npm
- Yarn
- pnpm
npm install @langchain/community @langchain/core couchbase
yarn add @langchain/community @langchain/core couchbase
pnpm add @langchain/community @langchain/core couchbase
用法
¥Usage
从 Couchbase 查询文档
¥Querying for Documents from Couchbase
对于不支持流式传输的模型,整个响应将作为单个块返回。
¥For more details on connecting to a Couchbase cluster, please check the Node.js SDK documentation.
有关使用 SQL++(用于 JSON 的 SQL)查询文档的帮助,请查看 documentation。
¥For help with querying for documents using SQL++ (SQL for JSON), please check the documentation.
import { CouchbaseDocumentLoader } from "@langchain/community/document_loaders/web/couchbase";
import { Cluster } from "couchbase";
const connectionString = "couchbase://localhost"; // valid couchbase connection string
const dbUsername = "Administrator"; // valid database user with read access to the bucket being queried
const dbPassword = "Password"; // password for the database user
// query is a valid SQL++ query
const query = `
SELECT h.* FROM \`travel-sample\`.inventory.hotel h
WHERE h.country = 'United States'
LIMIT 1
`;
连接到 Couchbase 集群
¥Connect to Couchbase Cluster
const couchbaseClient = await Cluster.connect(connectionString, {
username: dbUsername,
password: dbPassword,
configProfile: "wanDevelopment",
});
创建加载器
¥Create the Loader
const loader = new CouchbaseDocumentLoader(
couchbaseClient, // The connected couchbase cluster client
query // A valid SQL++ query which will return the required data
);
加载文档
¥Load Documents
你可以通过调用加载器的 load 方法来获取文档。它将返回包含所有文档的列表。如果你想避免这种阻塞调用,你可以调用返回迭代器的 lazy_load 方法。
¥You can fetch the documents by calling the load method of the loader. It will return a list with all the documents. If you want to avoid this blocking call, you can call lazy_load method that returns an Iterator.
// using load method
docs = await loader.load();
console.log(docs);
// using lazy_load
for await (const doc of this.lazyLoad()) {
console.log(doc);
break; // break based on required condition
}
指定包含内容和元数据的字段
¥Specifying Fields with Content and Metadata
可以使用 pageContentFields 参数指定文档内容中的字段。可以使用 metadataFields 参数指定文档的元数据字段。
¥The fields that are part of the Document content can be specified using the pageContentFields parameter.
The metadata fields for the Document can be specified using the metadataFields parameter.
const loaderWithSelectedFields = new CouchbaseDocumentLoader(
couchbaseClient,
query,
// pageContentFields
[
"address",
"name",
"city",
"phone",
"country",
"geo",
"description",
"reviews",
],
["id"] // metadataFields
);
const filtered_docs = await loaderWithSelectedFields.load();
console.log(filtered_docs);