XataChatMessageHistory
class, you can use Xata databases for longer-term persistence of chat sessions.
Because Xata works via a REST API and has a pure TypeScript SDK, you can use this with Vercel Edge, Cloudflare Workers and any other Serverless environment.
Setup
Install the Xata CLI
Create a database to be used as a vector store
In the Xata UI create a new database. You can name it whatever you want, but for this example we’ll uselangchain
.
When executed for the first time, the Xata LangChain integration will create the table used for storing the chat messages. If a table with that name already exists, it will be left untouched.
Initialize the project
In your project, run:xata.ts
or xata.js
file that defines the client you can use to interact with the database. See the Xata getting started docs for more details on using the Xata JavaScript/TypeScript SDK.
Usage
Each chat history session stored in Xata database must have a unique id. In this example, thegetXataClient()
function is used to create a new Xata client based on the environment variables. However, we recommend using the code generated by the xata init
command, in which case you only need to import the getXataClient()
function from the generated xata.ts
file.
npm
With pre-created table
If you don’t want the code to always check if the table exists, you can create the table manually in the Xata UI and passcreateTable: false
to the constructor. The table must have the following columns:
sessionId
of typeString
type
of typeString
role
of typeString
content
of typeText
name
of typeString
additionalKwargs
of typeText