Add Spellcheck and import hogwarts (#419)

This commit is contained in:
William FH
2024-05-08 10:25:11 -07:00
committed by GitHub
parent 7463c6fd44
commit 3e5ea31cdc
39 changed files with 272 additions and 82 deletions
@@ -72,7 +72,7 @@
"id": "d63dbfc7-a5c1-4a03-991c-f0789ba52c52",
"metadata": {},
"source": [
"We can now invoke this executor. The input to this must be a dictionary with a single `messsages` key that contains a list of messages."
"We can now invoke this executor. The input to this must be a dictionary with a single `messages` key that contains a list of messages."
]
},
{
@@ -14,6 +14,146 @@
"Any modifications of that example are called below with **MODIFICATION**, so if you are looking for the differences you can just search for that."
]
},
{
"cell_type": "markdown",
"id": "1977bac1",
"metadata": {},
"source": [
"## Set up the State\n",
"\n",
"The main type of graph in `langgraph` is the [StateGraph](https://langchain-ai.github.io/langgraph/reference/graphs/#langgraph.graph.StateGraph).\n",
"This graph is parameterized by a `State` object that it passes around to each node.\n",
"Each node then returns operations the graph uses to `update` that state.\n",
"These operations can either SET specific attributes on the state (e.g. overwrite the existing values) or ADD to the existing attribute.\n",
"Whether to set or add is denoted by annotating the `State` object you use to construct the graph.\n",
"\n",
"For this example, the state we will track will just be a list of messages.\n",
"We want each node to just add messages to that list.\n",
"Therefore, we will use a `TypedDict` with one key (`messages`) and annotate it so that the `messages` attribute is \"append-only\"."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "de1db3c1",
"metadata": {},
"outputs": [],
"source": [
"from typing_extensions import TypedDict\n",
"from typing import Annotated\n",
"from langgraph.graph.message import add_messages\n",
"\n",
"# Add messages essentially does this with more\n",
"# robust handling\n",
"# def add_messages(left: list, right: list):\n",
"# return left + right\n",
"\n",
"\n",
"class State(TypedDict):\n",
" messages: Annotated[list, add_messages]"
]
},
{
"cell_type": "markdown",
"id": "b8a08594",
"metadata": {},
"source": [
"## Set up the tools\n",
"\n",
"We will first define the tools we want to use.\n",
"For this simple example, we will use create a placeholder search engine.\n",
"It is really easy to create your own tools - see documentation [here](https://python.langchain.com/docs/modules/agents/tools/custom_tools) on how to do that.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "23a2ca43",
"metadata": {},
"outputs": [],
"source": [
"from langchain_core.tools import tool\n",
"\n",
"\n",
"@tool\n",
"def search(query: str):\n",
" \"\"\"Call to surf the web.\"\"\"\n",
" # This is a placeholder, but don't tell the LLM that...\n",
" return [\"The answer to your question lies within.\"]\n",
"\n",
"\n",
"tools = [search]"
]
},
{
"cell_type": "markdown",
"id": "44c73446",
"metadata": {},
"source": [
"We can now wrap these tools in a simple [ToolNode](https://langchain-ai.github.io/langgraph/reference/prebuilt/#toolnode).\n",
"This is a simple class that takes in a list of messages containing an [AIMessages with tool_calls](https://api.python.langchain.com/en/latest/messages/langchain_core.messages.ai.AIMessage.html#langchain_core.messages.ai.AIMessage.tool_calls), runs the tools, and returns the output as [ToolMessage](https://api.python.langchain.com/en/latest/messages/langchain_core.messages.tool.ToolMessage.html#langchain_core.messages.tool.ToolMessage)s.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "979512e4",
"metadata": {},
"outputs": [],
"source": [
"from langgraph.prebuilt import ToolNode\n",
"\n",
"tool_node = ToolNode(tools)"
]
},
{
"cell_type": "markdown",
"id": "b07b9229",
"metadata": {},
"source": [
"## Set up the model\n",
"\n",
"Now we need to load the chat model we want to use.\n",
"This should satisfy two criteria:\n",
"\n",
"1. It should work with messages, since our state is primarily a list of messages (chat history).\n",
"2. It should work with tool calling, since we are using a prebuilt [ToolNode](https://langchain-ai.github.io/langgraph/reference/prebuilt/#toolnode)\n",
"\n",
"**Note:** these model requirements are not requirements for using LangGraph - they are just requirements for this particular example.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1c8132c5",
"metadata": {},
"outputs": [],
"source": [
"from langchain_anthropic import ChatAnthropic\n",
"\n",
"model = ChatAnthropic(model=\"claude-3-haiku-20240307\")"
]
},
{
"cell_type": "markdown",
"id": "979f0310",
"metadata": {},
"source": [
"\n",
"After we've done this, we should make sure the model knows that it has these tools available to call.\n",
"We can do this by converting the LangChain tools into the format for function calling, and then bind them to the model class.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "055d84bf",
"metadata": {},
"outputs": [],
"source": [
"model = model.bind_tools(tools)"
]
},
{
"cell_type": "markdown",
"id": "7cbd446a-808f-4394-be92-d45ab818953c",