Skip to content

Commit

Permalink
Adds ZepMemory and ZepRetriever (#1450)
Browse files Browse the repository at this point in the history
* 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
jacoblee93 and rsharath authored May 30, 2023
1 parent cd69ebf commit c39c434
Show file tree
Hide file tree
Showing 14 changed files with 420 additions and 2 deletions.
20 changes: 20 additions & 0 deletions docs/docs/modules/indexes/retrievers/zep-retriever.mdx
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>
26 changes: 26 additions & 0 deletions docs/docs/modules/memory/examples/zep_memory.mdx
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>
1 change: 1 addition & 0 deletions examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"dependencies": {
"@clickhouse/client": "^0.0.14",
"@getmetal/metal-sdk": "^4.0.0",
"@getzep/zep-js": "0.3.1",
"@gomomento/sdk": "^1.23.0",
"@opensearch-project/opensearch": "^2.2.0",
"@pinecone-database/pinecone": "^0.0.14",
Expand Down
39 changes: 39 additions & 0 deletions examples/src/memory/zep.ts
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."
}
}
*/
14 changes: 14 additions & 0 deletions examples/src/retrievers/zep.ts
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);
};
6 changes: 6 additions & 0 deletions langchain/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@ text_splitter.d.ts
memory.cjs
memory.js
memory.d.ts
memory/zep.cjs
memory/zep.js
memory/zep.d.ts
document.cjs
document.js
document.d.ts
Expand Down Expand Up @@ -274,6 +277,9 @@ retrievers/remote.d.ts
retrievers/supabase.cjs
retrievers/supabase.js
retrievers/supabase.d.ts
retrievers/zep.cjs
retrievers/zep.js
retrievers/zep.d.ts
retrievers/metal.cjs
retrievers/metal.js
retrievers/metal.d.ts
Expand Down
21 changes: 21 additions & 0 deletions langchain/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,9 @@
"memory.cjs",
"memory.js",
"memory.d.ts",
"memory/zep.cjs",
"memory/zep.js",
"memory/zep.d.ts",
"document.cjs",
"document.js",
"document.d.ts",
Expand Down Expand Up @@ -286,6 +289,9 @@
"retrievers/supabase.cjs",
"retrievers/supabase.js",
"retrievers/supabase.d.ts",
"retrievers/zep.cjs",
"retrievers/zep.js",
"retrievers/zep.d.ts",
"retrievers/metal.cjs",
"retrievers/metal.js",
"retrievers/metal.d.ts",
Expand Down Expand Up @@ -385,6 +391,7 @@
"@clickhouse/client": "^0.0.14",
"@faker-js/faker": "^7.6.0",
"@getmetal/metal-sdk": "^4.0.0",
"@getzep/zep-js": "^0.3.1",
"@gomomento/sdk": "^1.23.0",
"@huggingface/inference": "^1.5.1",
"@jest/globals": "^29.5.0",
Expand Down Expand Up @@ -456,6 +463,7 @@
"@aws-sdk/client-sagemaker-runtime": "^3.310.0",
"@clickhouse/client": "^0.0.14",
"@getmetal/metal-sdk": "*",
"@getzep/zep-js": "^0.3.1",
"@gomomento/sdk": "^1.23.0",
"@huggingface/inference": "^1.5.1",
"@opensearch-project/opensearch": "*",
Expand Down Expand Up @@ -512,6 +520,9 @@
"@getmetal/metal-sdk": {
"optional": true
},
"@getzep/zep-js": {
"optional": true
},
"@gomomento/sdk": {
"optional": true
},
Expand Down Expand Up @@ -913,6 +924,11 @@
"import": "./memory.js",
"require": "./memory.cjs"
},
"./memory/zep": {
"types": "./memory/zep.d.ts",
"import": "./memory/zep.js",
"require": "./memory/zep.cjs"
},
"./document": {
"types": "./document.d.ts",
"import": "./document.js",
Expand Down Expand Up @@ -1134,6 +1150,11 @@
"import": "./retrievers/supabase.js",
"require": "./retrievers/supabase.cjs"
},
"./retrievers/zep": {
"types": "./retrievers/zep.d.ts",
"import": "./retrievers/zep.js",
"require": "./retrievers/zep.cjs"
},
"./retrievers/metal": {
"types": "./retrievers/metal.d.ts",
"import": "./retrievers/metal.js",
Expand Down
4 changes: 4 additions & 0 deletions langchain/scripts/create-entrypoints.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ const entrypoints = {
text_splitter: "text_splitter",
// memory
memory: "memory/index",
"memory/zep": "memory/zep",
// document
document: "document",
// docstore
Expand Down Expand Up @@ -119,6 +120,7 @@ const entrypoints = {
retrievers: "retrievers/index",
"retrievers/remote": "retrievers/remote/index",
"retrievers/supabase": "retrievers/supabase",
"retrievers/zep": "retrievers/zep",
"retrievers/metal": "retrievers/metal",
"retrievers/databerry": "retrievers/databerry",
"retrievers/contextual_compression": "retrievers/contextual_compression",
Expand Down Expand Up @@ -190,6 +192,7 @@ const requiresOptionalDependency = [
"vectorstores/myscale",
"vectorstores/redis",
"vectorstores/tigris",
"memory/zep",
"document_loaders/web/apify_dataset",
"document_loaders/web/cheerio",
"document_loaders/web/puppeteer",
Expand Down Expand Up @@ -217,6 +220,7 @@ const requiresOptionalDependency = [
"chat_models/googlevertexai",
"sql_db",
"retrievers/supabase",
"retrievers/zep",
"retrievers/metal",
"retrievers/self_query",
"output_parsers/expression",
Expand Down
44 changes: 44 additions & 0 deletions langchain/src/memory/tests/zep_memory.int.test.ts
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");
}
});
Loading

1 comment on commit c39c434

@vercel
Copy link

@vercel vercel bot commented on c39c434 May 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.