如何使用表达式语言向langchain添加另一个通道

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

我正在使用Langchain的表达语言来创建一个检索器。目标是创建一个功能,让人工智能检查对先前交互的响应。

因此,我需要给这个函数两个输入:原始问题和答案。这意味着我想向它传递两个参数:“问题”和“答案”。

目前我只能通过如下所示的“问题”。我怎样才能将“答案”传递到提示中?”

from langchain_community.vectorstores import DocArrayInMemorySearch
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.runnables import RunnableParallel, RunnablePassthrough

region = "usa"
vector_databases_path = "db"
k = 10
model_name = "gpt-4"
reteriver_chain_type= "stuff"
input = "Albuquerque"
answer = '  MetroArea State\n0  Albuquerque   NM'
####################### Get the Retriever ##################
#******************** Set Embeddings  ***********************
embeddings = OpenAIEmbeddings()

#******************** Load Database Embeddings  ***********************
docsearch = FAISS.load_local(os.path.join(os.getcwd(), vector_databases_path, "MSA_State_faiss_index"), embeddings)

#******************** Set Up the Retriever  ***********************
retriever = docsearch.as_retriever(search_type="similarity", search_kwargs={"k": k})


prompt_template = """
    Use the following pieces of context to check the AI's Answer. 

    {context}

    Job: Your job is to check the MetroArea and State code based on what the AI has returned.
        
    Here is the original input: {question}
    Here is the AI's answer: {answer}
    
   
    Report your answer as a CSV. Where the column names are AI_Correct, 
    AI_Correct: Return TRUE if the AI was correct or FALSE if it is incorrect.
    
    
    Each variable after the comma should be one row. If there is no comma between them then treat input as one row.

    If you do not know the answer, then return: IDK
    
"""
####################### Initialize LLM ##################
llm = ChatOpenAI(
    model_name=model_name,
    temperature=0.0
)


#How do I change this code to allow for an additional parameter, answer, to pass through it to the prompt?

prompt = ChatPromptTemplate.from_template(prompt_template)
output_parser = StrOutputParser()

setup_and_retrieval = RunnableParallel(
    {"context": retriever, "question": RunnablePassthrough()}
)
chain = setup_and_retrieval | prompt | llm | output_parser

final_answer = chain.invoke(input)

final_answer


python langchain
1个回答
0
投票

我可能会使用

question
(输入)和
answer
作为初始变量来调用链, 并使用
RunnablePassThrough.assign
方法添加上下文(这会向链的状态添加一个新密钥)。

因此,在

setup_and_retrieval
之后,您将拥有:

{
    "question": input,
    "answer": answer,
    "context": retriever result
}

这样您就可以在提示模板中访问

answer
键。

代码如下所示:

setup_and_retrieval = RunnablePassthrough.assign(
    context=itemgetter("question") | retriever, # Note that itemgetter is used to get the value of the key "question" -> same effect as invoking the chain with a single "input" parameter
)
chain = setup_and_retrieval | prompt | llm | output_parser

final_answer = chain.invoke({"question": input, "answer": answer})

print(final_answer)

希望这有帮助!

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