Skip to content

Commit

Permalink
Add tests for pinecone (langchain-ai#166)
Browse files Browse the repository at this point in the history
  • Loading branch information
nfcampos authored Feb 28, 2023
1 parent fc58513 commit 8b02631
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 1 deletion.
4 changes: 3 additions & 1 deletion langchain/.env.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
OPENAI_API_KEY=ADD_YOUR_API_KEY_HERE
SERPAPI_API_KEY=ADD_YOUR_API_KEY_HERE
COHERE_API_KEY=ADD_YOUR_API_KEY_HERE
HUGGINGFACEHUB_API_KEY=ADD_YOUR_API_KEY_HERE
HUGGINGFACEHUB_API_KEY=ADD_YOUR_API_KEY_HERE
PINECONE_API_KEY=your_api_key
PINECONE_ENVIRONMENT=your_environment
64 changes: 64 additions & 0 deletions langchain/src/vectorstores/tests/pinecone.int.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { test, expect } from "@jest/globals";
import { PineconeClient } from "@pinecone-database/pinecone";
import { OpenAIEmbeddings } from "../../embeddings/index.js";
import { Document } from "../../document.js";

import { PineconeStore } from "../pinecone.js";

test("PineconeStore with external ids", async () => {
const client = new PineconeClient();

await client.init({
environment: process.env.PINECONE_ENVIRONMENT!,
apiKey: process.env.PINECONE_API_KEY!,
});

const index = client.Index(process.env.PINECONE_INDEX!);

const embeddings = new OpenAIEmbeddings();

const store = new PineconeStore(index, embeddings);

expect(store).toBeDefined();

await store.addDocuments(
[{ pageContent: "hello", metadata: { a: 1 } }],
["id1"]
);

const results = await store.similaritySearch("hello", 1);

expect(results).toHaveLength(1);

expect(results).toEqual([
new Document({ metadata: { a: 1 }, pageContent: "hello" }),
]);
});

test("PineconeStore with generated ids", async () => {
const client = new PineconeClient();

await client.init({
environment: process.env.PINECONE_ENVIRONMENT!,
apiKey: process.env.PINECONE_API_KEY!,
});

const index = client.Index(process.env.PINECONE_INDEX!);

const embeddings = new OpenAIEmbeddings();

const store = new PineconeStore(index, embeddings);

expect(store).toBeDefined();

await store.addDocuments([{ pageContent: "hello", metadata: { a: 1 } }]);

const results = await store.similaritySearch("hello", 1);

expect(results).toHaveLength(1);

// This assert can fail if the test index is used for other things.
expect(results).toEqual([
new Document({ metadata: { a: 1 }, pageContent: "hello" }),
]);
});
51 changes: 51 additions & 0 deletions langchain/src/vectorstores/tests/pinecone.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { jest, test, expect } from "@jest/globals";
import { FakeEmbeddings } from "../../embeddings/fake.js";

import { PineconeStore } from "../pinecone.js";

test("PineconeStore with external ids", async () => {
const client = {
upsert: jest.fn(),
query: jest.fn<any>().mockResolvedValue({
matches: [],
}),
};
const embeddings = new FakeEmbeddings();

const store = new PineconeStore(client as any, embeddings);

expect(store).toBeDefined();

await store.addDocuments(
[{ pageContent: "hello", metadata: { a: 1 } }],
["id1"]
);

expect(client.upsert).toHaveBeenCalledTimes(1);

const results = await store.similaritySearch("hello");

expect(results).toHaveLength(0);
});

test("PineconeStore with generated ids", async () => {
const client = {
upsert: jest.fn(),
query: jest.fn<any>().mockResolvedValue({
matches: [],
}),
};
const embeddings = new FakeEmbeddings();

const store = new PineconeStore(client as any, embeddings);

expect(store).toBeDefined();

await store.addDocuments([{ pageContent: "hello", metadata: { a: 1 } }]);

expect(client.upsert).toHaveBeenCalledTimes(1);

const results = await store.similaritySearch("hello");

expect(results).toHaveLength(0);
});

0 comments on commit 8b02631

Please sign in to comment.