-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds ZepMemory and ZepRetriever (#1450)
* Zep Integration for Memory, Retriever, Docs + Tests * Adding tests for retriever, zep_memory impl * updated yarn.lock * updated * updated yarn.lock * updated * updated with PR changes * minor updates * Removed zep from docs/package, ^ver on langchain/package.json, updated example * few edits to make sure memory is properly added * reverting unnecessary package changes * added memory * bump js sdk ver to 0.3.0 * Updated Integration tests and bumped sdk ver * Updated retriever example, zepMemory/zepConvBufferMemory to zep_memory.ts * Adding 2 examples, removed unnecessary console.log * Removed Zep Memory Backed Chat from example & doc * removed option-2 implementation completely * Change entrypoint to memory/zep * Typo fix * Standardize parameters * Change entrypoint to fix docs * Fix Zep session loading * Reduce yarn.lock diff --------- Co-authored-by: Sharath Rajasekar <sharathr@gmail.com>
- Loading branch information
1 parent
cd69ebf
commit c39c434
Showing
14 changed files
with
420 additions
and
2 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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
--- | ||
hide_table_of_contents: true | ||
--- | ||
|
||
# Zep Retriever | ||
|
||
This example shows how to use the Zep Retriever in a `RetrievalQAChain` to retrieve documents from Zep memory store. | ||
|
||
## Setup | ||
|
||
```bash npm2yarn | ||
npm i @getzep/zep-js | ||
``` | ||
|
||
## Usage | ||
|
||
import CodeBlock from "@theme/CodeBlock"; | ||
import Example from "@examples/retrievers/zep.ts"; | ||
|
||
<CodeBlock language="typescript">{Example}</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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
--- | ||
hide_table_of_contents: true | ||
--- | ||
|
||
# Zep Memory | ||
|
||
[Zep](https://github.com/getzep/zep) is a memory server that stores, summarizes, embeds, indexes, and enriches conversational AI chat histories, autonomous agent histories, document Q&A histories and exposes them via simple, low-latency APIs. | ||
|
||
Key Features: | ||
|
||
- Long-term memory persistence, with access to historical messages irrespective of your summarization strategy. | ||
- Auto-summarization of memory messages based on a configurable message window. A series of summaries are stored, providing flexibility for future summarization strategies. | ||
- Vector search over memories, with messages automatically embedded on creation. | ||
- Auto-token counting of memories and summaries, allowing finer-grained control over prompt assembly. | ||
- [Python](https://github.com/getzep/zep-python) and [JavaScript](https://github.com/getzep/zep-js) SDKs. | ||
|
||
## Setup | ||
|
||
See the instructions from [Zep](https://github.com/getzep/zep) for running the server locally or through an automated hosting provider. | ||
|
||
## Usage | ||
|
||
import CodeBlock from "@theme/CodeBlock"; | ||
import Example from "@examples/memory/zep.ts"; | ||
|
||
<CodeBlock language="typescript">{Example}</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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { ChatOpenAI } from "langchain/chat_models/openai"; | ||
import { ConversationChain } from "langchain/chains"; | ||
import { ZepMemory } from "langchain/memory/zep"; | ||
|
||
const sessionId = "TestSession1234"; | ||
const zepURL = "http://localhost:8000"; | ||
|
||
const memory = new ZepMemory({ | ||
sessionId, | ||
baseURL: zepURL, | ||
}); | ||
|
||
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." | ||
} | ||
} | ||
*/ |
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,14 @@ | ||
import { ZepRetriever } from "langchain/retrievers/zep"; | ||
|
||
export const run = async () => { | ||
const url = process.env.ZEP_URL || "http://localhost:8000"; | ||
const sessionId = "TestSession1232"; | ||
console.log(`Session ID: ${sessionId}, URL: ${url}`); | ||
|
||
const retriever = new ZepRetriever({ sessionId, url }); | ||
|
||
const query = "hello"; | ||
const docs = await retriever.getRelevantDocuments(query); | ||
|
||
console.log(docs); | ||
}; |
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,44 @@ | ||
import { expect, test } from "@jest/globals"; | ||
import { v4 as uuid } from "uuid"; | ||
import { ZepMemory } from "../zep.js"; | ||
|
||
const sessionId = uuid(); | ||
const baseURL = "http://localhost:8000"; | ||
const zepMemory = new ZepMemory({ sessionId, baseURL }); | ||
|
||
beforeEach((done) => { | ||
setTimeout(done, 1000); // 1-second delay before each test case | ||
}); | ||
|
||
test("addMemory to Zep memory", async () => { | ||
await zepMemory.saveContext( | ||
{ input: "Who was Octavia Butler?" }, | ||
{ | ||
response: | ||
"Octavia Estelle Butler (June 22, 1947 β " + | ||
"February 24, 2006) was an American science fiction author.", | ||
} | ||
); | ||
}); | ||
|
||
test("getMessages from Zep memory", async () => { | ||
const memoryVariables = await zepMemory.loadMemoryVariables({}); | ||
console.log("memoryVariables", memoryVariables); | ||
|
||
// Check if memoryKey exists in the memoryVariables | ||
expect(memoryVariables).toHaveProperty(zepMemory.memoryKey); | ||
|
||
const messages = memoryVariables[zepMemory.memoryKey]; | ||
|
||
// Check if messages is an array or string | ||
if (typeof messages === "string") { | ||
// In this case, we can at least expect a non-empty string. | ||
expect(messages.length).toBeGreaterThanOrEqual(1); | ||
} else if (Array.isArray(messages)) { | ||
expect(messages.length).toBeGreaterThanOrEqual(1); | ||
} else { | ||
console.log("failed to get messages: ", messages); | ||
// Fail the test because messages is neither string nor array | ||
throw new Error("Returned messages is neither string nor array"); | ||
} | ||
}); |
Oops, something went wrong.
c39c434
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
langchainjs-docs β ./
langchainjs-docs-langchain.vercel.app
langchainjs-docs-ruddy.vercel.app
langchainjs-docs-git-main-langchain.vercel.app
js.langchain.com