fix openai qa chain (#6487)

master
Davis Chase 11 months ago committed by GitHub
parent 10adec5f1b
commit b7ad4c4c30
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -17,16 +17,7 @@
"execution_count": 1,
"id": "f059012e",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/Users/harrisonchase/.pyenv/versions/3.9.1/envs/langchain/lib/python3.9/site-packages/deeplake/util/check_latest_version.py:32: UserWarning: A newer version of deeplake (3.6.4) is available. It's recommended that you update to the latest version using `pip install -U deeplake`.\n",
" warnings.warn(\n"
]
}
],
"outputs": [],
"source": [
"from langchain.chains import RetrievalQA\n",
"from langchain.document_loaders import TextLoader\n",
@ -152,7 +143,7 @@
{
"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 remarks 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 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}'"
]
},
"execution_count": 10,
@ -220,7 +211,7 @@
{
"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 economic impact of sanctions on Russia and the release of oil reserves to help mitigate gas prices. Overall, the President conveyed a message of solidarity with Ukraine and a determination to protect American interests and support freedom.\", 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 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'])"
]
},
"execution_count": 14,
@ -244,7 +235,7 @@
},
{
"cell_type": "code",
"execution_count": 21,
"execution_count": 15,
"id": "18e5f090",
"metadata": {},
"outputs": [],
@ -354,10 +345,75 @@
"result"
]
},
{
"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."
]
},
{
"cell_type": "code",
"execution_count": 21,
"id": "f34a48f8",
"metadata": {},
"outputs": [],
"source": [
"from typing import List\n",
"\n",
"from pydantic import BaseModel, Field\n",
"\n",
"from langchain.chains.openai_functions import create_qa_with_structure_chain"
]
},
{
"cell_type": "code",
"execution_count": 24,
"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'])"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"class CustomResponseSchema(BaseModel):\n",
" \"\"\"An answer to the question being asked, with sources.\"\"\"\n",
"\n",
" answer: str = Field(..., description=\"Answer to the question that was asked\")\n",
" countries_referenced: List[str] = Field(..., description=\"All of the countries mentioned in the sources\")\n",
" sources: List[str] = Field(\n",
" ..., 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",
"final_qa_chain_pydantic = StuffDocumentsChain(\n",
" llm_chain=qa_chain_pydantic, \n",
" document_variable_name='context',\n",
" document_prompt=doc_prompt,\n",
")\n",
"retrieval_qa_pydantic = RetrievalQA(\n",
" retriever=docsearch.as_retriever(),\n",
" combine_documents_chain=final_qa_chain_pydantic\n",
")\n",
"query = \"What did he say about russia\"\n",
"retrieval_qa_pydantic.run(query)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e8d07f6f",
"id": "b822ef59",
"metadata": {},
"outputs": [],
"source": []
@ -365,9 +421,9 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "venv",
"language": "python",
"name": "python3"
"name": "venv"
},
"language_info": {
"codemirror_mode": {
@ -379,7 +435,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.1"
"version": "3.11.3"
}
},
"nbformat": 4,

@ -1,4 +1,4 @@
from typing import Any, List
from typing import Any, List, Type, Union
from pydantic import BaseModel, Field
@ -23,9 +23,16 @@ class AnswerWithSources(BaseModel):
def create_qa_with_structure_chain(
llm: BaseLanguageModel, schema: Any, output_parser: str = "base"
llm: BaseLanguageModel,
schema: Union[dict, Type[BaseModel]],
output_parser: str = "base",
) -> LLMChain:
if output_parser == "pydantic":
if not (isinstance(schema, type) and issubclass(schema, BaseModel)):
raise ValueError(
"Must provide a pydantic class for schema when output_parser is "
"'pydantic'."
)
_output_parser: BaseLLMOutputParser = PydanticOutputFunctionsParser(
pydantic_schema=schema
)
@ -36,11 +43,14 @@ def create_qa_with_structure_chain(
f"Got unexpected output_parser: {output_parser}. "
f"Should be one of `pydantic` or `base`."
)
schema = AnswerWithSources.schema()
if isinstance(schema, type) and issubclass(schema, BaseModel):
schema_dict = schema.schema()
else:
schema_dict = schema
function = {
"name": schema["title"],
"description": schema["description"],
"parameters": schema,
"name": schema_dict["title"],
"description": schema_dict["description"],
"parameters": schema_dict,
}
llm_kwargs = get_llm_kwargs(function)
messages = [

Loading…
Cancel
Save