Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 25,
"id": "f059012e",
"metadata": {},
"outputs": [],
Expand All @@ -28,20 +28,12 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 26,
"id": "f10b831c",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Using embedded DuckDB without persistence: data will be transient\n"
]
}
],
"outputs": [],
"source": [
"loader = TextLoader(\"../../state_of_the_union.txt\")\n",
"loader = TextLoader(\"../../state_of_the_union.txt\", encoding=\"utf-8\")\n",
"documents = loader.load()\n",
"text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)\n",
"texts = text_splitter.split_documents(documents)\n",
Expand All @@ -53,7 +45,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 27,
"id": "70f3a38c",
"metadata": {},
"outputs": [],
Expand All @@ -66,7 +58,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 28,
"id": "7b3e1731",
"metadata": {},
"outputs": [],
Expand All @@ -76,7 +68,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 29,
"id": "70a9ccff",
"metadata": {},
"outputs": [],
Expand All @@ -86,7 +78,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 30,
"id": "efcdb6fb",
"metadata": {},
"outputs": [],
Expand All @@ -99,7 +91,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 31,
"id": "64a08263",
"metadata": {},
"outputs": [],
Expand All @@ -113,7 +105,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 32,
"id": "cb876c97",
"metadata": {},
"outputs": [],
Expand All @@ -126,7 +118,7 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 33,
"id": "a75bad9b",
"metadata": {},
"outputs": [],
Expand All @@ -136,17 +128,17 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 34,
"id": "9a60f109",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'{\\n \"answer\": \"The President expressed strong condemnation of Russia\\'s actions in Ukraine and announced measures to isolate Russia and provide support to Ukraine. He stated that Russia\\'s invasion of Ukraine will have long-term consequences for Russia and emphasized the commitment of the United States and its allies to defend NATO countries. The President also mentioned the imposition of sanctions on Russia and the release of oil reserves to help mitigate gas prices. Overall, the President\\'s message conveyed a firm stance against Russia\\'s aggression and a commitment to supporting Ukraine and protecting American interests.\",\\n \"sources\": [\"0-pl\", \"4-pl\", \"5-pl\", \"6-pl\"]\\n}'"
"'{\\n \"answer\": \"The President expressed strong condemnation of Russia\\'s actions in Ukraine and announced measures to isolate Russia and provide support to Ukraine. He stated that Russia\\'s invasion of Ukraine will have long-term consequences for Russia and emphasized the commitment to defend NATO countries. The President also mentioned taking robust action through sanctions and releasing oil reserves to mitigate gas prices. Overall, the President conveyed a message of solidarity with Ukraine and determination to protect American interests.\",\\n \"sources\": [\"0-pl\", \"4-pl\", \"5-pl\", \"6-pl\"]\\n}'"
]
},
"execution_count": 10,
"execution_count": 34,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -167,7 +159,7 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 35,
"id": "3559727f",
"metadata": {},
"outputs": [],
Expand All @@ -177,7 +169,7 @@
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": 36,
"id": "5a7997d1",
"metadata": {},
"outputs": [],
Expand All @@ -191,7 +183,7 @@
},
{
"cell_type": "code",
"execution_count": 13,
"execution_count": 37,
"id": "79368e40",
"metadata": {},
"outputs": [],
Expand All @@ -204,17 +196,17 @@
},
{
"cell_type": "code",
"execution_count": 14,
"execution_count": 38,
"id": "6b8641de",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"AnswerWithSources(answer=\"The President expressed strong condemnation of Russia's actions in Ukraine and announced measures to isolate Russia and provide support to Ukraine. He stated that Russia's invasion of Ukraine will have long-term consequences for Russia and emphasized the commitment of the United States and its allies to defend NATO countries. The President also mentioned the imposition of sanctions on Russia and the release of oil reserves to help mitigate gas prices. Overall, the President's message conveyed a firm stance against Russia's aggression and support for Ukraine.\", sources=['0-pl', '4-pl', '5-pl', '6-pl'])"
"AnswerWithSources(answer=\"The President expressed strong condemnation of Russia's actions in Ukraine and announced measures to isolate Russia and provide support to Ukraine. He stated that Russia's invasion of Ukraine will have long-term consequences for Russia and emphasized the commitment to defend NATO countries. The President also mentioned taking robust action through sanctions and releasing oil reserves to mitigate gas prices. Overall, the President conveyed a message of solidarity with Ukraine and determination to protect American interests.\", sources=['0-pl', '4-pl', '5-pl', '6-pl'])"
]
},
"execution_count": 14,
"execution_count": 38,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -235,7 +227,7 @@
},
{
"cell_type": "code",
"execution_count": 15,
"execution_count": 39,
"id": "18e5f090",
"metadata": {},
"outputs": [],
Expand All @@ -260,7 +252,7 @@
},
{
"cell_type": "code",
"execution_count": 16,
"execution_count": 40,
"id": "975c3c2b",
"metadata": {},
"outputs": [],
Expand All @@ -275,7 +267,7 @@
},
{
"cell_type": "code",
"execution_count": 17,
"execution_count": 41,
"id": "784aee3a",
"metadata": {},
"outputs": [],
Expand All @@ -286,7 +278,7 @@
},
{
"cell_type": "code",
"execution_count": 18,
"execution_count": 42,
"id": "dfd0ccc1",
"metadata": {},
"outputs": [
Expand All @@ -299,7 +291,7 @@
" 'answer': '{\\n \"answer\": \"The President nominated Ketanji Brown Jackson as a Circuit Court of Appeals Judge and praised her as one of the nation\\'s top legal minds who will continue Justice Breyer\\'s legacy of excellence.\",\\n \"sources\": [\"31-pl\"]\\n}'}"
]
},
"execution_count": 18,
"execution_count": 42,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -310,7 +302,7 @@
},
{
"cell_type": "code",
"execution_count": 19,
"execution_count": 43,
"id": "c93f805b",
"metadata": {},
"outputs": [],
Expand All @@ -321,7 +313,7 @@
},
{
"cell_type": "code",
"execution_count": 20,
"execution_count": 44,
"id": "5d8612c0",
"metadata": {},
"outputs": [
Expand All @@ -336,7 +328,7 @@
" 'answer': '{\\n \"answer\": \"The President honored Justice Stephen Breyer for his service as an Army veteran, Constitutional scholar, and retiring Justice of the United States Supreme Court.\",\\n \"sources\": [\"31-pl\"]\\n}'}"
]
},
"execution_count": 20,
"execution_count": 44,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -346,18 +338,21 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "ac9e4626",
"metadata": {},
"source": [
"## Using your own output schema\n",
"\n",
"We can change the outputs of our chain by passing in our own schema. The values and descriptions of this schema will inform the function we pass to the OpenAI API, meaning it won't just affect how we parse outputs but will also change the OpenAI output itself. For example we can add a `countries_referenced` parameter to our schema and describe what we want this parameter to mean, and that'll cause the OpenAI output to include a description of a speaker in the response."
"We can change the outputs of our chain by passing in our own schema. The values and descriptions of this schema will inform the function we pass to the OpenAI API, meaning it won't just affect how we parse outputs but will also change the OpenAI output itself. For example we can add a `countries_referenced` parameter to our schema and describe what we want this parameter to mean, and that'll cause the OpenAI output to include a description of a speaker in the response.\n",
"\n",
"In addition to the previous example, we can also add a custom prompt to the chain. This will allow you to add additional context to the response, which can be useful for question answering."
]
},
{
"cell_type": "code",
"execution_count": 21,
"execution_count": 45,
"id": "f34a48f8",
"metadata": {},
"outputs": [],
Expand All @@ -366,22 +361,25 @@
"\n",
"from pydantic import BaseModel, Field\n",
"\n",
"from langchain.chains.openai_functions import create_qa_with_structure_chain"
"from langchain.chains.openai_functions import create_qa_with_structure_chain\n",
"\n",
"from langchain.prompts.chat import ChatPromptTemplate, HumanMessagePromptTemplate\n",
"from langchain.schema import SystemMessage, HumanMessage"
]
},
{
"cell_type": "code",
"execution_count": 24,
"execution_count": 46,
"id": "5647c161",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"CustomResponseSchema(answer=\"He announced that American airspace will be closed off to all Russian flights, further isolating Russia and adding economic pressure. The Ruble has lost 30% of its value and the Russian stock market has lost 40% of its value. He also mentioned providing support to Ukraine in terms of military, economic, and humanitarian assistance. The US is giving more than $1 billion in direct assistance to Ukraine. He clarified that US forces are not engaged in conflict with Russian forces in Ukraine but are deployed to defend NATO allies. He emphasized that Putin's actions have consequences and that the free world is holding him accountable through economic sanctions and targeting Russian oligarchs.\", countries_referenced=['Russia', 'Ukraine'], sources=['4-pl', '5-pl', '2-pl', '3-pl'])"
"CustomResponseSchema(answer=\"He announced that American airspace will be closed off to all Russian flights, further isolating Russia and adding an additional squeeze on their economy. The Ruble has lost 30% of its value and the Russian stock market has lost 40% of its value. He also mentioned that Putin alone is to blame for Russia's reeling economy. The United States and its allies are providing support to Ukraine in their fight for freedom, including military, economic, and humanitarian assistance. The United States is giving more than $1 billion in direct assistance to Ukraine. He made it clear that American forces are not engaged and will not engage in conflict with Russian forces in Ukraine, but they are deployed to defend NATO allies in case Putin decides to keep moving west. He also mentioned that Putin's attack on Ukraine was premeditated and unprovoked, and that the West and NATO responded by building a coalition of freedom-loving nations to confront Putin. The free world is holding Putin accountable through powerful economic sanctions, cutting off Russia's largest banks from the international financial system, and preventing Russia's central bank from defending the Russian Ruble. The U.S. Department of Justice is also assembling a task force to go after the crimes of Russian oligarchs.\", countries_referenced=['AMERICA', 'RUSSIA', 'UKRAINE'], sources=['4-pl', '5-pl', '2-pl', '3-pl'])"
]
},
"execution_count": 24,
"execution_count": 46,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -396,9 +394,25 @@
" ..., description=\"List of sources used to answer the question\"\n",
" )\n",
"\n",
"qa_chain_pydantic = create_qa_with_structure_chain(llm, CustomResponseSchema, output_parser=\"pydantic\")\n",
"\n",
"prompt_messages = [\n",
" SystemMessage(\n",
" content=(\n",
" \"You are a world class algorithm to answer \"\n",
" \"questions in a specific format.\"\n",
" )\n",
" ),\n",
" HumanMessage(content=\"Answer question using the following context\"),\n",
" HumanMessagePromptTemplate.from_template(\"{context}\"),\n",
" HumanMessagePromptTemplate.from_template(\"Question: {question}\"),\n",
" HumanMessage(content=\"Tips: Make sure to answer in the correct format. Return all of the countries mentioned in the sources in uppercase characters.\"),\n",
"]\n",
"\n",
"chain_prompt = ChatPromptTemplate(messages=prompt_messages)\n",
"\n",
"qa_chain_pydantic = create_qa_with_structure_chain(llm, CustomResponseSchema, output_parser=\"pydantic\", prompt=chain_prompt)\n",
"final_qa_chain_pydantic = StuffDocumentsChain(\n",
" llm_chain=qa_chain_pydantic, \n",
" llm_chain=qa_chain_pydantic,\n",
" document_variable_name='context',\n",
" document_prompt=doc_prompt,\n",
")\n",
Expand All @@ -409,21 +423,13 @@
"query = \"What did he say about russia\"\n",
"retrieval_qa_pydantic.run(query)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b822ef59",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "venv",
"display_name": "Python 3",
"language": "python",
"name": "venv"
"name": "python3"
},
"language_info": {
"codemirror_mode": {
Expand All @@ -435,7 +441,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.3"
"version": "3.10.5"
}
},
"nbformat": 4,
Expand Down
6 changes: 4 additions & 2 deletions langchain/chains/openai_functions/qa_with_structure.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Any, List, Type, Union
from typing import Any, List, Optional, Type, Union

from pydantic import BaseModel, Field

Expand All @@ -9,6 +9,7 @@
OutputFunctionsParser,
PydanticOutputFunctionsParser,
)
from langchain.prompts import PromptTemplate
from langchain.prompts.chat import ChatPromptTemplate, HumanMessagePromptTemplate
from langchain.schema import BaseLLMOutputParser, HumanMessage, SystemMessage

Expand All @@ -26,6 +27,7 @@ def create_qa_with_structure_chain(
llm: BaseLanguageModel,
schema: Union[dict, Type[BaseModel]],
output_parser: str = "base",
prompt: Optional[Union[PromptTemplate, ChatPromptTemplate]] = None,
) -> LLMChain:
if output_parser == "pydantic":
if not (isinstance(schema, type) and issubclass(schema, BaseModel)):
Expand Down Expand Up @@ -65,7 +67,7 @@ def create_qa_with_structure_chain(
HumanMessagePromptTemplate.from_template("Question: {question}"),
HumanMessage(content="Tips: Make sure to answer in the correct format"),
]
prompt = ChatPromptTemplate(messages=messages)
prompt = prompt or ChatPromptTemplate(messages=messages)

chain = LLMChain(
llm=llm,
Expand Down