Aurora DSQL 聊天内存
¥Aurora DSQL Chat Memory
为了在聊天会话中实现更长期的持久化,你可以将默认的内存中 chatHistory 替换为与 PostgreSQL 兼容的无服务器 Amazon Aurora DSQL 数据库。
¥For longer-term persistence across chat sessions, you can swap out the default in-memory chatHistory for the serverless PostgreSQL-compatible Amazon Aurora DSQL Database.
这与 PostgreSQL 集成非常相似,但有一些区别,以使其与 DSQL 兼容:
¥This is very similar to the PostgreSQL integration with a few differences to make it compatible with DSQL:
PostgreSQL 中的
id列是 SERIAL 自增类型,DSQL 是使用数据库函数gen_random_uuid的 UUID 格式。¥The
idcolumn in PostgreSQL is SERIAL auto-incrementent, and DSQL is UUID using the database functiongen_random_uuid.创建
created_at列来跟踪消息的顺序和历史记录。¥A
created_atcolumn is created to track the order and history of the messages.PostgreSQL 中的
message列是 JSONB 格式,DSQL 是经过 JavaScript 解析处理的 TEXT 格式。¥The
messagecolumn in PostgreSQL is JSONB, and DSQL is TEXT with Javascript parsing handling
设置
¥Setup
转到你的 AWS 控制台并创建一个 Aurora DSQL 集群 https://console.aws.amazon.com/dsql/clusters
¥Go to you AWS Console and create an Aurora DSQL Cluster, 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
用法
¥Usage
每个聊天历史记录会话都存储在 Aurora DSQL(兼容 Postgres)数据库中,并且需要一个会话 ID。
¥Each chat history session is stored in a Aurora DSQL (Postgres-compatible) database and requires a session id.
与 Aurora DSQL 的连接通过 PostgreSQL 池处理。你可以通过 pool 参数传递一个池实例,也可以通过 poolConfig 参数传递一个池配置。有关更多信息,请参阅 pg-node 池文档。提供的池优先,因此,如果同时传递了池实例和池配置,则只会使用池。
¥The connection to Aurora DSQL is handled through a PostgreSQL pool. You can either pass an instance of a pool via the pool parameter or pass a pool config via the poolConfig parameter. See pg-node docs on pools
for more information. A provided pool takes precedence, thus if both a pool instance and a pool config are passed, only the pool will be used.
有关常规安全最佳实践的更多信息,请参阅我们的 https://docs.aws.amazon.com/aurora-dsql/latest/userguide/authentication-authorization.html。
¥For options on how to do the authentication and authorization for DSQL please check https://docs.aws.amazon.com/aurora-dsql/latest/userguide/authentication-authorization.html.
以下示例使用 AWS-SDK 生成传递给池配置的身份验证令牌:
¥The following example uses the AWS-SDK to generate an authentication token that is passed to the pool configuration:
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 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