Skip to content

Commit

Permalink
docs: Add document for AzureCosmosDBMongoChatMessageHistory (#7519)
Browse files Browse the repository at this point in the history
Co-authored-by: root <root@CPC-yangq-FRSGK>
  • Loading branch information
fatmelon and root authored Jan 22, 2025
1 parent fe37032 commit a9298a6
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
---
hide_table_of_contents: true
---

import CodeBlock from "@theme/CodeBlock";

# Azure Cosmos DB Mongo vCore Chat Message History

The AzureCosmosDBMongoChatMessageHistory uses Azure Cosmos DB Mongo vCore to store chat message history. For longer-term persistence across chat sessions, you can swap out the default in-memory `chatHistory` that backs chat memory classes like `BufferMemory`.
If you don't have an Azure account, you can [create a free account](https://azure.microsoft.com/free/) to get started.

## Setup

You'll first need to install the [`@langchain/azure-cosmosdb`](https://www.npmjs.com/package/@langchain/azure-cosmosdb) package:

```bash npm2yarn
npm install @langchain/azure-cosmosdb @langchain/core
```

import IntegrationInstallTooltip from "@mdx_components/integration_install_tooltip.mdx";

<IntegrationInstallTooltip></IntegrationInstallTooltip>

```bash npm2yarn
npm install @langchain/openai @langchain/community @langchain/core
```

You'll also need to have an Azure Cosmos DB mongo vCore instance running. You can deploy a free version on Azure Portal without any cost, following [this guide](https://learn.microsoft.com/en-us/azure/cosmos-db/mongodb/vcore/quickstart-portal).

Once you have your instance running, make sure you have the connection string.

## Usage

import Example from "@examples/memory/azure_cosmosdb_mongo.ts";

<CodeBlock language="typescript">{Example}</CodeBlock>
14 changes: 14 additions & 0 deletions docs/core_docs/docs/integrations/platforms/microsoft.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,20 @@ See [usage example](/docs/integrations/memory/azure_cosmosdb_nosql.mdx).
import { AzureCosmosDBNoSQLChatMessageHistory } from "@langchain/azure-cosmosdb";
```

### Azure Cosmos DB MongoDB vCore Chat Message History

> The AzureCosmosDBMongoChatMessageHistory uses Cosmos DB Mongo vCore to store chat message history. For longer-term persistence across chat sessions, you can swap out the default in-memory `chatHistory` that backs chat memory classes like `BufferMemory`.
```bash npm2yarn
npm install @langchain/azure-cosmosdb @langchain/core
```

See a [usage example](/docs/integrations/memory/azure_cosmos_mongo_vcore.mdx).

```typescript
import { AzureCosmosDBMongoChatMessageHistory } from "@langchain/azure-cosmosdb";
```

## Document loaders

### Azure Blob Storage
Expand Down
86 changes: 86 additions & 0 deletions examples/src/memory/azure_cosmosdb_mongo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import { ChatOpenAI } from "@langchain/openai";
import {
AzureCosmosDBMongoChatMessageHistory,
AzureCosmosDBMongoChatHistoryDBConfig,
} from "@langchain/azure-cosmosdb";
import { RunnableWithMessageHistory } from "@langchain/core/runnables";
import { StringOutputParser } from "@langchain/core/output_parsers";
import {
ChatPromptTemplate,
MessagesPlaceholder,
} from "@langchain/core/prompts";

const model = new ChatOpenAI({
model: "gpt-3.5-turbo",
temperature: 0,
});

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 dbcfg: AzureCosmosDBMongoChatHistoryDBConfig = {
connectionString: process.env.AZURE_COSMOSDB_MONGODB_CONNECTION_STRING,
databaseName: "langchain",
collectionName: "chathistory",
};

const chainWithHistory = new RunnableWithMessageHistory({
runnable: chain,
inputMessagesKey: "input",
historyMessagesKey: "chat_history",
getMessageHistory: async (sessionId) => {
const chatHistory = new AzureCosmosDBMongoChatMessageHistory(
dbcfg,
sessionId,
"user-id"
);
return chatHistory;
},
});

const res1 = await chainWithHistory.invoke(
{ input: "Hi! I'm Jim." },
{ configurable: { sessionId: "langchain-test-session" } }
);
console.log({ res1 });
/*
{ res1: 'Hi Jim! How can 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 });
/*
{ res2: { response: 'You said your name was Jim.' }
*/

// Give this session a title
const chatHistory = (await chainWithHistory.getMessageHistory(
"langchain-test-session"
)) as AzureCosmosDBMongoChatMessageHistory;

await chatHistory.setContext({ title: "Introducing Jim" });

// List all session for the user
const sessions = await chatHistory.getAllSessions();

console.log(sessions);
/*
[
{
id: 'langchain-test-session',
user_id: 'user-id',
context: { title: 'Introducing Jim' }
}
]
*/

0 comments on commit a9298a6

Please sign in to comment.