Aurora DSQL 聊天记忆
为了在聊天会话之间实现更长期的持久化,您可以将默认的内存中 chatHistory 替换为无服务器且兼容 PostgreSQL 的 Amazon Aurora DSQL 数据库。
这与 PostgreSQL 的集成非常相似,但有一些差异以使其兼容 DSQL:
- PostgreSQL 中的
id列是 SERIAL 自增列,而 DSQL 中使用的是 UUID,并通过数据库函数gen_random_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(兼容 PostgreSQL)数据库中,并需要一个会话 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({
model: "gpt-4o-mini",
});
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 Reference:
- AuroraDsqlChatMessageHistory from
@langchain/community/stores/message/aurora_dsql - ChatOpenAI from
@langchain/openai - RunnableWithMessageHistory from
@langchain/core/runnables - ChatPromptTemplate from
@langchain/core/prompts - MessagesPlaceholder from
@langchain/core/prompts - StringOutputParser from
@langchain/core/output_parsers