无法将提示模板传递给语言链中的检索问答模块。

huangapple go评论101阅读模式
英文:

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: [&#39;question&#39;] (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[&quot;OPENAI_API_KEY&quot;] = &quot;my-key&quot;



def metadata_func(record: dict, metadata: dict) -&gt; dict:
    metadata[&quot;drug_name&quot;] = record[&quot;drug_name&quot;]

    return metadata


loader = JSONLoader(
    file_path=&#39;./drugs_data_v2.json&#39;, 
    jq_schema=&#39;.drugs[]&#39;,
    content_key=&quot;data&quot;,
    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 = &quot;&quot;&quot;/
example custom prommpt

Question: {question}
Answer: 
&quot;&quot;&quot;

PROMPT = PromptTemplate(template=template, input_variables=[&#39;question&#39;])


qa = RetrievalQA.from_chain_type(
        llm=ChatOpenAI(
           model_name=&#39;gpt-3.5-turbo-16k&#39;       
    ),
    chain_type=&quot;stuff&quot;,
    chain_type_kwargs={&quot;prompt&quot;: PROMPT},
    retriever=docsearch.as_retriever(),
)

query = &quot;What did the president say about Ketanji Brown Jackson&quot;
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=&quot;your question&quot;)

huangapple
  • 本文由 发表于 2023年6月26日 15:25:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76554411.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定