python-3.x 无法将提示模板传递到langchain中的RetrievalQA

gj3fmq9x  于 2023-08-08  发布在  Python
关注(0)|答案(2)|浏览(279)

我是Langchain的新手,遵循了这个Retrival QA - Langchain。我有一个自定义的提示符,但当我试图通过提示与chain_type_kwargs它抛出错误在pydanticStufDocumentsChain。而在删除chain_type_kwargs时,itt就可以工作了。
怎么能传递到提示符?

错误

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)

字符串

代码

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)

eyh26e7m

eyh26e7m1#

模板中缺少{context}。

bprjcwpo

bprjcwpo2#

在定义PROMPT后尝试对其进行格式化

PROMPT.format(question="your question")

字符串

相关问题