langchain-ai / langchainjs

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

How can I query from existing collection in a PGVector? #2307

Closed eRuaro closed 1 year ago

eRuaro commented 1 year ago

In the Python library, you can use something like this:

store = PGVector(
    collection_name=COLLECTION_NAME,
    connection_string=CONNECTION_STRING,
    embedding_function=embeddings,
)

And in the JS chroma integration, you can use this:

const vectorStore = await Chroma.fromExistingCollection(
  new OpenAIEmbeddings(),
  { collectionName: "godel-escher-bach" }
);

What's the equivalent code for PGVectors in JS? There don't seem to be any relevant / equivalent functions to Chroma's fromExistingCollection nor can I specify in the constructor the collection_name.

dosubot[bot] commented 1 year ago

🤖

Hello,

In the LangChain Javascript/Typescript framework, the equivalent way to query from an existing collection in a PGVector is by using the MyScaleStore class. Here's an example:

import { MyScaleStore } from "langchain/vectorstores/myscale";
import { OpenAIEmbeddings } from "langchain/embeddings/openai";

const vectorStore = await MyScaleStore.fromExistingIndex(
  new OpenAIEmbeddings(),
  {
    host: process.env.MYSCALE_HOST || "localhost",
    port: process.env.MYSCALE_PORT || "8443",
    username: process.env.MYSCALE_USERNAME || "username",
    password: process.env.MYSCALE_PASSWORD || "password",
    database: "your_database", // defaults to "default"
    table: "your_table", // defaults to "vector_table"
  }
);

const results = await vectorStore.similaritySearch("hello world", 1);
console.log(results);

const filteredResults = await vectorStore.similaritySearch("hello world", 1, {
  whereStr: "metadata.name = '1'",
});
console.log(filteredResults);

In this code, MyScaleStore.fromExistingIndex is used to create a vector store from an existing index. The vectorStore.similaritySearch method is used to search for the most similar text in the vector store. The optional whereStr parameter can be used to filter the results based on the metadata.

You can find more examples in the LangChain repository:

I hope this helps! If you have any other questions, feel free to ask.

Sources


This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research.

Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.

If you want to continue the conversation, start your reply with @dosu-bot.

jacoblee93 commented 1 year ago

Your best bet is the TypeORM vector store.

Going to close this but if you have trouble, feel free to open another issue:

https://js.langchain.com/docs/modules/data_connection/vectorstores/integrations/typeorm

eRuaro commented 1 year ago

There are no methods though that let you pass in a specific collection_name though? Meaning I'll have to dive in the typeorm API itself?