harrison/router_docs
Harrison Chase 2 years ago
parent 1c6f64021d
commit 4de8b089aa

@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "markdown",
"id": "f6766398",
"id": "b5db08e0",
"metadata": {},
"source": [
"# Custom Routing Chains\n",
@ -12,7 +12,7 @@
},
{
"cell_type": "markdown",
"id": "f909b220",
"id": "a922e870",
"metadata": {},
"source": [
"## Terminology\n",
@ -32,7 +32,7 @@
},
{
"cell_type": "markdown",
"id": "7e14d6e8",
"id": "a9f99ac7",
"metadata": {},
"source": [
"## Router\n",
@ -88,15 +88,61 @@
},
{
"cell_type": "markdown",
"id": "8f4730a3",
"id": "4f8835fc",
"metadata": {},
"source": [
"In order to understand why the router interface is what it is, let's take a look at how it is used in the RoutingChain class:\n",
"\n",
"```python\n",
"def _call(self, inputs: Dict[str, str]) -> Dict[str, str]:\n",
" # Construct a mapping of tool name to tool for easy lookup\n",
" name_to_tool_map = {tc.tool_name: tc.tool for tc in self.tool_configs}\n",
" # Construct the initial string to pass into the router. This is made up\n",
" # of the user input, the special starter string, and then the router prefix.\n",
" # The starter string is a special string that may be used by a router to\n",
" # immediately follow the user input. The router prefix is a string that\n",
" # prompts the router to start routing.\n",
" starter_string = (\n",
" inputs[self.input_key]\n",
" + self.router.starter_string\n",
" + self.router.router_prefix\n",
" )\n",
" # We use the ChainedInput class to iteratively add to the input over time.\n",
" chained_input = ChainedInput(starter_string, verbose=self.verbose)\n",
" # We construct a mapping from each tool to a color, used for logging.\n",
" color_mapping = get_color_mapping(\n",
" [c.tool_name for c in self.tool_configs], excluded_colors=[\"green\"]\n",
" )\n",
" # We now enter the router loop (until it returns something).\n",
" while True:\n",
" # Call the router to see what to do.\n",
" output = self.router.route(chained_input.input)\n",
" # Add the log to the Chained Input.\n",
" chained_input.add(output.log, color=\"green\")\n",
" # If the tool chosen is the finishing tool, then we end and return.\n",
" if output.tool == self.router.finish_tool_name:\n",
" return {self.output_key: output.tool_input}\n",
" # Otherwise we lookup the tool\n",
" chain = name_to_tool_map[output.tool]\n",
" # We then call the tool on the tool input to get an observation\n",
" observation = chain(output.tool_input)\n",
" # We then log the observation\n",
" chained_input.add(f\"\\n{self.router.observation_prefix}\")\n",
" chained_input.add(observation, color=color_mapping[output.tool])\n",
" # We then add the router prefix into the prompt to get the router to start\n",
" # thinking, and start the loop all over.\n",
" chained_input.add(f\"\\n{self.router.router_prefix}\")\n",
"\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d22d0f7a",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {

Loading…
Cancel
Save