Skip to content

Commit

Permalink
Merge pull request #43 from getzep/v2-integration
Browse files Browse the repository at this point in the history
feat: Add support for v2 api
  • Loading branch information
paul-paliychuk authored Feb 8, 2024
2 parents f4d2faa + e6e52fb commit 8829cf0
Show file tree
Hide file tree
Showing 34 changed files with 1,674 additions and 7,165 deletions.
4 changes: 3 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,12 @@
"max-classes-per-file": 0,
"multiline-ternary": 0,
"no-unused-vars": "off",
"no-underscore-dangle": "off",
"no-shadow": 0,
"@typescript-eslint/no-unused-vars": "error",
"no-useless-constructor": "off",
"@typescript-eslint/no-useless-constructor": "error",
"import/prefer-default-export": "off",
"import/extensions": [
"error",
"ignorePackages",
Expand All @@ -61,7 +63,7 @@
{
"code": 100,
"tabWidth": 3,
"comments": 100,
"comments": 150,
"ignoreComments": false,
"ignoreTrailingComments": true,
"ignoreUrls": true,
Expand Down
8 changes: 4 additions & 4 deletions .github/workflows/doc-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@ jobs:
uses: actions/setup-node@v3
with:
node-version: 20
cache: 'npm'
cache: 'yarn'

- name: Install Dependencies
run: npm ci
run: yarn install --frozen-lockfile

- name: Build Project
run: npm run build
run: yarn build

- name: Install TypeDoc
run: npm install typedoc typedoc-plugin-markdown
run: yarn add typedoc typedoc-plugin-markdown

- name: Generate Documentation
run: npx typedoc --out docs --excludePrivate --excludeInternal --excludeExternals src/
Expand Down
8 changes: 4 additions & 4 deletions .github/workflows/node-build-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
- run: npm ci
- run: npm run build --if-present
- run: npm test
cache: 'yarn'
- run: yarn install --frozen-lockfile
- run: yarn build
- run: yarn test
14 changes: 7 additions & 7 deletions .github/workflows/npm-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ jobs:
node-version: '18.x'
registry-url: 'https://registry.npmjs.org'
- name: Install dependencies
run: npm ci
- name: Publish package release
if: "!github.event.release.prerelease"
run: npm publish
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
run: yarn install --frozen-lockfile
# - name: Publish package release
# if: "!github.event.release.prerelease"
# run: yarn publish
# env:
# NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
- name: Publish package pre-release
if: "github.event.release.prerelease"
run: npm publish --tag next
run: yarn publish --tag next
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,31 @@ This is the TypeScript / JavaScript client package for the Zep service. For more
npm install @getzep/zep-js
```

## Zep cloud Installation
In order to install zep js sdk with zep-cloud support, you will need to install
a pre-release version tagged with `@next`.

```bash
npm install @getzep/zep-js@next
```

You will also need to provide a Zep Project API key to your zep client for cloud support.
You can find out about zep projects in our [cloud docs](https://help.getzep.com/projects.html)

### Using langchain zep classes with `zep-js@next`:
In the pre-release version `zep-js` sdk comes with `ZepChatMessageHistory` and `ZepVectorStore`
classes that are compatible with [`Langchain's JS expression language`](https://js.langchain.com/docs/expression_language/)

In order to use these classes in your application, you need to make sure that you have
`@langchain/core` package installed:

```bash
npm install @langchain/core@^0.1.23
```

You can import these classes in the following way:

```typescript
import { ZepChatMessageHistory, ZepVectorStore } from "@getzep/zep-js/langchain"
```

4 changes: 2 additions & 2 deletions __tests__/setup/setupJest.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
//setupJest.js or similar file
require('jest-fetch-mock').enableMocks()
// setupJest.js or similar file
require("jest-fetch-mock").enableMocks();
42 changes: 21 additions & 21 deletions examples/documents/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ import { Document, IDocument, ISearchQuery, ZepClient } from "../../src";
*/
async function checkEmbeddingStatus(
client: ZepClient,
collectionName: string
collectionName: string,
): Promise<void> {
let c = await client.document.getCollection(collectionName);

while (c.status !== "ready") {
console.log(
`Embedding status: ${c.document_embedded_count}/${c.document_count} documents embedded`
`Embedding status: ${c.document_embedded_count}/${c.document_count} documents embedded`,
);
// Wait for 1 second
await new Promise((resolve) => setTimeout(resolve, 1000));
Expand All @@ -47,7 +47,7 @@ function readChunkFromFile(file: string, chunkSize: number): string[] {
const text = fs.readFileSync(file, "utf8");
const chunks = naiveSplitText(text, chunkSize);
console.log(
`Splitting text into ${chunks.length} chunks of max size ${chunkSize} characters.`
`Splitting text into ${chunks.length} chunks of max size ${chunkSize} characters.`,
);
return chunks;
}
Expand All @@ -61,7 +61,7 @@ function printResults(results: IDocument[]): void {
console.log(
`${result.content} - ${JSON.stringify(result.metadata)} -> ${
result.score
}\n`
}\n`,
);
}
}
Expand Down Expand Up @@ -117,20 +117,20 @@ function naiveSplitText(text: string, maxChunkSize: number): string[] {
* This demonstrates how to use the ZepClient to interact with a Zep API.
*/
async function main() {
const projectApiKey = process.env.ZEP_API_KEY;
const projectApiUrl = process.env.ZEP_API_URL;

const file = "babbages_calculating_engine.txt";
const zepApiUrl = "http://localhost:8000";
const maxChunkSize = 500;
const collectionName = `babbage${faker.string.alphanumeric({ length: 8 })}`;

console.log(`Creating collection ${collectionName}`);

const client = await ZepClient.init(zepApiUrl);
const client = await ZepClient.init(projectApiKey, projectApiUrl);
const collection = await client.document.addCollection({
name: collectionName,
embeddingDimensions: 1536, // this must match the embedding dimensions of your embedding model
description: "Babbage's Calculating Engine", // optional
metadata: { qux: faker.string.sample() }, // optional
isAutoEmbedded: true, // optional (default: true) - whether Zep should automatically embed documents
});

console.log(`Created collection ${collectionName}`);
Expand All @@ -143,17 +143,17 @@ async function main() {
content: chunk,
document_id: faker.system.fileName(), // optional document ID used in your system
metadata: { foo: faker.string.sample(), bar: "qux" }, // optional metadata
})
}),
);

console.log(
`Adding ${documents.length} documents to collection ${collectionName}`
`Adding ${documents.length} documents to collection ${collectionName}`,
);

const uuids = await collection.addDocuments(documents);

console.log(
`Added ${uuids.length} documents to collection ${collectionName}`
`Added ${uuids.length} documents to collection ${collectionName}`,
);

await checkEmbeddingStatus(client, collectionName);
Expand All @@ -164,10 +164,10 @@ async function main() {
{
text: query,
},
3
3,
);
console.log(
`Found ${searchResults.length} documents matching query '${query}'`
`Found ${searchResults.length} documents matching query '${query}'`,
);
printResults(searchResults);

Expand All @@ -180,14 +180,14 @@ async function main() {

const mmrSearchResults = await collection.search(mmrSearchQuery, 3);
console.log(
`Found ${mmrSearchResults.length} documents matching MMR query '${query}'`
`Found ${mmrSearchResults.length} documents matching MMR query '${query}'`,
);
printResults(mmrSearchResults);

// Search by embedding
const interestingDocument = searchResults[0];
console.log(
`Searching for documents similar to:\n${interestingDocument.content}\n`
`Searching for documents similar to:\n${interestingDocument.content}\n`,
);
if (!interestingDocument.embedding) {
throw new Error("No embedding found for document");
Expand All @@ -197,10 +197,10 @@ async function main() {
{
embedding: vectorToSearch,
},
3
3,
);
console.log(
`Found ${embeddingSearchResults.length} documents matching embedding`
`Found ${embeddingSearchResults.length} documents matching embedding`,
);
printResults(embeddingSearchResults);

Expand All @@ -214,10 +214,10 @@ async function main() {
text: query,
metadata: metadataQuery,
},
3
3,
);
console.log(
`Found ${newSearchResults.length} documents matching query '${query}' ${metadataQuery}`
`Found ${newSearchResults.length} documents matching query '${query}' ${metadataQuery}`,
);
printResults(newSearchResults);

Expand All @@ -228,7 +228,7 @@ async function main() {
where: { jsonpath: '$[*] ? (@.this_key_does == "not exist")' },
},
},
3
3,
);
console.log("Returned array length:", embeddingSearchResults.length);

Expand All @@ -239,7 +239,7 @@ async function main() {
}
console.log(`Deleting document ${documentToDelete}`);
await collection.deleteDocument(documentToDelete);

console.log(`Deleted document ${documentToDelete}`);
// Get a list of documents in the collection by UUID
const docsToGet = uuids.slice(48, 50);
console.log(`Getting documents: ${docsToGet}`);
Expand Down
59 changes: 59 additions & 0 deletions examples/langchain/message_history_example.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { ZepClient } from "../../src";
import { ZepChatMessageHistory } from "../../src/langchain";
import { ChatOpenAI } from "@langchain/openai";
import {
ChatPromptTemplate,
MessagesPlaceholder,
} from "@langchain/core/prompts";
import { RunnableWithMessageHistory } from "@langchain/core/runnables";
import { ConsoleCallbackHandler } from "@langchain/core/tracers/console";
async function main() {
const zepClient = await ZepClient.init(
process.env.ZEP_API_KEY,
process.env.ZEP_API_URL,
);

const prompt = ChatPromptTemplate.fromMessages([
["system", "Answer the user's question below. Be polite and helpful:"],
new MessagesPlaceholder("history"),
["human", "{question}"],
]);

const chain = prompt
.pipe(
new ChatOpenAI({
temperature: 0.8,
modelName: "gpt-3.5-turbo-1106",
}),
)
.withConfig({
callbacks: [new ConsoleCallbackHandler()],
});

const chainWithHistory = new RunnableWithMessageHistory({
runnable: chain,
getMessageHistory: (sessionId) =>
new ZepChatMessageHistory({
client: zepClient,
sessionId: sessionId,
memoryType: "perpetual",
}),
inputMessagesKey: "question",
historyMessagesKey: "history",
});

const result = await chainWithHistory.invoke(
{
question: "What did we talk about earlier?",
},
{
configurable: {
sessionId: "06d114e2-7c6e-4268-91b9-63a4b9645bbf",
},
},
);

console.log("result", result);
}

main();
Loading

0 comments on commit 8829cf0

Please sign in to comment.