-
I understand that the loadQAStuffChain function is used to initialize the LLMChain with a custom prompt template.??? ^^^ Can you give me an example of how to use the LoadQAStuffChain function to use it in the LLMChain custom prompt template? Right now the code I have doesn't work to combine LLMChain with RetrievalQAChain Code:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
🤖 Hello, From your code, it seems like you're trying to combine The Here is the function code for reference: export function loadQAStuffChain(
llm: BaseLanguageModel,
params: StuffQAChainParams = {}
) {
const { prompt = QA_PROMPT_SELECTOR.getPrompt(llm), verbose } = params;
const llmChain = new LLMChain({ prompt, llm, verbose });
const chain = new StuffDocumentsChain({ llmChain, verbose });
return chain;
} (Source: langchain/src/chains/question_answering/load.ts) To correctly use the import { OpenAI } from "langchain/llms/openai";
import { RetrievalQAChain } from "langchain/chains";
import { HNSWLib } from "langchain/vectorstores/hnswlib";
import { OpenAIEmbeddings } from "langchain/embeddings/openai";
import { RecursiveCharacterTextSplitter } from "langchain/text_splitter";
import * as fs from "fs";
// Initialize the LLM to use to answer the question.
const model = new OpenAI({});
const text = fs.readFileSync("state_of_the_union.txt", "utf8");
const textSplitter = new RecursiveCharacterTextSplitter({ chunkSize: 1000 });
const docs = await textSplitter.createDocuments([text]);
// Create a vector store from the documents.
const vectorStore = await HNSWLib.fromDocuments(docs, new OpenAIEmbeddings());
// Initialize a retriever wrapper around the vector store
const vectorStoreRetriever = vectorStore.asRetriever();
// Create a chain that uses the OpenAI LLM and HNSWLib vector store.
const chain = RetrievalQAChain.fromLLM(model, vectorStoreRetriever);
const res = await chain.call({
query: "What did the president say about Justice Breyer?",
});
console.log({ res }); (Source: examples/src/use_cases/local_retrieval_qa/qa_chain.ts) 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. |
Beta Was this translation helpful? Give feedback.
🤖
Hello,
From your code, it seems like you're trying to combine
LLMChain
withRetrievalQAChain
using theloadQAStuffChain
function. However, theloadQAStuffChain
function is not designed to be used in this way.The
loadQAStuffChain
function is used to create and load aStuffQAChain
instance based on the provided parameters. It takes an instance ofBaseLanguageModel
and an optionalStuffQAChainParams
object as parameters. TheStuffQAChainParams
object can contain aprompt
of typeBasePromptTemplate
and averbose
boolean flag.Here is the function code for reference: