langchain-ai / langchainjs

🦜🔗 Build context-aware reasoning applications 🦜🔗
https://js.langchain.com/docs/
MIT License
12.16k stars 2.04k forks source link

Passing Pinecone.Index to PineconeStore.fromExistingIndex broken after update #5686

Closed dejoma closed 2 months ago

dejoma commented 2 months ago

Checked other resources

Example Code

Package updates that trigger fail

If you update all pinecone/langchain packages to their latest versions:

↑ @langchain/community 0.2.4 → 0.2.6
↑ @pinecone-database/pinecone 2.2.1 → 2.2.2
↑ langchain 0.2.4 → 0.2.5

# having @langchain/pinecone@0.0.7

Code

Then the following code starts failing (in build-time):

// My index with or without custom type; doesn't matter
const pinecone = new Pinecone();
const pineconeIndex = pinecone.Index<{
  helpMePlease: string;
}>('index-prod-eu');

const vectorStore = await PineconeStore.fromExistingIndex(embeddings, {
    pineconeIndex, // <- errors here
    namespace: 'i-say-whut-whut'
  });

Error messages

Error with custom type:
image

Error without custom type: image

Error Message and Stack Trace (if applicable)

No response

Description

This is the code that works with the version numbers before upgrading.

System Info

I am using bun 1.1.12 Node v20 Build fails

dosubot[bot] commented 2 months ago

To resolve the TypeScript type mismatch error when passing Pinecone.Index to PineconeStore.fromExistingIndex, ensure that the pineconeIndex parameter in the PineconeStoreParams interface is of type PineconeIndex. The PineconeStore.fromExistingIndex method expects a PineconeStoreParams object, which includes the pineconeIndex property of type PineconeIndex.

Here is an example of how you can correctly pass the PineconeIndex to PineconeStore.fromExistingIndex:

import { PineconeIndex } from "@pinecone-database/pinecone";
import { PineconeStore } from "path-to-your-pinecone-store";
import { EmbeddingsInterface } from "@langchain/core/embeddings";

// Assuming you have an instance of PineconeIndex
const pineconeIndex: PineconeIndex = new PineconeIndex("your-index-name", { /* your config */ });

// Your embeddings instance
const embeddings: EmbeddingsInterface = /* your embeddings instance */;

// Configuration for PineconeStore
const dbConfig = {
  pineconeIndex: pineconeIndex,
  textKey: "text",
  namespace: "your-namespace",
};

// Create PineconeStore from existing index
PineconeStore.fromExistingIndex(embeddings, dbConfig)
  .then((pineconeStore) => {
    console.log("PineconeStore created successfully", pineconeStore);
  })
  .catch((error) => {
    console.error("Error creating PineconeStore", error);
  });

Ensure that the pineconeIndex you are passing is indeed an instance of PineconeIndex and that it matches the expected type in the PineconeStoreParams interface.

Additionally, you can refer to the test cases in the libs/langchain-pinecone/src/tests/vectorstores.test.ts file for more examples of how to instantiate and use PineconeStore. For instance, the test cases show how to mock a Pinecone client and use it with PineconeStore:

import { jest, test, expect } from "@jest/globals";
import { FakeEmbeddings } from "@langchain/core/utils/testing";
import { PineconeStore } from "../vectorstores.js";

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

  const store = new PineconeStore(embeddings, { pineconeIndex: client as any });

  expect(store).toBeDefined();

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

  expect(upsert).toHaveBeenCalledTimes(1);

  expect(upsert).toHaveBeenCalledWith([
    {
      id: "id1",
      metadata: { a: 1, "b.nested.0": 1, "b.nested.1.a": 4, text: "hello" },
      values: [0.1, 0.2, 0.3, 0.4],
    },
  ]);

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

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

These examples should help you understand how to correctly configure and use PineconeStore with the updated packages [1][2].

To continue talking to Dosu, mention @dosu.

jacoblee93 commented 2 months ago

Closing as a duplicate of #5685