Langchain js ConversationalRetrievalQAChain 和提示

问题描述 投票:0回答:1

我正在使用 langchain js 并使用 ConversationalRetrievalQAChain 和提示进行一些实验。

我尝试在提示符中添加一个名为 lang 的变量,并在调用期间设置该值,但我总是收到此错误

抛出新错误(

Missing value for input ${node.name}
);

我正在使用 langchain.js,但我不明白为什么。没有 {lang} 并使用正确的语言替换(例如“西班牙语”),它可以正常工作。



const QA_PROMPT = `You are an Assistant that speak only in {lang}, you speak and write only in {lang}. Use the following pieces of context to answer the question at the end.
If you don't know the answer, just say you don't know. DO NOT try to make up an answer.
If the question is not related to the context, politely respond that you are tuned to only answer questions that are related to the context.

{context}

Question: {question}
Helpful answer write in {lang}:`;

const chain = await ConversationalRetrievalQAChain.fromLLM(
    model,
    vectorStore.asRetriever(),
    //dobbiamo specificare i k
    {
        qaTemplate: QA_PROMPT,
        returnSourceDocuments: true,

        },

)

let res = ""
const stream = await chain.call({
    question: "Chi erano i partecipanti al verbale?",
    chat_history: [],
    lang: "Italian" // Qui dovremmo specificare la lingua


}, [{
    handleLLMNewToken(token) {
        res += token;
        console.clear()
        console.log(res)
    }
    }]
);

我希望使用自定义语言作为提示的输入。

我尝试使用提示或其他方式为 ConversationalRetrievalQAChain 设置提示,但没有,因为它只接受字符串。

javascript prompt langchain langchain-js
1个回答
0
投票

将您的代码更改为:

const chain = await ConversationalRetrievalQAChain.fromLLM(
model,
vectorStore.asRetriever(),
//dobbiamo specificare i k
{
    qaChainOptions: {
        type: 'stuff',
        prompt: PromptTemplate.fromTemplate(QA_PROMPT)
    },
    returnSourceDocuments: true,

    },
)

我是一名大三学生,所以我可以向你解释它为什么有效,但我通过查看 langchain 测试找到了答案https://github.com/langchain-ai/langchainjs/blob/main/langchain/src/链/测试/conversational_retrieval_chain.int.test.ts

© www.soinside.com 2019 - 2024. All rights reserved.