{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import json\n", "import requests\n", "from openai import OpenAI\n", "from inference import (base_url, inflection_api_key)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# **Function Calling with Structured API Queries**\n", "\n", "This notebook demonstrates how to implement **function calling** using new OpenAI compatible API for structured API queries. The goal is to efficiently retrieve relevant information based on specific user inputs by utilizing structured function calls. This approach enhances **accuracy**, improves **response efficiency**, and enables seamless **integration** with various API-based workflows.\n", "\n", "## **Overview**\n", "\n", "This notebook focuses on structured function execution to enhance automation and data retrieval. It provides a systematic approach to invoking API functions with well-defined parameters.\n", "\n", "## **Key Functionalities**\n", "\n", "This notebook implements the following function:\n", "\n", "- `get_weather(latitude: str, longitude: str) -> dict:` \n", " - Fetches weather data based on geographic coordinates.\n", "\n", "The structured approach ensures that API calls are **consistent, interpretable, and easily adaptable** for various use cases.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "url = base_url + \"/external/api/inference/openai/v1/\"\n", "model = \"inflection_3_with_tools\"\n", "client = OpenAI(base_url=url, api_key=inflection_api_key)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def get_weather(latitude: str, longitude: str) -> dict:\n", " \"\"\"\n", " Get the weather information for the given latitude and longitude.\n", " \"\"\"\n", " try:\n", " # Fetch the weather data in Celsius\n", " response_celsius = requests.get(\n", " f\"https://api.open-meteo.com/v1/forecast?latitude={latitude}&longitude={longitude}\"\n", " f\"¤t=temperature_2m,wind_speed_10m&hourly=temperature_2m,relative_humidity_2m,wind_speed_10m\"\n", " f\"&temperature_unit=celsius\"\n", " )\n", " data_celsius = response_celsius.json()\n", " temperature_celsius = data_celsius['current']['temperature_2m']\n", "\n", " # Fetch the weather data in Fahrenheit\n", " response_fahrenheit = requests.get(\n", " f\"https://api.open-meteo.com/v1/forecast?latitude={latitude}&longitude={longitude}\"\n", " f\"¤t=temperature_2m,wind_speed_10m&hourly=temperature_2m,relative_humidity_2m,wind_speed_10m\"\n", " f\"&temperature_unit=fahrenheit\"\n", " )\n", " data_fahrenheit = response_fahrenheit.json()\n", " temperature_fahrenheit = data_fahrenheit['current']['temperature_2m']\n", "\n", " return {\n", " 'temperature_celsius': temperature_celsius,\n", " 'temperature_fahrenheit': temperature_fahrenheit\n", " }\n", " except Exception as e:\n", " return {\"error\": str(e)}" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Honolulu, Hawaii\n", "latitude = \"21.3069\"\n", "longitude = \"-157.8583\"\n", "weather_data = get_weather(latitude, longitude)\n", "print(weather_data)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "tool_list = [\n", " {\n", " \"type\": \"function\",\n", " \"function\": {\n", " \"name\": \"get_weather\",\n", " \"description\": \"Get the weather for a given location\",\n", " \"parameters\": {\n", " \"properties\": {\n", " \"latitude\": {\n", " \"type\": \"string\", \n", " \"description\": \"Latitude of the location\"\n", " },\n", " \"longitude\": {\n", " \"type\": \"string\", \n", " \"description\": \"Longitude of the location\"\n", " }\n", " },\n", " \"required\": [\"latitude\", \"longitude\"],\n", " \"type\": \"object\",\n", " \"additionalProperties\": False,\n", " },\n", " },\n", " }\n", "]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "system_message = {\n", " \"role\": \"system\",\n", " \"content\": \"You are a helpful assistant. When calling a tool, use the tool results in your response. Use emojis to make your response more engaging.\"\n", "}\n", "\n", "# Define messages for user prompts\n", "messages = [\n", " system_message,\n", " {\n", " \"role\": \"user\",\n", " \"content\": \"What is the weather like in Honolulu, Hawaii?\"\n", " },\n", "]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# First API call to determine if function calling is needed\n", "completion = client.chat.completions.create(\n", " model=model,\n", " messages=messages,\n", " tools=tool_list,\n", " stream=False\n", ")\n", "\n", "print(f\"\\nUser Input: {messages[-1]}\")\n", "print(f\"\\nFirst Assistant Response: {completion.choices[0].message}\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "class color:\n", " BOLD = '\\033[1m'\n", " END = '\\033[0m'" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "if completion.choices[0].message.tool_calls:\n", " # Extract function details\n", " tool_call = completion.choices[0].message.tool_calls[0]\n", " print(f\"\\n{color.BOLD}Tool Call: {color.END} {tool_call}\")\n", " function_name = tool_call.function.name\n", " arguments = json.loads(tool_call.function.arguments)\n", "\n", " # Execute the function call\n", " result = eval(f\"{function_name}('{arguments['latitude']}', '{arguments['longitude']}')\")\n", " print(f\"\\n{color.BOLD}Tool Response: {color.END} {result}\")\n", "\n", "\n", " # Append the tool call result as a **string**\n", " messages.append({\n", " \"role\": \"tool\",\n", " \"content\": str(result),\n", " \"tool_call_id\": completion.choices[0].message.tool_calls[0].id\n", " })\n", " print(f\"\\n{color.BOLD}Tool Messages: {color.END} {messages}\")\n", "\n", " \n", " # Final call to generate response with function output\n", " completion_2 = client.chat.completions.create(\n", " model=model,\n", " messages=messages,\n", " tools=tool_list,\n", " stream=False,\n", " )\n", "\n", " print(f\"\\n{color.BOLD}Final Assistant Response: {color.END} {completion_2.choices[0].message.content}\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": ".venv", "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.12.8" } }, "nbformat": 4, "nbformat_minor": 2 }