Rockset
Rockset 是一个在云中运行的实时分析 SQL 数据库。Rockset 提供向量搜索功能,以 SQL 函数 的形式,以支持依赖文本相似性的 AI 应用程序。
设置
安装 rockset 客户端。
yarn add @rockset/client
用法
提示
参见 有关安装集成包的常规说明的此部分。
- npm
- Yarn
- pnpm
npm install @langchain/openai @langchain/core @langchain/community
yarn add @langchain/openai @langchain/core @langchain/community
pnpm add @langchain/openai @langchain/core @langchain/community
以下示例展示了如何使用 OpenAI 和 Rockset 来回答有关文本文件的问题
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/chains/combine_documents";
import { createRetrievalChain } from "langchain/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();
API 参考
- ChatOpenAI 来自
@langchain/openai
- OpenAIEmbeddings 来自
@langchain/openai
- RocksetStore 来自
@langchain/community/vectorstores/rockset
- RecursiveCharacterTextSplitter 来自
@langchain/textsplitters
- ChatPromptTemplate 来自
@langchain/core/prompts
- createStuffDocumentsChain 来自
langchain/chains/combine_documents
- createRetrievalChain 来自
langchain/chains/retrieval