跳到主要内容

Xata 聊天记忆

Xata 是一个基于 PostgreSQL 的无服务器数据平台。它提供类型安全的 TypeScript/JavaScript SDK 用于与数据库交互,以及一个用于管理数据的 UI。

通过 XataChatMessageHistory 类,你可以使用 Xata 数据库来持久化更长时间的聊天会话。

由于 Xata 通过 REST API 工作,并拥有纯 TypeScript SDK,你可以将其与 Vercel EdgeCloudflare Workers 和任何其他无服务器环境一起使用。

设置

安装 Xata CLI

npm install @xata.io/cli -g

创建一个数据库用作向量存储

Xata UI 中创建一个新数据库。你可以随意命名,但在本例中,我们将使用 langchain

首次执行时,Xata LangChain 集成将创建用于存储聊天消息的表。如果已存在具有该名称的表,则会保持不变。

初始化项目

在你的项目中,运行

xata init

然后选择上面创建的数据库。 这还将生成一个 xata.tsxata.js 文件,其中定义了可用于与数据库交互的客户端。 有关使用 Xata JavaScript/TypeScript SDK 的更多详细信息,请参阅 Xata 入门文档

使用方法

存储在 Xata 数据库中的每个聊天历史会话都必须具有唯一的 ID。

在此示例中,getXataClient() 函数用于基于环境变量创建新的 Xata 客户端。 但是,我们建议使用 xata init 命令生成的代码,在这种情况下,你只需要从生成的 xata.ts 文件中导入 getXataClient() 函数。

npm install @langchain/openai @langchain/community @langchain/core
import { BufferMemory } from "langchain/memory";
import { ChatOpenAI } from "@langchain/openai";
import { ConversationChain } from "langchain/chains";
import { XataChatMessageHistory } from "@langchain/community/stores/message/xata";
import { BaseClient } from "@xata.io/client";

// if you use the generated client, you don't need this function.
// Just import getXataClient from the generated xata.ts instead.
const getXataClient = () => {
if (!process.env.XATA_API_KEY) {
throw new Error("XATA_API_KEY not set");
}

if (!process.env.XATA_DB_URL) {
throw new Error("XATA_DB_URL not set");
}
const xata = new BaseClient({
databaseURL: process.env.XATA_DB_URL,
apiKey: process.env.XATA_API_KEY,
branch: process.env.XATA_BRANCH || "main",
});
return xata;
};

const memory = new BufferMemory({
chatHistory: new XataChatMessageHistory({
table: "messages",
sessionId: new Date().toISOString(), // Or some other unique identifier for the conversation
client: getXataClient(),
apiKey: process.env.XATA_API_KEY, // The API key is needed for creating the table.
}),
});

const model = new ChatOpenAI();
const chain = new ConversationChain({ llm: model, memory });

const res1 = await chain.invoke({ input: "Hi! I'm Jim." });
console.log({ res1 });
/*
{
res1: {
text: "Hello Jim! It's nice to meet you. My name is AI. How may I assist you today?"
}
}
*/

const res2 = await chain.invoke({ input: "What did I just say my name was?" });
console.log({ res2 });

/*
{
res1: {
text: "You said your name was Jim."
}
}
*/

API 参考

使用预先创建的表

如果你不希望代码始终检查表是否存在,你可以在 Xata UI 中手动创建表,并将 createTable: false 传递给构造函数。该表必须具有以下列

  • 类型为 StringsessionId
  • 类型为 Stringtype
  • 类型为 Stringrole
  • 类型为 Textcontent
  • 类型为 Stringname
  • 类型为 TextadditionalKwargs
import { BufferMemory } from "langchain/memory";
import { ChatOpenAI } from "@langchain/openai";
import { ConversationChain } from "langchain/chains";
import { XataChatMessageHistory } from "@langchain/community/stores/message/xata";
import { BaseClient } from "@xata.io/client";

// Before running this example, see the docs at
// https://js.langchain.ac.cn/docs/modules/memory/integrations/xata

// if you use the generated client, you don't need this function.
// Just import getXataClient from the generated xata.ts instead.
const getXataClient = () => {
if (!process.env.XATA_API_KEY) {
throw new Error("XATA_API_KEY not set");
}

if (!process.env.XATA_DB_URL) {
throw new Error("XATA_DB_URL not set");
}
const xata = new BaseClient({
databaseURL: process.env.XATA_DB_URL,
apiKey: process.env.XATA_API_KEY,
branch: process.env.XATA_BRANCH || "main",
});
return xata;
};

const memory = new BufferMemory({
chatHistory: new XataChatMessageHistory({
table: "messages",
sessionId: new Date().toISOString(), // Or some other unique identifier for the conversation
client: getXataClient(),
createTable: false, // Explicitly set to false if the table is already created
}),
});

const model = new ChatOpenAI();
const chain = new ConversationChain({ llm: model, memory });

const res1 = await chain.invoke({ input: "Hi! I'm Jim." });
console.log({ res1 });
/*
{
res1: {
text: "Hello Jim! It's nice to meet you. My name is AI. How may I assist you today?"
}
}
*/

const res2 = await chain.invoke({ input: "What did I just say my name was?" });
console.log({ res2 });

/*
{
res1: {
text: "You said your name was Jim."
}
}
*/

API 参考


此页内容是否对您有帮助?


您也可以留下详细的反馈 在 GitHub 上.