英文:
unable to pass prompt template to RetrievalQA in langchain
问题
I am new to Langchain and followed this Retrival QA - Langchain. I have a custom prompt but when I try to pass Prompt with chain_type_kwargs
its throws error in pydantic
StuffDocumentsChain
. and on removing chain_type_kwargs
it just works.
how can pass to the prompt?
error
File /usr/local/lib/python3.11/site-packages/pydantic/main.py:341, in pydantic.main.BaseModel.__init__()
ValidationError: 1 validation error for StuffDocumentsChain
__root__
document_variable_name context was not found in llm_chain input_variables: ['question'] (type=value_error)
Code
import json, os
from langchain.chains import RetrievalQA
from langchain.llms import OpenAI
from langchain.document_loaders import JSONLoader
from langchain.text_splitter import CharacterTextSplitter
from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.vectorstores import Chroma
from langchain.chat_models import ChatOpenAI
from langchain import PromptTemplate
from pathlib import Path
from pprint import pprint
os.environ["OPENAI_API_KEY"] = "my-key"
def metadata_func(record: dict, metadata: dict) -> dict:
metadata["drug_name"] = record["drug_name"]
return metadata
loader = JSONLoader(
file_path='./drugs_data_v2.json',
jq_schema='.drugs[]',
content_key="data",
metadata_func=metadata_func)
docs = loader.load()
text_splitter = CharacterTextSplitter(chunk_size=5000, chunk_overlap=200)
texts = text_splitter.split_documents(docs)
embeddings = OpenAIEmbeddings()
docsearch = Chroma.from_documents(texts, embeddings)
template = """/
example custom prompt
Question: {question}
Answer:
"""
PROMPT = PromptTemplate(template=template, input_variables=['question'])
qa = RetrievalQA.from_chain_type(
llm=ChatOpenAI(
model_name='gpt-3.5-turbo-16k'
),
chain_type="stuff",
chain_type_kwargs={"prompt": PROMPT},
retriever=docsearch.as_retriever(),
)
query = "What did the president say about Ketanji Brown Jackson"
qa.run(query)
<details>
<summary>英文:</summary>
I am new to Langchain and followed this [Retrival QA - Langchain][1]. I have a custom prompt but when I try to pass Prompt with `chain_type_kwargs` its throws error in `pydantic` `StufDocumentsChain`. and on removing `chain_type_kwargs` itt just works.
how can pass to the prompt?
error
---
```cmd
File /usr/local/lib/python3.11/site-packages/pydantic/main.py:341, in pydantic.main.BaseModel.__init__()
ValidationError: 1 validation error for StuffDocumentsChain
__root__
document_variable_name context was not found in llm_chain input_variables: ['question'] (type=value_error)
Code
import json, os
from langchain.chains import RetrievalQA
from langchain.llms import OpenAI
from langchain.document_loaders import JSONLoader
from langchain.text_splitter import CharacterTextSplitter
from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.vectorstores import Chroma
from langchain.chat_models import ChatOpenAI
from langchain import PromptTemplate
from pathlib import Path
from pprint import pprint
os.environ["OPENAI_API_KEY"] = "my-key"
def metadata_func(record: dict, metadata: dict) -> dict:
metadata["drug_name"] = record["drug_name"]
return metadata
loader = JSONLoader(
file_path='./drugs_data_v2.json',
jq_schema='.drugs[]',
content_key="data",
metadata_func=metadata_func)
docs = loader.load()
text_splitter = CharacterTextSplitter(chunk_size=5000, chunk_overlap=200)
texts = text_splitter.split_documents(docs)
embeddings = OpenAIEmbeddings()
docsearch = Chroma.from_documents(texts, embeddings)
template = """/
example custom prommpt
Question: {question}
Answer:
"""
PROMPT = PromptTemplate(template=template, input_variables=['question'])
qa = RetrievalQA.from_chain_type(
llm=ChatOpenAI(
model_name='gpt-3.5-turbo-16k'
),
chain_type="stuff",
chain_type_kwargs={"prompt": PROMPT},
retriever=docsearch.as_retriever(),
)
query = "What did the president say about Ketanji Brown Jackson"
qa.run(query)
答案1
得分: 1
{context}在模板中缺失。
英文:
{context} is missing in template.
答案2
得分: 0
尝试在定义之后格式化 PROMPT:
PROMPT.format(question="你的问题")
英文:
Try to format the PROMPT after you define it
PROMPT.format(question="your question")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论