Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError using similaritySearchWithScore method from XataVectorSearch with the xata client #6240

Closed
5 tasks done
kappita opened this issue Jul 27, 2024 · 1 comment · Fixed by #6241
Closed
5 tasks done
Labels
auto:bug Related to a bug, vulnerability, unexpected error with an existing feature

Comments

@kappita
Copy link
Contributor

kappita commented Jul 27, 2024

Checked other resources

  • I added a very descriptive title to this issue.
  • I searched the LangChain.js documentation with the integrated search.
  • I used the GitHub search to find a similar question and didn't find it.
  • I am sure that this is a bug in LangChain.js rather than my code.
  • The bug is not resolved by updating to the latest stable version of LangChain (or the specific integration package).

Example Code

import { getXataClient } from "../xata"; // client created using `xata init` command, suggested by the langchain documentation
import { OllamaEmbeddings } from "@langchain/community/embeddings/ollama";
import { XataVectorSearch } from "@langchain/community/vectorstores/xata";

const client = getXataClient();
const table = "scholarships";
const embeddings = new OllamaEmbeddings({ model: "mxbai-embed-large" });
const store = new XataVectorSearch(embeddings, { client, table });

const results = await store.similaritySearchWithScore("pascuense", 6, {
    university: "USACH",
  });

Error Message and Stack Trace (if applicable)

TypeError: Cannot read properties of undefined (reading 'score')
at C:\Users<user>\Desktop\fwa\ai\node_modules.pnpm@langchain+community@0.2.20_@xata.io+client@0.0.0-next.v64f10283eb9a098c575590d3ea17600e35032_ocbcrk4os3vohexqsbp5nhgrva\node_modules@langchain\community\dist\vectorstores\xata.cjs:106:25
at Array.map ()
at XataVectorSearch.similaritySearchVectorWithScore (C:\Users<user>\Desktop\fwa\ai\node_modules.pnpm@langchain+community@0.2.20_@xata.io+client@0.0.0-next.v64f10283eb9a098c575590d3ea17600e35032_ocbcrk4os3vohexqsbp5nhgrva\node_modules@langchain\community\dist\vectorstores\xata.cjs:98:18)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at example (c:\Users<user>\Desktop\fwa\ai\src\scholarships\example.ts:69:19)

Description

I'm trying to use Xata to store the embeddings and documents, and then use its similarity search.

I use the similaritySearchWithScore method, from the XataVectorSearch object, but it gets a typeError, stopping execution of the code.

System Info

yarn info v1.22.22
warning package.json: No license field
{
name: 'langchain',
'dist-tags': {
next: '0.2.3-rc.0',
latest: '0.2.11'
},
versions: [
'0.1.20',
'0.1.21',
'0.1.22',
'0.1.23',
'0.1.24',
'0.1.25',
'0.1.26',
'0.1.27',
'0.1.28',
'0.1.29-rc.0',
'0.1.29',
'0.1.30',
'0.1.31',
'0.1.32',
'0.1.33',
'0.1.34',
'0.1.35',
'0.1.36',
'0.1.37',
'0.2.0-rc.0',
'0.2.0-rc.1',
'0.2.0',
'0.2.1',
'0.2.2',
'0.2.3-rc.0',
'0.2.3',
'0.2.4',
'0.2.5',
'0.2.6',
'0.2.7',
'0.2.8',
'0.2.9',
'0.2.10',
'0.2.11'
],

CUT OUT MID HERE BECAUSE ERROR BODY TOO LONG

platform: windows

node v20.11.0

yarn v1.22.22

@dosubot dosubot bot added the auto:bug Related to a bug, vulnerability, unexpected error with an existing feature label Jul 27, 2024
Copy link

dosubot bot commented Jul 27, 2024

Hey there, @kappita! I'm a bot here to help you with your coding conundrums. Let's squash those bugs together while we wait for a human maintainer to join in.

To resolve the TypeError when using the similaritySearchWithScore method from the XataVectorSearch class with the Xata client, ensure that the metadata parameter in the similaritySearchWithScore method is correctly structured. The correct method name is similaritySearchVectorWithScore, not similaritySearchWithScore.

Here is the corrected code snippet:

import { getXataClient } from "../xata"; // client created using `xata init` command, suggested by the langchain documentation
import { OllamaEmbeddings } from "@langchain/community/embeddings/ollama";
import { XataVectorSearch } from "@langchain/community/vectorstores/xata";

const client = getXataClient();
const table = "scholarships";
const embeddings = new OllamaEmbeddings({ model: "mxbai-embed-large" });
const store = new XataVectorSearch(embeddings, { client, table });

const results = await store.similaritySearchVectorWithScore("pascuense", 6, {
    university: "USACH",
  });

Additionally, ensure that the university field exists in the documents' metadata and that the similaritySearchVectorWithScore method is being called with the correct parameters.

If you are still encountering the TypeError, it might be due to null or undefined records. This issue was addressed in a recent pull request that fixed null or undefined records causing errors in the similaritySearchVectorWithScore method. The relevant change ensures that if records is null or undefined, the method returns an empty array instead of attempting to map over records and causing an error [1].

Here is the relevant code change:

const { records } = await this.client.db[this.table].vectorSearch(
  "embedding",
  query,
  {
    size: k,
    filter,
  }
);

return (
  records?.map((record: any) => [
    new Document({
      pageContent: record.content,
      metadata: Object.fromEntries(
        Object.entries(record).filter(
          ([key]) =>
            key !== "content" &&
            key !== "embedding" &&
            key !== "xata" &&
            key !== "id"
        )
      ),
    }),
    record.xata.score,
  ]) ?? []
);

This ensures that if records is null or undefined, the method returns an empty array instead of causing an error [1].

To continue talking to Dosu, mention @dosu.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
auto:bug Related to a bug, vulnerability, unexpected error with an existing feature
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant