diff --git a/examples/function_calling/fireworks_langgraph_tool_usage.ipynb b/examples/function_calling/fireworks_langgraph_tool_usage.ipynb new file mode 100644 index 0000000..08df4d3 --- /dev/null +++ b/examples/function_calling/fireworks_langgraph_tool_usage.ipynb @@ -0,0 +1,458 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "XR-OOx5vQbsA" + }, + "source": [ + "[![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/fw-ai/cookbook/blob/main/examples/function_calling/fireworks_langgraph_tool_usage.ipynb)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "HSmb-rejDOnT" + }, + "source": [ + "# Introduction - Fireworks x LangGraph\n", + "\n", + "In this notebook, we demonstrate how to use the Fireworks function-calling model as a router across multiple models with specialized capabilities. Function-calling models have seen a rapid rise in usage due to their ability to easily utilize external tools. One such powerful tool is other LLMs. We have a variety of specialized OSS LLMs for [coding](https://www.deepseek.com/), [chatting in certain languages](https://github.com/QwenLM/Qwen), or just plain [HF Assistants](https://huggingface.co/chat/assistants).\n", + "\n", + "The function-calling model allows us to:\n", + "1. Analyze the user query for intent.\n", + "2. Find the best model to answer the request, which could even be the function-calling model itself!\n", + "3. Construct the right query for the chosen LLM.\n", + "4. Profit!\n", + "\n", + "This notebook is a sister notebook to LangChain, though we will use the newer and more controllable[LangGraph](https://www.langchain.com/langgraph) framework to construct an agent graph capable of chit-chatting and solving math equations using a calculator tool.\n", + "\n", + "This agent chain will utilize [custom-defined tools](https://langchain-ai.github.io/langgraph/how-tos/tool-calling/) capable of executing a math query using an LLM. The main routing LLM will be the Fireworks function-calling model.\n", + "\n", + "\n", + "\n", + "---\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "s40suaVseAN6" + }, + "outputs": [], + "source": [ + "!pip install langgraph langchain-fireworks" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "pYRSabqIL35i" + }, + "source": [ + "## Setup Dependencies\n", + "\n", + "To accomplish the task in this notebook, we need to import some dependencies from LangChain to interface with the model. Specifically, we will use the [ChatFireworks](https://python.langchain.com/v0.2/docs/integrations/chat/fireworks/) implementation.\n", + "\n", + "For solving our math equations, we will use the recently released [Firefunction V2](https://fireworks.ai/blog/firefunction-v2-launch-post) and interface with it using the [Fireworks Inference Service](https://fireworks.ai/models).\n", + "\n", + "To use the Fireworks AI function-calling model, you must first obtain Fireworks API keys. If you don't already have one, you can get one by following the instructions [here](https://readme.fireworks.ai/docs/quickstart). When prompted below paste in your `FIREWORKS_API_KEY`.\n", + "\n", + "**NOTE:** It's important to set the temperature to 0.0 for the function-calling model because we want reliable behavior in routing.\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "rVndeZE_eMFi" + }, + "outputs": [], + "source": [ + "import getpass\n", + "import os\n", + "\n", + "from langchain_fireworks import ChatFireworks\n", + "\n", + "if \"FIREWORKS_API_KEY\" not in os.environ:\n", + " os.environ[\"FIREWORKS_API_KEY\"] = getpass.getpass(\"Your Fireworks API Key:\")\n", + "\n", + "# Initialize a Fireworks chat model\n", + "llm = ChatFireworks(\n", + " model=\"accounts/fireworks/models/firefunction-v2\",\n", + " temperature=0.0,\n", + " max_tokens=256\n", + " )" + ] + }, + { + "cell_type": "markdown", + "source": [ + "## Base LangGraph" + ], + "metadata": { + "id": "RoiXlEo0SxUR" + } + }, + { + "cell_type": "code", + "source": [ + "from typing import Annotated, TypedDict\n", + "\n", + "from langchain_core.messages import BaseMessage, HumanMessage\n", + "from langgraph.graph import START, END, StateGraph\n", + "from langgraph.graph.message import AnyMessage, add_messages\n", + "\n", + "# This is the default state same as \"MessageState\" TypedDict but allows us accessibility to\n", + "# custom keys to our state like user's details\n", + "class GraphsState(TypedDict):\n", + " messages: Annotated[list[AnyMessage], add_messages]\n", + " # user_id: int\n", + "\n", + "graph = StateGraph(GraphsState)\n", + "\n", + "def _call_model(state: GraphsState):\n", + " messages = state[\"messages\"]\n", + " response = llm.invoke(messages)\n", + " return {\"messages\": [response]}\n", + "\n", + "graph.add_edge(START, \"modelNode\")\n", + "graph.add_node(\"modelNode\", _call_model)\n", + "graph.add_edge(\"modelNode\", END)\n", + "\n", + "graph_runnable = graph.compile()" + ], + "metadata": { + "id": "qF1Iqw7dSwp4" + }, + "execution_count": 10, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "#We can visualize it using Mermaid\n", + "from IPython.display import Image, display\n", + "\n", + "try:\n", + " display(Image(graph_runnable.get_graph().draw_mermaid_png()))\n", + "except Exception:\n", + " # This requires some extra dependencies and is optional\n", + " pass" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 236 + }, + "id": "uzu0szKEYnnS", + "outputId": "8d060e8f-c327-4933-ffce-90fab3d0764e" + }, + "execution_count": 11, + "outputs": [ + { + "output_type": "display_data", + "data": { + "image/jpeg": "/9j/4AAQSkZJRgABAQAAAQABAAD/4gHYSUNDX1BST0ZJTEUAAQEAAAHIAAAAAAQwAABtbnRyUkdCIFhZWiAH4AABAAEAAAAAAABhY3NwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAQAA9tYAAQAAAADTLQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAlkZXNjAAAA8AAAACRyWFlaAAABFAAAABRnWFlaAAABKAAAABRiWFlaAAABPAAAABR3dHB0AAABUAAAABRyVFJDAAABZAAAAChnVFJDAAABZAAAAChiVFJDAAABZAAAAChjcHJ0AAABjAAAADxtbHVjAAAAAAAAAAEAAAAMZW5VUwAAAAgAAAAcAHMAUgBHAEJYWVogAAAAAAAAb6IAADj1AAADkFhZWiAAAAAAAABimQAAt4UAABjaWFlaIAAAAAAAACSgAAAPhAAAts9YWVogAAAAAAAA9tYAAQAAAADTLXBhcmEAAAAAAAQAAAACZmYAAPKnAAANWQAAE9AAAApbAAAAAAAAAABtbHVjAAAAAAAAAAEAAAAMZW5VUwAAACAAAAA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pnDVizVjfFnTslviTY9RyIV/uFiTbXre8NNvvolRnVOJWlZSwpK1oUE7SBnmEkjkKuR4M6CIAOjbGQBgZgNcv/AG1YbDpy1aXg+JWe3RbXE3lfQRGkto3HtOAMZ5Cs505VVhmkvH0Bklk0ixC1jw9uNmsl7hQnZl0mzVXcuuvtuLjhsLeUtSyjfsTgKIPPszkVDTtN3q3T5178hT7hCtmvXro5b2o5Lr8dUVDYfZQcdJtWoqG3tIVjmK9C0rN7JG1k7Z38EvQGYcMZEq78R9e3pdoulrgzW7cIyrnDXHU7sbcCiAoZ5EjPeMjPbWn1Gag0vZ9WQ0RL1bIl1jNuB1DMxlLqErAICgFA88EjPpNQKeDWg0hQGjrGAoYIEBrmM558vQK2hGdNYVZ8Xxtxd9GC407KrVl4aaS03cW59q01ardNbBCJEWIhtxIIwcKAzzBIq96S0c/r+eqI1ubtTSts6YnIAT3tIPe4ew4PwAdx57QrqVVUoOpVyS/uoJXNa4MRVxeGtnLgx4x00tPLHwHXluoP7qxV2rgyy3HaQ00hLTSEhKEIGEpA5AAdwrnXzOtU31WVTpNvvO3mxSlKxIda422JeITsOdGamRXRhxl9AWhQ9IPKqBO4EWF97fDnXW1oznoo8hLiP/mSsgegED4sVpFK9NHaa2z/AIpNFuZZ9oGD8pb3+yL7in2gYPylvf7IvuK1Oler4ntf7PL2FzLPtAwflLe/2RfcU+0DB+Ut7/ZF9xWp0p8T2v8AZ5ewuZZ9oGD8pb3+yL7in2gYPylvf7IvuK1OlPie1/s8vYXM5g8CdPsPb5su53VOc9FJkBCP7sNJRkeg5Hx5q/QYEa1w2okOO1EitJ2tsMICEIHxBI5AV2KV5a201q/5ZNi4pSleYh//2Q==\n", + "text/plain": [ + "" + ] + }, + "metadata": {} + } + ] + }, + { + "cell_type": "code", + "source": [ + "# simple Fireworks x LangGraph implementation\n", + "resp = graph_runnable.invoke({\"messages\": HumanMessage(\"What is your name?\")})\n", + "resp[\"messages\"][-1].pretty_print()" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "PEg4XDmkT8X8", + "outputId": "f95dd47f-3843-40b8-be67-ae5b0d919f28" + }, + "execution_count": 12, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "==================================\u001b[1m Ai Message \u001b[0m==================================\n", + "\n", + "I'm an AI assistant, and I don't have a personal name. I'm here to help you with any questions or tasks you may have.\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "EGjCfWemNsse" + }, + "source": [ + "## Custom Tools\n", + "\n", + "To seamlessly use the function-calling ability of the models, we can utilize the [Tool Node](https://langchain-ai.github.io/langgraph/how-tos/tool-calling/) functionality built into LangGraph. This allows the model to select the appropriate tool based on the given options.\n", + "\n", + "For this notebook, we will construct an `area_of_circle` function, which will use an LLM to calculate and return the area of a circle given a radius `r`.\n" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": { + "id": "50g_NBE_Atn5" + }, + "outputs": [], + "source": [ + "import math\n", + "\n", + "from langchain_core.messages import AIMessage\n", + "from langchain_core.tools import tool\n", + "from langgraph.prebuilt import ToolNode\n", + "\n", + "@tool\n", + "def get_weather(location: str):\n", + " \"\"\"Call to get the fake current weather\"\"\"\n", + " if location.lower() in [\"sf\", \"san francisco\"]:\n", + " return \"It's 60 degrees and foggy.\"\n", + " else:\n", + " return \"It's 90 degrees and sunny.\"\n", + "\n", + "@tool\n", + "def area_of_circle(r: float):\n", + " \"\"\"Call to get the area of a circle in units squared\"\"\"\n", + " return math.pi * r * r\n", + "\n", + "\n", + "tools = [get_weather, area_of_circle]\n", + "tool_node = ToolNode(tools)\n", + "\n", + "model_with_tools = llm.bind_tools(tools)" + ] + }, + { + "cell_type": "markdown", + "source": [ + "now let's adjust the graph to include the ToolNode" + ], + "metadata": { + "id": "iS_NBVgJVKEa" + } + }, + { + "cell_type": "code", + "source": [ + "from typing import Literal\n", + "from langgraph.graph import START, END, StateGraph, MessagesState\n", + "\n", + "#note for clarity, I am treating this cell as if `Base LangGraph` was not instantiated, but `Setup Dep` was.\n", + "\n", + "def call_model(state: MessagesState):\n", + " messages = state[\"messages\"]\n", + " response = model_with_tools.invoke(messages)\n", + " return {\"messages\": [response]}\n", + "\n", + "def tool_handler(state: MessagesState) -> Literal[\"tools\", \"__end__\"]:\n", + " messages = state[\"messages\"]\n", + " last_message = messages[-1]\n", + " if last_message.tool_calls:\n", + " return \"tools\"\n", + " return END\n", + "\n", + "workflow = StateGraph(MessagesState)\n", + "\n", + "workflow.add_edge(START, \"modelNode\")\n", + "workflow.add_node(\"modelNode\", call_model)\n", + "workflow.add_conditional_edges(\n", + " \"modelNode\",\n", + " tool_handler,\n", + ")\n", + "workflow.add_node(\"tools\", tool_node)\n", + "workflow.add_edge(\"tools\", \"modelNode\")\n", + "\n", + "app = workflow.compile()" + ], + "metadata": { + "id": "xGMAnlxDT6T-" + }, + "execution_count": 6, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "from IPython.display import Image, display\n", + "\n", + "try:\n", + " display(Image(app.get_graph().draw_mermaid_png()))\n", + "except Exception:\n", + " # This requires some extra dependencies and is optional\n", + " pass" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 236 + }, + "id": "q7say9gHYkNm", + "outputId": "e500406c-6612-4a58-a3ff-8567ba199e49" + }, + "execution_count": 7, + "outputs": [ + { + "output_type": "display_data", + "data": { + "image/jpeg": "/9j/4AAQSkZJRgABAQAAAQABAAD/4gHYSUNDX1BST0ZJTEUAAQEAAAHIAAAAAAQwAABtbnRyUkdCIFhZWiAH4AABAAEAAAAAAABhY3NwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAQAA9tYAAQAAAADTLQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAlkZXNjAAAA8AAAACRyWFlaAAABFAAAABRnWFlaAAABKAAAABRiWFlaAAABPAAAABR3dHB0AAABUAAAABRyVFJDAAABZAAAAChnVF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/1R/8AhW0qU+J6X+56GUKsHCDHLDIbkqYeuktshSHri52vKR3EI0EA/OEg1NaUriu3rl6rWuVTM9pkpSlaUf/Z\n", + "text/plain": [ + "" + ] + }, + "metadata": {} + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "n2FKMb9gORy-" + }, + "source": [ + "### Test Chit Chat Ability\n", + "\n", + "As we outlined in the beginning, the model should be able to both chit-chat, route queries to external tools when necessary or answer from internal knowledge.\n", + "\n", + "Let's first start with a question that can be answered from internal knowledge." + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "A18JJlKaOgLL", + "outputId": "24c51a31-b2a9-4548-f383-2c7beb301142" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "==================================\u001b[1m Ai Message \u001b[0m==================================\n", + "\n", + "George Washington was the first President of the United States.\n" + ] + } + ], + "source": [ + "from langchain_core.messages import HumanMessage\n", + "app.invoke({\"messages\": HumanMessage(\"Who was the first President of the USA?\")})[\"messages\"][-1].pretty_print()" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "jL0hyeYCOikE" + }, + "source": [ + "## Test Area of Circle and Weather in SF\n", + "\n", + "Now let's test it's ability to detect a area of circle or weather questions & route the query accordingly." + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "qu0HFN35OuRo", + "outputId": "aadbf697-ff2f-4ad3-f188-9d2f8338db37" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "User (q/Q to quit): What is the area of a circle with radius 5?\n", + "==================================\u001b[1m Ai Message \u001b[0m==================================\n", + "Tool Calls:\n", + " area_of_circle (call_cP9MoOJgYMBQ5fXYJdV0XJXC)\n", + " Call ID: call_cP9MoOJgYMBQ5fXYJdV0XJXC\n", + " Args:\n", + " r: 5.0\n", + "=================================\u001b[1m Tool Message \u001b[0m=================================\n", + "Name: area_of_circle\n", + "\n", + "78.53981633974483\n", + "==================================\u001b[1m Ai Message \u001b[0m==================================\n", + "\n", + "The area of a circle with radius 5 is 78.53981633974483.\n", + "User (q/Q to quit): what is the current weather in sf?\n", + "==================================\u001b[1m Ai Message \u001b[0m==================================\n", + "Tool Calls:\n", + " get_weather (call_OQeWUZFw855o6dibY0e5NMOn)\n", + " Call ID: call_OQeWUZFw855o6dibY0e5NMOn\n", + " Args:\n", + " location: sf\n", + "=================================\u001b[1m Tool Message \u001b[0m=================================\n", + "Name: get_weather\n", + "\n", + "It's 60 degrees and foggy.\n", + "==================================\u001b[1m Ai Message \u001b[0m==================================\n", + "\n", + "It's 60 degrees and foggy.\n", + "User (q/Q to quit): q\n", + "AI: Byebye\n" + ] + } + ], + "source": [ + "from langchain_core.messages import HumanMessage\n", + "while True:\n", + " user = input(\"User (q/Q to quit): \")\n", + " if user in {\"q\", \"Q\"}:\n", + " print(\"AI: Byebye\")\n", + " break\n", + " for output in app.stream({\"messages\": HumanMessage(user)}, stream_mode=\"updates\"):\n", + " last_message = next(iter(output.values()))['messages'][-1]\n", + " last_message.pretty_print()" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "XFrp6ckCOyYt" + }, + "source": [ + "# Conclusion\n", + "\n", + "The fireworks function calling model can route request to external tools or internal knowledge appropriately - thus helping developers build co-operative agents." + ] + } + ], + "metadata": { + "colab": { + "provenance": [] + }, + "kernelspec": { + "display_name": "Python 3", + "name": "python3" + }, + "language_info": { + "name": "python" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} \ No newline at end of file