Postgres 聊天内存
¥Postgres Chat Memory
为了在聊天会话中实现更长期的持久化,你可以将默认的内存中 chatHistory 替换为 Postgres 数据库。
¥For longer-term persistence across chat sessions, you can swap out the default in-memory chatHistory for a Postgres Database.
设置
¥Setup
首先安装 node-postgres 包:
¥First install the node-postgres package:
- npm
- Yarn
- pnpm
npm install @langchain/openai @langchain/community @langchain/core pg
yarn add @langchain/openai @langchain/community @langchain/core pg
pnpm add @langchain/openai @langchain/community @langchain/core pg
用法
¥Usage
每个聊天历史记录会话都存储在 Postgres 数据库中,并且需要一个会话 ID。
¥Each chat history session is stored in a Postgres database and requires a session id.
与 Postgres 的连接通过池处理。你可以通过 pool 参数传递一个池实例,也可以通过 poolConfig 参数传递一个池配置。有关更多信息,请参阅 pg-node 池文档。提供的池优先,因此,如果同时传递了池实例和池配置,则只会使用池。
¥The connection to postgres is handled through a 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.
import pg from "pg";
import { PostgresChatMessageHistory } from "@langchain/community/stores/message/postgres";
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";
const poolConfig = {
host: "127.0.0.1",
port: 5432,
user: "myuser",
password: "ChangeMe",
database: "api",
};
const pool = new pg.Pool(poolConfig);
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 PostgresChatMessageHistory({
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:
- PostgresChatMessageHistory from
@langchain/community/stores/message/postgres - 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