Here are the code snippet,
const vectorStore = await PineconeStore.fromExistingIndex(embeddings, {
pineconeIndex,
});
const retriever = vectorStore.asRetriever();
// what are the differences between invoke() and getRelevantDocuments?
await retriever.invoke("How to make a cake?");
await retriever.getRelevantDocuments("How to make a cake?");
I seem to have difficulty to search from the API reference that describe these two functions. Do you know when should we use invoke() and getRelevantDocuments?
In this context, calling
await retriever.invoke("How to make a cake?")andawait retriever.getRelevantDocuments("How to make a cake?")have the same result, but the two methods belong to different interfaces.getRelevantDocuments()
getRelevantDocuments()belongs to theBaseRetrieverInterface, whichVectorStoreRetrieverimplements.vectorStore.asRetriever()returns aVectorStoreRetrieverinstance (a subclass ofBaseRetriever).You can call
getRelevantDocuments()directly if your use case utilizes ONLY the retriever.References
invoke()
invoke()belongs to theRunnableInterface, whichBaseRetrieverInterfaceextends. The implementation ofinvoke()is in theBaseRetrieverclass. The current implementation (0.1.28) ofinvoke()callsgetRelevantDocuments()directly.So why are both
invoke()andgetRelevantDocuments()needed?invoke()is needed because the chain abstraction in LangChain is arbitrary with respect to which runnable abstractions (prompt templates, retrievers, models, output parsers, etc) exist in a chain.For example, a chain can be composed of a retriever and a model. In order to maintain a common API that can be called to run the chain, each runnable abstraction (retriever and model) should have the
invoke()method. Another chain can be composed of a model and an output parser. For the same reasons as before, each runnable abstraction (model and output parser) should have theinvoke()method.If your use case requires a chain, call
invoke().References