Skip to content

Commit

Permalink
pass in prompts to vectordbqa (#1158)
Browse files Browse the repository at this point in the history
  • Loading branch information
hwchase17 committed Feb 20, 2023
1 parent 65cc81c commit 955c89f
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 4 deletions.
63 changes: 62 additions & 1 deletion docs/modules/chains/combine_docs_examples/vector_db_qa.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
{
"data": {
"text/plain": [
"\" The president said that Ketanji Brown Jackson is one of the nation's top legal minds, a former top litigator in private practice, a former federal public defender, and from a family of public school educators and police officers. He also said that she is a consensus builder, and has received a broad range of support from the Fraternal Order of Police to former judges appointed by Democrats and Republicans.\""
"\" The president said that Ketanji Brown Jackson is one of the nation's top legal minds, a former top litigator in private practice and federal public defender, from a family of public school educators and police officers, a consensus builder, and has received a broad range of support from the Fraternal Order of Police to former judges appointed by Democrats and Republicans.\""
]
},
"execution_count": 4,
Expand Down Expand Up @@ -166,6 +166,67 @@
"qa.run(query)"
]
},
{
"cell_type": "markdown",
"id": "90c7899a",
"metadata": {},
"source": [
"## Custom Prompts\n",
"You can pass in custom prompts to do question answering. These prompts are the same prompts as you can pass into the [base question answering chain](./question_answering.ipynb)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "a45232a2",
"metadata": {},
"outputs": [],
"source": [
"from langchain.prompts import PromptTemplate\n",
"prompt_template = \"\"\"Use the following pieces of context to answer the question at the end. If you don't know the answer, just say that you don't know, don't try to make up an answer.\n",
"\n",
"{context}\n",
"\n",
"Question: {question}\n",
"Answer in Italian:\"\"\"\n",
"PROMPT = PromptTemplate(\n",
" template=prompt_template, input_variables=[\"context\", \"question\"]\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "9b5c8d1d",
"metadata": {},
"outputs": [],
"source": [
"chain_type_kwargs = {\"prompt\": PROMPT}\n",
"qa = VectorDBQA.from_chain_type(llm=OpenAI(), chain_type=\"stuff\", vectorstore=docsearch, chain_type_kwargs=chain_type_kwargs)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "26ee7671",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"\" Il Presidente ha detto che Ketanji Brown Jackson è uno dei pensatori legali più importanti del nostro Paese, che continuerà l'eccellente eredità di giustizia Breyer. È un ex principale litigante in pratica privata, un ex difensore federale pubblico e appartiene a una famiglia di insegnanti e poliziotti delle scuole pubbliche. È un costruttore di consenso che ha ricevuto un ampio supporto da parte di Fraternal Order of Police e giudici designati da democratici e repubblicani.\""
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"query = \"What did the president say about Ketanji Brown Jackson\"\n",
"qa.run(query)"
]
},
{
"cell_type": "markdown",
"id": "0b8c37f7",
Expand Down
13 changes: 10 additions & 3 deletions langchain/chains/vector_db_qa/base.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Chain for question-answering against a vector database."""
from __future__ import annotations

from typing import Any, Dict, List
from typing import Any, Dict, List, Optional

from pydantic import BaseModel, Extra, Field, root_validator

Expand Down Expand Up @@ -120,10 +120,17 @@ def from_llm(

@classmethod
def from_chain_type(
cls, llm: BaseLLM, chain_type: str = "stuff", **kwargs: Any
cls,
llm: BaseLLM,
chain_type: str = "stuff",
chain_type_kwargs: Optional[dict] = None,
**kwargs: Any,
) -> VectorDBQA:
"""Load chain from chain type."""
combine_documents_chain = load_qa_chain(llm, chain_type=chain_type)
_chain_type_kwargs = chain_type_kwargs or {}
combine_documents_chain = load_qa_chain(
llm, chain_type=chain_type, **_chain_type_kwargs
)
return cls(combine_documents_chain=combine_documents_chain, **kwargs)

def _call(self, inputs: Dict[str, str]) -> Dict[str, Any]:
Expand Down

0 comments on commit 955c89f

Please sign in to comment.