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.
Add tests for pinecone (langchain-ai#166)
- Loading branch information
Showing
3 changed files
with
118 additions
and
1 deletion.
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 |
---|---|---|
@@ -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 |
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,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" }), | ||
]); | ||
}); |
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,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); | ||
}); |