How to add indexes using langchine PGVector() and retrive using index to be used in RAG chain?

327 Views Asked by At

db = PGVector(embedding_function=embeddings, collection_name= case_id,connection_string = CONNECTION_STRING)

1

There are 1 best solutions below

0
On

Take a look at this example that creates a multi-index router in Langchain's repo.

Although they are using the ArxivRetriever, KayAiRetriever, PubMedRetriever, and WikipediaRetriever, I don't see why you cannot create two separate collections with PGVector and then use the as_retriever() method to convert them to Retriever objects, i.e.:

retriever_one = PGVector(...).as_retriever().with_config(run_name="retriever_one")
retriever_two = PGVector(...).as_retriever().with_config(run_name="retriever_two")

Then, use LangChain's Router, as seen in the example repo, to create a Runnable, which "makes it easy to define custom chains as well as invoke them in a standard way":

from langchain.schema.runnable import RouterRunnable

retriever_map = {
    "one": retriever_one,
    "two": retriever_two,
}
router_retriever = RouterRunnable(runnables=retriever_map)

You can then use router_retriever in any chain, as you can see in the example linked above:

retriever_chain = (
    {"input": itemgetter("question"), "key": itemgetter("retriever_choice")}
    | router_retriever  # here's your router_retriever
    | format_docs
).with_config(run_name="retrieve")
answer_chain = (
    {"sources": retriever_chain, "question": itemgetter("question")}
    | prompt
    | llm
    | StrOutputParser()
)

I haven't tested this myself, but this should do the trick.