Aurora DSQL 聊天记忆
为了在聊天会话中实现更长期的持久性,您可以将默认的内存中 chatHistory
替换为无服务器的 PostgreSQL 兼容的 Amazon Aurora DSQL 数据库。
这与 PostgreSQL 集成非常相似,但有一些差异使其与 DSQL 兼容
- PostgreSQL 中的
id
列是 SERIAL 自动递增的,而 DSQL 是使用数据库函数gen_random_uuid
的 UUID。 - 创建了
created_at
列来跟踪消息的顺序和历史记录。 - PostgreSQL 中的
message
列是 JSONB,而 DSQL 是 TEXT,使用 Javascript 解析处理
设置
转到您的 AWS 控制台并创建一个 Aurora DSQL 集群,https://console.aws.amazon.com/dsql/clusters
提示
有关安装集成包的通用说明,请参阅此部分。
- npm
- Yarn
- pnpm
npm install @langchain/openai @langchain/community @langchain/core pg @aws-sdk/dsql-signer
yarn add @langchain/openai @langchain/community @langchain/core pg @aws-sdk/dsql-signer
pnpm add @langchain/openai @langchain/community @langchain/core pg @aws-sdk/dsql-signer
用法
每个聊天历史会话都存储在 Aurora DSQL(Postgres 兼容)数据库中,并且需要会话 ID。
与 Aurora DSQL 的连接通过 PostgreSQL 连接池处理。您可以通过 pool
参数传递连接池的实例,也可以通过 poolConfig
参数传递连接池配置。有关更多信息,请参阅 pg-node 关于连接池的文档。提供的连接池优先,因此,如果同时传递了连接池实例和连接池配置,则仅使用连接池。
有关如何为 DSQL 进行身份验证和授权的选项,请查看 https://docs.aws.amazon.com/aurora-dsql/latest/userguide/authentication-authorization.html。
以下示例使用 AWS-SDK 生成身份验证令牌,该令牌将传递给连接池配置
import pg from "pg";
import { DsqlSigner } from "@aws-sdk/dsql-signer";
import { AuroraDsqlChatMessageHistory } from "@langchain/community/stores/message/aurora_dsql";
import { ChatOpenAI } from "@langchain/openai";
import { RunnableWithMessageHistory } from "@langchain/core/runnables";
import {
ChatPromptTemplate,
MessagesPlaceholder,
} from "@langchain/core/prompts";
import { StringOutputParser } from "@langchain/core/output_parsers";
async function getPostgresqlPool() {
const signer = new DsqlSigner({
hostname: process.env.DSQL_ENDPOINT!,
});
const token = await signer.getDbConnectAdminAuthToken();
if (!token) throw new Error("Auth token error for DSQL");
const poolConfig: pg.PoolConfig = {
host: process.env.DSQL_ENDPOINT,
port: 5432,
user: "admin",
password: token,
ssl: true,
database: "postgres",
};
const pool = new pg.Pool(poolConfig);
return pool;
}
const pool = await getPostgresqlPool();
const model = new ChatOpenAI();
const prompt = ChatPromptTemplate.fromMessages([
[
"system",
"You are a helpful assistant. Answer all questions to the best of your ability.",
],
new MessagesPlaceholder("chat_history"),
["human", "{input}"],
]);
const chain = prompt.pipe(model).pipe(new StringOutputParser());
const chainWithHistory = new RunnableWithMessageHistory({
runnable: chain,
inputMessagesKey: "input",
historyMessagesKey: "chat_history",
getMessageHistory: async (sessionId) => {
const chatHistory = new AuroraDsqlChatMessageHistory({
sessionId,
pool,
// Can also pass `poolConfig` to initialize the pool internally,
// but easier to call `.end()` at the end later.
});
return chatHistory;
},
});
const res1 = await chainWithHistory.invoke(
{
input: "Hi! I'm MJDeligan.",
},
{ configurable: { sessionId: "langchain-test-session" } }
);
console.log(res1);
/*
"Hello MJDeligan! It's nice to meet you. My name is AI. How may I assist you today?"
*/
const res2 = await chainWithHistory.invoke(
{ input: "What did I just say my name was?" },
{ configurable: { sessionId: "langchain-test-session" } }
);
console.log(res2);
/*
"You said your name was MJDeligan."
*/
// If you provided a pool config you should close the created pool when you are done
await pool.end();
API 参考
- AuroraDsqlChatMessageHistory 来自
@langchain/community/stores/message/aurora_dsql
- ChatOpenAI 来自
@langchain/openai
- RunnableWithMessageHistory 来自
@langchain/core/runnables
- ChatPromptTemplate 来自
@langchain/core/prompts
- MessagesPlaceholder 来自
@langchain/core/prompts
- StringOutputParser 来自
@langchain/core/output_parsers