langchain-ai / langchainjs

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

Typing issue with Pinecone #5685

Open dejoma opened 4 weeks ago

dejoma commented 4 weeks ago

Checklist

Issue with current documentation:

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

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, // <- 
    namespace: 'books'
  });

Error with custom type:
image

Error without custom type: image

Idea or request for content:

No response

dosubot[bot] commented 4 weeks ago

To resolve the TypeScript type mismatch error (TS2322) when using Pinecone and Langchain packages with the latest versions, ensure that the pineconeIndex property in PineconeStoreParams is correctly typed as Index<RecordMetadata> | undefined. The provided code already defines pineconeIndex as an optional property of type PineconeIndex in the PineconeStoreParams interface:

export interface PineconeStoreParams extends AsyncCallerParams {
  pineconeIndex?: PineconeIndex;
  textKey?: string;
  namespace?: string;
  filter?: PineconeMetadata;
  pineconeConfig?: {
    indexName: ConstructorParameters<typeof PineconeIndex>[0];
    config: ConstructorParameters<typeof PineconeIndex>[1];
    namespace?: string;
    indexHostUrl?: string;
    additionalHeaders?: HTTPHeaders;
  };
}

In the PineconeStore class constructor, the pineconeIndex is correctly assigned based on the provided parameters:

constructor(embeddings: EmbeddingsInterface, params: PineconeStoreParams) {
  super(embeddings, params);
  this.embeddings = embeddings;

  const {
    namespace,
    pineconeIndex,
    textKey,
    filter,
    pineconeConfig,
    ...asyncCallerArgs
  } = params;
  this.namespace = namespace;
  if (!pineconeIndex && !pineconeConfig) {
    throw new Error("pineconeConfig or pineconeIndex must be provided.");
  }
  if (pineconeIndex && pineconeConfig) {
    throw new Error(
      "Only one of pineconeConfig or pineconeIndex can be provided."
    );
  }

  if (pineconeIndex) {
    this.pineconeIndex = pineconeIndex;
  } else if (pineconeConfig) {
    this.pineconeIndex = new PineconeIndex(
      pineconeConfig.indexName,
      {
        ...pineconeConfig.config,
        sourceTag: "langchainjs",
      },
      pineconeConfig.namespace,
      pineconeConfig.indexHostUrl,
      pineconeConfig.additionalHeaders
    );
  }

  this.textKey = textKey ?? "text";
  this.filter = filter;
  this.caller = new AsyncCaller(asyncCallerArgs);
}

Ensure that the PineconeIndex type is correctly imported and used. The PineconeIndex type should be imported from the @pinecone-database/pinecone package:

import { Index as PineconeIndex } from "@pinecone-database/pinecone";

Additionally, refer to the test cases provided in the libs/langchain-pinecone/src/tests/vectorstores.test.ts file to ensure that your implementation aligns with the expected usage. For example, the test cases show how to instantiate PineconeStore with the pineconeIndex property:

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

Make sure that the PineconeIndex type is correctly defined in the @pinecone-database/pinecone package and that you are using the latest versions of both Pinecone and Langchain packages. If the error persists, it might be due to a version mismatch or an incorrect type definition in the Pinecone package [1][2].

To continue talking to Dosu, mention @dosu.

jacoblee93 commented 4 weeks ago

Hey @dejoma, sorry you're running into this - could you share your lockfile? I'm unable to repro with my local setup.

Additionally (a bit of a shot in the dark) - can you try explicitly resolving your @langchain/core to a single version?

https://js.langchain.com/v0.2/docs/how_to/installation/#installing-integration-packages