forked from langchain-ai/langchainjs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add Momento as a standard cache and chat history provider (lang…
…chain-ai#1416) * chore: add .node-version to gitignore Because I'm Using nodenv with multiple node versions installed, we use the node local to specify the version to use in a particular repo. * feat: add @gomomento/sdk as a dev, peer, optional dependency * feat: add Momento as a standard caching provider * feat: add Momento as a chat message history store * chore: add momento to entrypoints * docs: add examples and docs for Momento * chore: make fields readonly We make appropriate fields readonly. * refactor: use factory method standard naming fromX * refactor: extract `ensureCacheExists` to utility module Previously both the Momento cache module and Momento chat history modules had a `ensureCacheExists` function, which were identical. This commit extracts the function to a single place in a utility module. * Docs fixes --------- Co-authored-by: jacoblee93 <[email protected]>
- Loading branch information
1 parent
b8afb1e
commit c5fa479
Showing
17 changed files
with
945 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,3 +27,4 @@ Chinook_Sqlite.sql | |
*.swp | ||
*.swo | ||
|
||
.node-version |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
--- | ||
hide_table_of_contents: true | ||
--- | ||
|
||
import CodeBlock from "@theme/CodeBlock"; | ||
|
||
# Momento-Backed Chat Memory | ||
|
||
For distributed, serverless persistence across chat sessions, you can swap in a [Momento](https://gomomento.com/)-backed chat message history. | ||
Because a Momento cache is instantly available and requires zero infrastructure maintenance, it's a great way to get started with chat history whether building locally or in production. | ||
|
||
## Setup | ||
|
||
You will need to install the [Momento Client Library](https://github.com/momentohq/client-sdk-javascript) in your project: | ||
|
||
```bash npm2yarn | ||
npm install @gomomento/sdk | ||
``` | ||
|
||
You will also need an API key from [Momento](https://gomomento.com/). You can sign up for a free account [here](https://console.gomomento.com/). | ||
|
||
## Usage | ||
|
||
To distinguish one chat history session from another, we need a unique `sessionId`. You may also provide an optional `sessionTtl` to make sessions expire after a given number of seconds. | ||
|
||
import MomentoExample from "@examples/memory/momento.ts"; | ||
|
||
<CodeBlock language="typescript">{MomentoExample}</CodeBlock> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { OpenAI } from "langchain/llms/openai"; | ||
import { MomentoCache } from "langchain/cache/momento"; | ||
import { | ||
CacheClient, | ||
Configurations, | ||
CredentialProvider, | ||
} from "@gomomento/sdk"; | ||
|
||
// See https://github.com/momentohq/client-sdk-javascript for connection options | ||
const client = new CacheClient({ | ||
configuration: Configurations.Laptop.v1(), | ||
credentialProvider: CredentialProvider.fromEnvironmentVariable({ | ||
environmentVariableName: "MOMENTO_AUTH_TOKEN", | ||
}), | ||
defaultTtlSeconds: 60 * 60 * 24, | ||
}); | ||
const cache = await MomentoCache.fromProps({ | ||
client, | ||
cacheName: "langchain", | ||
}); | ||
|
||
const model = new OpenAI({ cache }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { | ||
CacheClient, | ||
Configurations, | ||
CredentialProvider, | ||
} from "@gomomento/sdk"; | ||
import { BufferMemory } from "langchain/memory"; | ||
import { ChatOpenAI } from "langchain/chat_models/openai"; | ||
import { ConversationChain } from "langchain/chains"; | ||
import { MomentoChatMessageHistory } from "langchain/stores/message/momento"; | ||
|
||
// See https://github.com/momentohq/client-sdk-javascript for connection options | ||
const client = new CacheClient({ | ||
configuration: Configurations.Laptop.v1(), | ||
credentialProvider: CredentialProvider.fromEnvironmentVariable({ | ||
environmentVariableName: "MOMENTO_AUTH_TOKEN", | ||
}), | ||
defaultTtlSeconds: 60 * 60 * 24, | ||
}); | ||
|
||
// Create a unique session ID | ||
const sessionId = new Date().toISOString(); | ||
const cacheName = "langchain"; | ||
|
||
const memory = new BufferMemory({ | ||
chatHistory: await MomentoChatMessageHistory.fromProps({ | ||
client, | ||
cacheName, | ||
sessionId, | ||
sessionTtl: 300, | ||
}), | ||
}); | ||
console.log( | ||
`cacheName=${cacheName} and sessionId=${sessionId} . This will be used to store the chat history. You can inspect the values at your Momento console at https://console.gomomento.com.` | ||
); | ||
|
||
const model = new ChatOpenAI({ | ||
modelName: "gpt-3.5-turbo", | ||
temperature: 0, | ||
}); | ||
|
||
const chain = new ConversationChain({ llm: model, memory }); | ||
|
||
const res1 = await chain.call({ input: "Hi! I'm Jim." }); | ||
console.log({ res1 }); | ||
/* | ||
{ | ||
res1: { | ||
text: "Hello Jim! It's nice to meet you. My name is AI. How may I assist you today?" | ||
} | ||
} | ||
*/ | ||
|
||
const res2 = await chain.call({ input: "What did I just say my name was?" }); | ||
console.log({ res2 }); | ||
|
||
/* | ||
{ | ||
res1: { | ||
text: "You said your name was Jim." | ||
} | ||
} | ||
*/ | ||
|
||
// See the chat history in the Momento | ||
console.log(await memory.chatHistory.getMessages()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.