My code is not working it is showing key error "language". I have assigned language in prompt and in chain. I don't know the right approach to use a variable in this code help me resolve the error. Here is my code:
from langchain.schema import format_document
from langchain.schema.runnable import RunnableMap
from operator import itemgetter
import pinecone
from langchain.chat_models import ChatOpenAI
from langchain.embeddings import OpenAIEmbeddings
from langchain.prompts import ChatPromptTemplate
from langchain.schema.output_parser import StrOutputParser
from langchain.schema.runnable import RunnableLambda, RunnablePassthrough
from langchain.vectorstores import Pinecone
import os
os.environ["OPENAI_API_KEY"] = "**************"
llm = ChatOpenAI(
model_name='gpt-3.5-turbo',
temperature=0.4
)
embeddings = OpenAIEmbeddings()
# Orignal Database
pinecone.init(
api_key='********-147c-****-8096-***********',
environment='*******'
)
index_name = 'bmaster'
index = pinecone.Index(index_name)
text_field = "text"
db = Pinecone(index, embeddings.embed_query, text_field)
retriever = db.as_retriever()
from langchain.prompts.prompt import PromptTemplate
_template = """Given the following conversation and a follow up question, rephrase the follow up question to be a standalone question, in its original language.
Chat History:
{chat_history}
Follow Up Input: {question}
Standalone question:"""
CONDENSE_QUESTION_PROMPT = PromptTemplate.from_template(_template)
template = """Answer the question based only on the following context:
{context}
Answer in the following language: {language}
Question: {question}
"""
ANSWER_PROMPT = ChatPromptTemplate.from_template(template)
DEFAULT_DOCUMENT_PROMPT = PromptTemplate.from_template(template="{page_content}")
def _combine_documents(
docs, document_prompt=DEFAULT_DOCUMENT_PROMPT, document_separator="\n\n"
):
doc_strings = [format_document(doc, document_prompt) for doc in docs]
return document_separator.join(doc_strings)
from typing import List, Tuple
def _format_chat_history(chat_history: List[Tuple]) -> str:
buffer = ""
for dialogue_turn in chat_history:
human = "Human: " + dialogue_turn[0]
ai = "Assistant: " + dialogue_turn[1]
buffer += "\n" + "\n".join([human, ai])
return buffer
_inputs = RunnableMap(
standalone_question=RunnablePassthrough.assign(
chat_history=lambda x: _format_chat_history(x["chat_history"])
)
| CONDENSE_QUESTION_PROMPT
| ChatOpenAI(temperature=0)
| StrOutputParser(),
)
_context = {
"context": itemgetter("standalone_question") | retriever | _combine_documents,
"question": lambda x: x["standalone_question"],
"language": itemgetter("language")
}
conversational_qa_chain = _inputs | _context | ANSWER_PROMPT | ChatOpenAI()
conversational_qa_chain.invoke(
{
"question": "Hi, how are you?",
"chat_history": [],
"language":"French"
}
)
Here is the error:
File ~/Langchain_new/lang/lib/python3.11/site-packages/langchain/schema/runnable/config.py:308, in call_func_with_variable_args(func, input, config, run_manager, **kwargs)
306 if run_manager is not None and accepts_run_manager(func):
307 kwargs["run_manager"] = run_manager
--> 308 return func(input, **kwargs)
KeyError: 'language'
I am following this langchain documentation https://python.langchain.com/docs/expression_language/cookbook/retrieval
In the first block of the documentation, it is using language variable and it is working correctly. But in the 2nd portion in Conversation Retrieval-chain right here. https://python.langchain.com/docs/expression_language/cookbook/retrieval#conversational-retrieval-chain it is creating standalone questions and doing other stuff for conversation and i want to language variable in it assigned in the prompt. But I'm getting error.