{ "cells": [ { "cell_type": "markdown", "id": "98f5e36a-da49-4ae2-8c74-b910a2f992fc", "metadata": {}, "source": [ "# Agent\n", "\n", "In this notebook, **we're going to build a simple agent using using LangGraph**.\n", "\n", "This notebook is part of the Hugging Face Agents Course, a free course from beginner to expert, where you learn to build Agents.\n", "\n", "![Agents course share](https://huggingface.co/datasets/agents-course/course-images/resolve/main/en/communication/share.png)\n", "\n", "As seen in the Unit 1, an agent needs 3 steps as introduced in the ReAct architecture :\n", "[ReAct](https://react-lm.github.io/), a general agent architecture.\n", " \n", "* `act` - let the model call specific tools \n", "* `observe` - pass the tool output back to the model \n", "* `reason` - let the model reason about the tool output to decide what to do next (e.g., call another tool or just respond directly)\n", "\n", "\n", "![Agent](https://huggingface.co/datasets/agents-course/course-images/resolve/main/en/unit2/LangGraph/Agent.png)" ] }, { "cell_type": "code", "execution_count": 1, "id": "63edff5a-724b-474d-9db8-37f0ae936c76", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Note: you may need to restart the kernel to use updated packages.\n" ] } ], "source": [ "%pip install -q -U langchain_openai langchain_core langgraph" ] }, { "cell_type": "code", "execution_count": 2, "id": "356a6482", "metadata": { "tags": [] }, "outputs": [], "source": [ "import os\n", "\n", "# Please setp your own key.\n", "os.environ[\"OPENAI_API_KEY\"]=\"sk-xxxxxx\"" ] }, { "cell_type": "code", "execution_count": 65, "id": "71795ff1-d6a7-448d-8b55-88bbd1ed3dbe", "metadata": { "tags": [] }, "outputs": [], "source": [ "import base64\n", "from typing import List\n", "from langchain.schema import HumanMessage\n", "from langchain_openai import ChatOpenAI\n", "\n", "\n", "vision_llm = ChatOpenAI(model=\"gpt-4o\")\n", "\n", "def extract_text(img_path: str) -> str:\n", " \"\"\"\n", " Extract text from an image file using a multimodal model.\n", "\n", " Args:\n", " img_path: A local image file path (strings).\n", "\n", " Returns:\n", " A single string containing the concatenated text extracted from each image.\n", " \"\"\"\n", " all_text = \"\"\n", " try:\n", " \n", " # Read image and encode as base64\n", " with open(img_path, \"rb\") as image_file:\n", " image_bytes = image_file.read()\n", "\n", " image_base64 = base64.b64encode(image_bytes).decode(\"utf-8\")\n", "\n", " # Prepare the prompt including the base64 image data\n", " message = [\n", " HumanMessage(\n", " content=[\n", " {\n", " \"type\": \"text\",\n", " \"text\": (\n", " \"Extract all the text from this image. \"\n", " \"Return only the extracted text, no explanations.\"\n", " ),\n", " },\n", " {\n", " \"type\": \"image_url\",\n", " \"image_url\": {\n", " \"url\": f\"data:image/png;base64,{image_base64}\"\n", " },\n", " },\n", " ]\n", " )\n", " ]\n", "\n", " # Call the vision-capable model\n", " response = vision_llm.invoke(message)\n", "\n", " # Append extracted text\n", " all_text += response.content + \"\\n\\n\"\n", "\n", " return all_text.strip()\n", " except Exception as e:\n", " # You can choose whether to raise or just return an empty string / error message\n", " error_msg = f\"Error extracting text: {str(e)}\"\n", " print(error_msg)\n", " return \"\"\n", "\n", "llm = ChatOpenAI(model=\"gpt-4o\")\n", "\n", "def divide(a: int, b: int) -> float:\n", " \"\"\"Divide a and b.\"\"\"\n", " return a / b\n", "\n", "tools = [\n", " divide,\n", " extract_text\n", "]\n", "llm_with_tools = llm.bind_tools(tools, parallel_tool_calls=False)" ] }, { "cell_type": "markdown", "id": "a2cec014-3023-405c-be79-de8fc7adb346", "metadata": {}, "source": [ "Let's create our LLM and prompt it with the overall desired agent behavior." ] }, { "cell_type": "code", "execution_count": 66, "id": "deb674bc-49b2-485a-b0c3-4d7b05a0bfac", "metadata": { "tags": [] }, "outputs": [], "source": [ "from typing import TypedDict, Annotated, List, Any, Optional\n", "from langchain_core.messages import AnyMessage\n", "from langgraph.graph.message import add_messages\n", "class AgentState(TypedDict):\n", " # The input document\n", " input_file: Optional[str] # Contains file path, type (PNG)\n", " messages: Annotated[list[AnyMessage], add_messages]" ] }, { "cell_type": "code", "execution_count": 76, "id": "d061813f-ebc0-432c-91ec-3b42b15c30b6", "metadata": { "tags": [] }, "outputs": [], "source": [ "from langchain_core.messages import HumanMessage, SystemMessage\n", "from langchain_core.utils.function_calling import convert_to_openai_tool\n", "\n", "\n", "# AgentState\n", "def assistant(state: AgentState):\n", " # System message\n", " textual_description_of_tool=\"\"\"\n", "extract_text(img_path: str) -> str:\n", " Extract text from an image file using a multimodal model.\n", "\n", " Args:\n", " img_path: A local image file path (strings).\n", "\n", " Returns:\n", " A single string containing the concatenated text extracted from each image.\n", "divide(a: int, b: int) -> float:\n", " Divide a and b\n", "\"\"\"\n", " image=state[\"input_file\"]\n", " sys_msg = SystemMessage(content=f\"You are an helpful agent that can analyse some images and run some computatio without provided tools :\\n{textual_description_of_tool} \\n You have access to some otpional images. Currently the loaded images is : {image}\")\n", "\n", "\n", " return {\"messages\": [llm_with_tools.invoke([sys_msg] + state[\"messages\"])],\"input_file\":state[\"input_file\"]}" ] }, { "cell_type": "markdown", "id": "4eb43343-9a6f-42cb-86e6-4380f928633c", "metadata": {}, "source": [ "We define a `Tools` node with our list of tools.\n", "\n", "The `Assistant` node is just our model with bound tools.\n", "\n", "We create a graph with `Assistant` and `Tools` nodes.\n", "\n", "We add `tools_condition` edge, which routes to `End` or to `Tools` based on whether the `Assistant` calls a tool.\n", "\n", "Now, we add one new step:\n", "\n", "We connect the `Tools` node *back* to the `Assistant`, forming a loop.\n", "\n", "* After the `assistant` node executes, `tools_condition` checks if the model's output is a tool call.\n", "* If it is a tool call, the flow is directed to the `tools` node.\n", "* The `tools` node connects back to `assistant`.\n", "* This loop continues as long as the model decides to call tools.\n", "* If the model response is not a tool call, the flow is directed to END, terminating the process." ] }, { "cell_type": "code", "execution_count": 77, "id": "aef13cd4-05a6-4084-a620-2e7b91d9a72f", "metadata": { "tags": [] }, "outputs": [ { "data": { "image/png": "iVBORw0KGgoAAAANSUhEUgAAANYAAAD5CAIAAADUe1yaAAAAAXNSR0IArs4c6QAAIABJREFUeJztnXdcU1fj/89NQnYChD1kiQgIjooTXFXqI44fUKt11Grr86271tX66GPt0Nplfdo+1rb6WBXrnlgVrK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SmYMi+H3H+MhEKM7fYehHQWbMOjoviUdQQAu3wGaakQLOqogAkESqNlAQAYpiIAMUhABGaQgAjJIQQRkkIIIyPx/ohlWIXXfCHUAAAAASUVORK5CYII=", "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "from langgraph.graph import START, StateGraph\n", "from langgraph.prebuilt import tools_condition\n", "from langgraph.prebuilt import ToolNode\n", "from IPython.display import Image, display\n", "\n", "# Graph\n", "builder = StateGraph(AgentState)\n", "\n", "# Define nodes: these do the work\n", "builder.add_node(\"assistant\", assistant)\n", "builder.add_node(\"tools\", ToolNode(tools))\n", "\n", "# Define edges: these determine how the control flow moves\n", "builder.add_edge(START, \"assistant\")\n", "builder.add_conditional_edges(\n", " \"assistant\",\n", " # If the latest message (result) from assistant is a tool call -> tools_condition routes to tools\n", " # If the latest message (result) from assistant is a not a tool call -> tools_condition routes to END\n", " tools_condition,\n", ")\n", "builder.add_edge(\"tools\", \"assistant\")\n", "react_graph = builder.compile()\n", "\n", "# Show\n", "display(Image(react_graph.get_graph(xray=True).draw_mermaid_png()))" ] }, { "cell_type": "code", "execution_count": 78, "id": "75602459-d8ca-47b4-9518-3f38343ebfe4", "metadata": { "tags": [] }, "outputs": [], "source": [ "messages = [HumanMessage(content=\"Divide 6790 by 5\")]\n", "\n", "messages = react_graph.invoke({\"messages\": messages,\"input_file\":None})" ] }, { "cell_type": "code", "execution_count": 79, "id": "b517142d-c40c-48bf-a5b8-c8409427aa79", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "================================\u001b[1m Human Message \u001b[0m=================================\n", "\n", "Divide 6790 by 5\n", "==================================\u001b[1m Ai Message \u001b[0m==================================\n", "Tool Calls:\n", " divide (call_s0G5ewtIQyHUCOv0fClsCpgh)\n", " Call ID: call_s0G5ewtIQyHUCOv0fClsCpgh\n", " Args:\n", " a: 6790\n", " b: 5\n", "=================================\u001b[1m Tool Message \u001b[0m=================================\n", "Name: divide\n", "\n", "1358.0\n", "==================================\u001b[1m Ai Message \u001b[0m==================================\n", "\n", "The result of dividing 6790 by 5 is 1358.0.\n" ] } ], "source": [ "for m in messages['messages']:\n", " m.pretty_print()" ] }, { "cell_type": "markdown", "id": "08386393-c270-43a5-bde2-2b4075238971", "metadata": {}, "source": [ "## Training program\n", "MR Wayne left a note with his training program for the week. I came up with a recipe for dinner leaft in a note.\n", "\n", "you can find the document [HERE](https://huggingface.co/datasets/agents-course/course-images/blob/main/en/unit2/LangGraph/Batman_training_and_meals.png), so download it and upload it in the local folder.\n", "\n", "![Training](https://huggingface.co/datasets/agents-course/course-images/resolve/main/en/unit2/LangGraph/Batman_training_and_meals.png)" ] }, { "cell_type": "code", "execution_count": 82, "id": "f6e97e84-3b05-4aaf-a38f-1de9b73cd37f", "metadata": { "tags": [] }, "outputs": [], "source": [ "messages = [HumanMessage(content=\"According the note provided by MR wayne in the provided images. What's the list of items I should buy for the dinner menu ?\")]\n", "\n", "messages = react_graph.invoke({\"messages\": messages,\"input_file\":\"Batman_training_and_meals.png\"})" ] }, { "cell_type": "code", "execution_count": 83, "id": "17686d52-c7ba-407b-a13f-f6c37668e5b0", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "================================\u001b[1m Human Message \u001b[0m=================================\n", "\n", "According the note provided by MR wayne in the provided images. What's the list of tiems I should buy for the dinner menu ?\n", "==================================\u001b[1m Ai Message \u001b[0m==================================\n", "Tool Calls:\n", " extract_text (call_JalVBOR82hwRknFcplnLoTtG)\n", " Call ID: call_JalVBOR82hwRknFcplnLoTtG\n", " Args:\n", " img_path: Batman_training_and_meals.png\n", "=================================\u001b[1m Tool Message \u001b[0m=================================\n", "Name: extract_text\n", "\n", "TRAINING SCHEDULE\n", "For the week of 2/20-2/26\n", "\n", "SUNDAY 2/20\n", "MORNING\n", "30 minute jog\n", "30 minute meditation\n", "\n", "EVENING\n", "clean and jerk lifts—3 reps/8 sets. 262 lbs.\n", "5 sets metabolic conditioning:\n", "10 mile run\n", "12 kettlebell swings\n", "12 pull-ups\n", "30 minutes flexibility\n", "30 minutes sparring\n", "\n", "MONDAY 2/21\n", "MORNING\n", "30 minute jog\n", "30 minutes traditional kata (focus on Japanese forms)\n", "\n", "EVENING\n", "5 sets 20 foot rope climb\n", "30 minutes gymnastics (work on muscle ups in\n", "particular)\n", "high bar jumps—12 reps/8 sets\n", "crunches—50 reps/5 sets\n", "30 minutes heavy bag\n", "30 minutes flexibility\n", "20 minutes target practice\n", "\n", "TUESDAY 2/22\n", "MORNING\n", "30 minute jog\n", "30 minutes yoga\n", "\n", "EVENING\n", "off day\n", "leg heavy dead lift—5 reps/7 sets. 600 lbs.\n", "clean and jerk lift—3 reps/10 sets\n", "30 minutes sparring\n", "\n", "WEDNESDAY 2/23\n", "OFF DAY\n", "\n", "MORNING\n", "20-mile run—last week’s time was 4:50 per mile.\n", "Need to better that time by a half a minute.\n", "\n", "EVENING\n", "skill training only\n", "30 minutes yoga\n", "30 minutes meditation\n", "30 minutes body basics\n", "30 minutes bow basics\n", "30 minutes sword basics\n", "30 minutes observational\n", "exercise\n", "30 minutes kata\n", "30 minutes pressure points\n", "30 minutes modus and pressure points\n", "\n", "THURSDAY 2/24\n", "MORNING\n", "30 minute jog\n", "30 minute meditation\n", "30 minutes traditional kata\n", "(focus on Japanese forms)\n", "\n", "EVENING\n", "squats—10 reps/5 sets. 525 lbs.\n", "30 minutes flexibility\n", "crunches—50 reps/5 sets\n", "20 minutes target practice\n", "30 minutes heavy bag\n", "\n", "FRIDAY 2/25\n", "MORNING\n", "30 minute jog\n", "30 minute meditation\n", "\n", "EVENING\n", "clean and jerk lifts—3 reps/8 sets. 262 lbs.\n", "5 sets metabolic conditioning:\n", "10 mile run\n", "12 kettlebell swings\n", "12 pull-ups\n", "30 minutes flexibility\n", "30 minutes sparring\n", "\n", "SATURDAY 2/26)\n", "MORNING\n", "30 minute jog\n", "30 minutes yoga\n", "\n", "EVENING\n", "crunches—50 reps/5 sets\n", "squats—(5 reps/10 sets. 525 lbs.\n", "push-ups—60 reps/sets\n", "30 minutes monkey bars\n", "30 minute pommel horse\n", "30 minutes heavy bag\n", "2 mile swim\n", "\n", "In an effort to inspire the all- important Dark Knight to take time out of his busy schedule and actually consume a reasonable amount of sustenance, I have taken the liberty of composing a menu for today's scheduled natal to its my hope that these elegantly prepared courses will not share the fate of their predecessors -mated cold and untouched on a computer console.\n", "-A\n", "\n", "W A Y N E M A N O R\n", "\n", "Tuesday's Menu\n", "\n", "Breakfast\n", "six poached eggs laid over artichoke bottoms with a sage pesto sauce\n", "thinly sliced baked ham\n", "mixed organic fresh fruit bowl\n", "freshly squeezed orange juice\n", "organic, grass-fed milk\n", "4 grams branched-chain amino acid\n", "2 grams fish oil\n", "\n", "Lunch\n", "local salmon with a ginger glaze\n", "organic asparagus with lemon garlic dusting\n", "Asian yam soup with diced onions\n", "2 grams fish oil\n", "\n", "Dinner\n", "grass-fed local sirloin steak\n", "bed of organic spinach and piquillo peppers\n", "oven-baked golden herb potato\n", "2 grams fish oil\n", "==================================\u001b[1m Ai Message \u001b[0m==================================\n", "\n", "For the dinner menu, you should buy the following items:\n", "\n", "1. Grass-fed local sirloin steak\n", "2. Organic spinach\n", "3. Piquillo peppers\n", "4. Potatoes (for oven-baked golden herb potato)\n", "5. Fish oil (2 grams)\n", "\n", "Ensure the steak is grass-fed and the spinach and peppers are organic for the best quality meal.\n" ] } ], "source": [ "for m in messages['messages']:\n", " m.pretty_print()" ] }, { "cell_type": "code", "execution_count": null, "id": "b96c8456-4093-4cd6-bc5a-f611967ab709", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.5" } }, "nbformat": 4, "nbformat_minor": 5 }