跳到主要内容
Rockset(已被 OpenAI 收购)是一个在云端运行的实时分析 SQL 数据库。Rockset 提供向量搜索功能,以 SQL 函数的形式,支持依赖文本相似度的人工智能应用。

设置

安装 rockset 客户端。
yarn add @rockset/client

用法

有关安装 LangChain 软件包的一般说明,请参阅此部分
npm
npm install @langchain/openai @langchain/core @langchain/community
import * as rockset from "@rockset/client";
import { ChatOpenAI, OpenAIEmbeddings } from "@langchain/openai";
import { RocksetStore } from "@langchain/community/vectorstores/rockset";
import { RecursiveCharacterTextSplitter } from "@langchain/textsplitters";
import { readFileSync } from "fs";
import { ChatPromptTemplate } from "@langchain/core/prompts";
import { createStuffDocumentsChain } from "@langchain/classic/chains/combine_documents";
import { createRetrievalChain } from "@langchain/classic/chains/retrieval";

const store = await RocksetStore.withNewCollection(new OpenAIEmbeddings(), {
  client: rockset.default.default(
    process.env.ROCKSET_API_KEY ?? "",
    `https://api.${process.env.ROCKSET_API_REGION ?? "usw2a1"}.rockset.com`
  ),
  collectionName: "langchain_demo",
});

const model = new ChatOpenAI({ model: "gpt-3.5-turbo-1106" });
const questionAnsweringPrompt = ChatPromptTemplate.fromMessages([
  [
    "system",
    "Answer the user's questions based on the below context:\n\n{context}",
  ],
  ["human", "{input}"],
]);

const combineDocsChain = await createStuffDocumentsChain({
  llm: model,
  prompt: questionAnsweringPrompt,
});

const chain = await createRetrievalChain({
  retriever: store.asRetriever(),
  combineDocsChain,
});

const text = readFileSync("state_of_the_union.txt", "utf8");
const docs = await new RecursiveCharacterTextSplitter().createDocuments([text]);

await store.addDocuments(docs);
const response = await chain.invoke({
  input: "When was America founded?",
});
console.log(response.answer);
await store.destroy();

以编程方式连接这些文档到 Claude、VSCode 等,通过 MCP 获取实时答案。
© . This site is unofficial and not affiliated with LangChain, Inc.