File size: 11,142 Bytes
c187244 fa36169 c187244 fa36169 c187244 fa36169 c187244 fa36169 c187244 fa36169 c187244 fa36169 c187244 fa36169 c187244 fa36169 c187244 fa36169 c187244 fa36169 c187244 fa36169 c187244 fa36169 c187244 fa36169 c187244 fa36169 c187244 fa36169 c187244 fa36169 c187244 fa36169 c187244 fa36169 c187244 fa36169 c187244 fa36169 c187244 fa36169 c187244 fa36169 c187244 fa36169 c187244 fa36169 c187244 fa36169 c187244 fa36169 c187244 fa36169 c187244 fa36169 c187244 fa36169 c187244 fa36169 c187244 fa36169 c187244 fa36169 c187244 fa36169 c187244 fa36169 c187244 fa36169 c187244 fa36169 c187244 fa36169 c187244 fa36169 c187244 fa36169 c187244 fa36169 c187244 fa36169 c187244 fa36169 c187244 fa36169 c187244 fa36169 c187244 fa36169 c187244 fa36169 c187244 fa36169 c187244 fa36169 c187244 fa36169 c187244 fa36169 c187244 fa36169 c187244 fa36169 c187244 fa36169 c187244 fa36169 c187244 fa36169 c187244 fa36169 c187244 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 |
{
"cells": [
{
"metadata": {},
"cell_type": "markdown",
"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 <a href=\"https://www.hf.co/learn/agents-course\">Hugging Face Agents Course</a>, a free course from beginner to expert, where you learn to build Agents.\n",
"\n",
"\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",
""
],
"id": "89791f21c171372a"
},
{
"metadata": {},
"cell_type": "code",
"outputs": [],
"execution_count": null,
"source": "%pip install -q -U langchain_openai langchain_core langgraph",
"id": "bef6c5514bd263ce"
},
{
"metadata": {},
"cell_type": "code",
"outputs": [],
"execution_count": null,
"source": [
"import os\n",
"\n",
"# Please setp your own key.\n",
"os.environ[\"OPENAI_API_KEY\"] = \"sk-xxxxxx\""
],
"id": "61d0ed53b26fa5c6"
},
{
"metadata": {},
"cell_type": "code",
"outputs": [],
"execution_count": null,
"source": [
"import base64\n",
"from langchain_core.messages import HumanMessage\n",
"from langchain_openai import ChatOpenAI\n",
"\n",
"vision_llm = ChatOpenAI(model=\"gpt-4o\")\n",
"\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",
"\n",
"llm = ChatOpenAI(model=\"gpt-4o\")\n",
"\n",
"\n",
"def divide(a: int, b: int) -> float:\n",
" \"\"\"Divide a and b.\"\"\"\n",
" return a / b\n",
"\n",
"\n",
"tools = [\n",
" divide,\n",
" extract_text\n",
"]\n",
"llm_with_tools = llm.bind_tools(tools, parallel_tool_calls=False)"
],
"id": "a4a8bf0d5ac25a37"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "Let's create our LLM and prompt it with the overall desired agent behavior.",
"id": "3e7c17a2e155014e"
},
{
"metadata": {},
"cell_type": "code",
"outputs": [],
"execution_count": null,
"source": [
"from typing import TypedDict, Annotated, Optional\n",
"from langchain_core.messages import AnyMessage\n",
"from langgraph.graph.message import add_messages\n",
"\n",
"\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]"
],
"id": "f31250bc1f61da81"
},
{
"metadata": {},
"cell_type": "code",
"outputs": [],
"execution_count": null,
"source": [
"from langchain_core.messages import HumanMessage, SystemMessage\n",
"from langchain_core.utils.function_calling import convert_to_openai_tool\n",
"\n",
"\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",
" return {\"messages\": [llm_with_tools.invoke([sys_msg] + state[\"messages\"])], \"input_file\": state[\"input_file\"]}"
],
"id": "3c4a736f9e55afa9"
},
{
"metadata": {},
"cell_type": "markdown",
"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."
],
"id": "6f1efedd943d8b1d"
},
{
"metadata": {},
"cell_type": "code",
"outputs": [],
"execution_count": null,
"source": [
"from langgraph.graph import START, StateGraph\n",
"from langgraph.prebuilt import ToolNode, tools_condition\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()))"
],
"id": "e013061de784638a"
},
{
"metadata": {},
"cell_type": "code",
"outputs": [],
"execution_count": null,
"source": [
"messages = [HumanMessage(content=\"Divide 6790 by 5\")]\n",
"\n",
"messages = react_graph.invoke({\"messages\": messages, \"input_file\": None})"
],
"id": "d3b0ba5be1a54aad"
},
{
"metadata": {},
"cell_type": "code",
"outputs": [],
"execution_count": null,
"source": [
"for m in messages['messages']:\n",
" m.pretty_print()"
],
"id": "55eb0f1afd096731"
},
{
"metadata": {},
"cell_type": "markdown",
"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",
""
],
"id": "e0062c1b99cb4779"
},
{
"metadata": {},
"cell_type": "code",
"outputs": [],
"execution_count": null,
"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\"})"
],
"id": "2e166ebba82cfd2a"
},
{
"metadata": {},
"cell_type": "code",
"outputs": [],
"execution_count": null,
"source": [
"for m in messages['messages']:\n",
" m.pretty_print()"
],
"id": "5bfd67af70b7dcf3"
},
{
"metadata": {},
"cell_type": "code",
"outputs": [],
"execution_count": null,
"source": "",
"id": "8cd664ab5ee5450e"
}
],
"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
}
|