docs: use first_tool_only instead of return_single (#19666)

This commit is contained in:
Bagatur 2024-03-27 11:19:39 -07:00 committed by GitHub
parent bcb8ab5216
commit 5fc6531c74
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 16 additions and 14 deletions

View File

@ -257,7 +257,7 @@
"id": "fc5695c5-451f-482f-bde6-462d85f1a93e", "id": "fc5695c5-451f-482f-bde6-462d85f1a93e",
"metadata": {}, "metadata": {},
"source": [ "source": [
"Certain models can return multiple tool invocations each call, so by default the output is a list. If we just want to return the first tool invocation, we can specify `return_single=True`" "Certain models can return multiple tool invocations each call, so by default the output is a list. If we just want to return the first tool invocation, we can specify `first_tool_only=True`"
] ]
}, },
{ {
@ -279,7 +279,7 @@
} }
], ],
"source": [ "source": [
"parser = JsonOutputKeyToolsParser(key_name=\"Joke\", return_single=True)\n", "parser = JsonOutputKeyToolsParser(key_name=\"Joke\", first_tool_only=True)\n",
"chain = prompt | model | parser\n", "chain = prompt | model | parser\n",
"chain.invoke({\"input\": \"tell me a joke\"})" "chain.invoke({\"input\": \"tell me a joke\"})"
] ]

View File

@ -418,7 +418,7 @@
"source": [ "source": [
"from langchain.output_parsers.openai_tools import JsonOutputKeyToolsParser\n", "from langchain.output_parsers.openai_tools import JsonOutputKeyToolsParser\n",
"\n", "\n",
"parser = JsonOutputKeyToolsParser(tool.name, return_single=True)\n", "parser = JsonOutputKeyToolsParser(tool.name, first_tool_only=True)\n",
"(llm_with_tools | parser).invoke(\n", "(llm_with_tools | parser).invoke(\n",
" \"I have a dataframe 'df' and want to know the correlation between the 'Age' and 'Fare' columns\"\n", " \"I have a dataframe 'df' and want to know the correlation between the 'Age' and 'Fare' columns\"\n",
")" ")"

View File

@ -281,7 +281,7 @@
"source": [ "source": [
"from langchain.output_parsers.openai_tools import JsonOutputKeyToolsParser\n", "from langchain.output_parsers.openai_tools import JsonOutputKeyToolsParser\n",
"\n", "\n",
"output_parser = JsonOutputKeyToolsParser(key_name=\"cited_answer\", return_single=True)\n", "output_parser = JsonOutputKeyToolsParser(key_name=\"cited_answer\", first_tool_only=True)\n",
"(llm_with_tool | output_parser).invoke(example_q)" "(llm_with_tool | output_parser).invoke(example_q)"
] ]
}, },
@ -403,7 +403,9 @@
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"output_parser_2 = JsonOutputKeyToolsParser(key_name=\"quoted_answer\", return_single=True)\n", "output_parser_2 = JsonOutputKeyToolsParser(\n",
" key_name=\"quoted_answer\", first_tool_only=True\n",
")\n",
"llm_with_tool_2 = llm.bind_tools(\n", "llm_with_tool_2 = llm.bind_tools(\n",
" [quoted_answer],\n", " [quoted_answer],\n",
" tool_choice=\"quoted_answer\",\n", " tool_choice=\"quoted_answer\",\n",
@ -785,7 +787,7 @@
"annotation_chain = (\n", "annotation_chain = (\n",
" prompt_5\n", " prompt_5\n",
" | llm_with_tools_5\n", " | llm_with_tools_5\n",
" | JsonOutputKeyToolsParser(key_name=\"annotated_answer\", return_single=True)\n", " | JsonOutputKeyToolsParser(key_name=\"annotated_answer\", first_tool_only=True)\n",
" | itemgetter(\"citations\")\n", " | itemgetter(\"citations\")\n",
")\n", ")\n",
"\n", "\n",

View File

@ -266,7 +266,7 @@
"id": "7f712d8d-0314-4d3d-b563-378b72fd8bb5", "id": "7f712d8d-0314-4d3d-b563-378b72fd8bb5",
"metadata": {}, "metadata": {},
"source": [ "source": [
"Since we know we're always invoking the `multiply` tool, we can simplify our output a bit to return only the args for the `multiply` tool using the `JsonoutputKeyToolsParser`. To further simplify we'll specify `return_single=True`, so that instead of a list of tool invocations our output parser returns only the first tool invocation." "Since we know we're always invoking the `multiply` tool, we can simplify our output a bit to return only the args for the `multiply` tool using the `JsonoutputKeyToolsParser`. To further simplify we'll specify `first_tool_only=True`, so that instead of a list of tool invocations our output parser returns only the first tool invocation."
] ]
}, },
{ {
@ -290,7 +290,7 @@
"from langchain.output_parsers import JsonOutputKeyToolsParser\n", "from langchain.output_parsers import JsonOutputKeyToolsParser\n",
"\n", "\n",
"chain = model_with_tools | JsonOutputKeyToolsParser(\n", "chain = model_with_tools | JsonOutputKeyToolsParser(\n",
" key_name=\"multiply\", return_single=True\n", " key_name=\"multiply\", first_tool_only=True\n",
")\n", ")\n",
"chain.invoke(\"What's four times 23\")" "chain.invoke(\"What's four times 23\")"
] ]
@ -328,7 +328,7 @@
"# Note: the `.map()` at the end of `multiply` allows us to pass in a list of `multiply` arguments instead of a single one.\n", "# Note: the `.map()` at the end of `multiply` allows us to pass in a list of `multiply` arguments instead of a single one.\n",
"chain = (\n", "chain = (\n",
" model_with_tools\n", " model_with_tools\n",
" | JsonOutputKeyToolsParser(key_name=\"multiply\", return_single=True)\n", " | JsonOutputKeyToolsParser(key_name=\"multiply\", first_tool_only=True)\n",
" | multiply\n", " | multiply\n",
")\n", ")\n",
"chain.invoke(\"What's four times 23\")" "chain.invoke(\"What's four times 23\")"

View File

@ -116,7 +116,7 @@
"\n", "\n",
"chain = (\n", "chain = (\n",
" model_with_tools\n", " model_with_tools\n",
" | JsonOutputKeyToolsParser(key_name=\"complex_tool\", return_single=True)\n", " | JsonOutputKeyToolsParser(key_name=\"complex_tool\", first_tool_only=True)\n",
" | complex_tool\n", " | complex_tool\n",
")" ")"
] ]
@ -190,7 +190,7 @@
"\n", "\n",
"chain = (\n", "chain = (\n",
" model_with_tools\n", " model_with_tools\n",
" | JsonOutputKeyToolsParser(key_name=\"complex_tool\", return_single=True)\n", " | JsonOutputKeyToolsParser(key_name=\"complex_tool\", first_tool_only=True)\n",
" | try_except_tool\n", " | try_except_tool\n",
")" ")"
] ]
@ -255,7 +255,7 @@
"source": [ "source": [
"chain = (\n", "chain = (\n",
" model_with_tools\n", " model_with_tools\n",
" | JsonOutputKeyToolsParser(key_name=\"complex_tool\", return_single=True)\n", " | JsonOutputKeyToolsParser(key_name=\"complex_tool\", first_tool_only=True)\n",
" | complex_tool\n", " | complex_tool\n",
")\n", ")\n",
"better_model = ChatOpenAI(model=\"gpt-4-1106-preview\", temperature=0).bind_tools(\n", "better_model = ChatOpenAI(model=\"gpt-4-1106-preview\", temperature=0).bind_tools(\n",
@ -263,7 +263,7 @@
")\n", ")\n",
"better_chain = (\n", "better_chain = (\n",
" better_model\n", " better_model\n",
" | JsonOutputKeyToolsParser(key_name=\"complex_tool\", return_single=True)\n", " | JsonOutputKeyToolsParser(key_name=\"complex_tool\", first_tool_only=True)\n",
" | complex_tool\n", " | complex_tool\n",
")\n", ")\n",
"\n", "\n",
@ -355,7 +355,7 @@
" prompt\n", " prompt\n",
" | model_with_tools\n", " | model_with_tools\n",
" | JsonOutputKeyToolsParser(\n", " | JsonOutputKeyToolsParser(\n",
" key_name=\"complex_tool\", return_id=True, return_single=True\n", " key_name=\"complex_tool\", return_id=True, first_tool_only=True\n",
" )\n", " )\n",
" | tool_custom_exception\n", " | tool_custom_exception\n",
")\n", ")\n",