{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "### Set up functions and routes" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from datetime import datetime\n", "from zoneinfo import ZoneInfo\n", "\n", "\n", "def get_time(timezone: str) -> str:\n", " \"\"\"Finds the current time in a specific timezone.\n", "\n", " :param timezone: The timezone to find the current time in, should\n", " be a valid timezone from the IANA Time Zone Database like\n", " \"America/New_York\" or \"Europe/London\".\n", " :type timezone: str\n", " :return: The current time in the specified timezone.\"\"\"\n", " now = datetime.now(ZoneInfo(timezone))\n", " print(f\"Invoked `get_time` function with timezone: `{timezone}`\")\n", " return now.strftime(\"%H:%M\")\n", "\n", "\n", "def get_news(category: str, country: str) -> str:\n", " \"\"\"Useful to get the news in a specific country\"\"\"\n", " print(\n", " f\"Invoked: `get_news` function with category: `{category}` \"\n", " f\"and country: `{country}`\"\n", " )\n", " return \"Results from dummy news API\"" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Invoked `get_time` function with timezone: `America/New_York`\n" ] }, { "data": { "text/plain": [ "'12:59'" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "get_time(\"America/New_York\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Define the LLM" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "c:\\Users\\Siraj\\Documents\\Personal\\Work\\Aurelio\\Virtual Environments\\semantic_router_3\\Lib\\site-packages\\tqdm\\auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n", " from .autonotebook import tqdm as notebook_tqdm\n" ] } ], "source": [ "from semantic_router.llms import OpenAILLM\n", "\n", "llm = OpenAILLM()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now generate a dynamic routing config for each function" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "\u001b[32m2024-05-07 20:59:12 INFO semantic_router.utils.logger Generating dynamic route...\u001b[0m\n", "\u001b[32m2024-05-07 20:59:15 INFO semantic_router.utils.logger Generated route config:\n", "{\n", " \"name\": \"get_time\",\n", " \"utterances\": [\n", " \"What time is it in New York?\",\n", " \"Can you tell me the current time in London?\",\n", " \"Get the time in Tokyo.\",\n", " \"What's the time in Sydney?\",\n", " \"Tell me the current time in Los Angeles.\"\n", " ]\n", " }\u001b[0m\n", "\u001b[32m2024-05-07 20:59:15 INFO semantic_router.utils.logger Generating dynamic route...\u001b[0m\n", "\u001b[32m2024-05-07 20:59:17 INFO semantic_router.utils.logger Generated route config:\n", "{\n", " \"name\": \"get_news\",\n", " \"utterances\": [\n", " \"What's the latest news in the United States?\",\n", " \"Tell me about the top headlines in Canada\",\n", " \"Get me the news for technology in Japan\",\n", " \"Show me the news for sports in Australia\",\n", " \"Any updates on politics in the United Kingdom?\"\n", " ]\n", " }\u001b[0m\n" ] } ], "source": [ "from semantic_router import Route\n", "\n", "functions = [get_time, get_news]\n", "\n", "routes = []\n", "\n", "for function in functions:\n", " route = Route.from_dynamic_route(llm=llm, entities=function)\n", " routes.append(route)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "# You can manually add or remove routes\n", "\n", "get_weather_route = Route(\n", " name=\"get_weather\",\n", " utterances=[\n", " \"what is the weather in SF\",\n", " \"what is the current temperature in London?\",\n", " \"tomorrow's weather in Paris?\",\n", " ],\n", " function_schemas=None,\n", ")\n", "routes.append(get_weather_route)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Add routes to the layer config" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "\u001b[32m2024-05-07 20:59:18 INFO semantic_router.utils.logger Using default openai encoder: text-embedding-ada-002\u001b[0m\n" ] }, { "data": { "text/plain": [ "{'encoder_type': 'openai',\n", " 'encoder_name': 'text-embedding-ada-002',\n", " 'routes': [{'name': 'get_time',\n", " 'utterances': ['What time is it in New York?',\n", " 'Can you tell me the current time in London?',\n", " 'Get the time in Tokyo.',\n", " \"What's the time in Sydney?\",\n", " 'Tell me the current time in Los Angeles.'],\n", " 'description': None,\n", " 'function_schema': {'name': 'get_time',\n", " 'description': 'Finds the current time in a specific timezone.\\n\\n:param timezone: The timezone to find the current time in, should\\n be a valid timezone from the IANA Time Zone Database like\\n \"America/New_York\" or \"Europe/London\".\\n:type timezone: str\\n:return: The current time in the specified timezone.',\n", " 'signature': '(timezone: str) -> str',\n", " 'output': \"\"},\n", " 'llm': {'module': 'semantic_router.llms.openai',\n", " 'class': 'OpenAILLM',\n", " 'model': 'gpt-3.5-turbo'},\n", " 'score_threshold': None},\n", " {'name': 'get_news',\n", " 'utterances': [\"What's the latest news in the United States?\",\n", " 'Tell me about the top headlines in Canada',\n", " 'Get me the news for technology in Japan',\n", " 'Show me the news for sports in Australia',\n", " 'Any updates on politics in the United Kingdom?'],\n", " 'description': None,\n", " 'function_schema': {'name': 'get_news',\n", " 'description': 'Useful to get the news in a specific country',\n", " 'signature': '(category: str, country: str) -> str',\n", " 'output': \"\"},\n", " 'llm': {'module': 'semantic_router.llms.openai',\n", " 'class': 'OpenAILLM',\n", " 'model': 'gpt-3.5-turbo'},\n", " 'score_threshold': None},\n", " {'name': 'get_weather',\n", " 'utterances': ['what is the weather in SF',\n", " 'what is the current temperature in London?',\n", " \"tomorrow's weather in Paris?\"],\n", " 'description': None,\n", " 'function_schema': None,\n", " 'llm': None,\n", " 'score_threshold': None}]}" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from semantic_router.layer import LayerConfig\n", "\n", "layer_config = LayerConfig(routes=routes)\n", "layer_config.to_dict()" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Route(name='get_time', utterances=['What time is it in New York?', 'Can you tell me the current time in London?', 'Get the time in Tokyo.', \"What's the time in Sydney?\", 'Tell me the current time in Los Angeles.'], description=None, function_schema={'name': 'get_time', 'description': 'Finds the current time in a specific timezone.\\n\\n:param timezone: The timezone to find the current time in, should\\n be a valid timezone from the IANA Time Zone Database like\\n \"America/New_York\" or \"Europe/London\".\\n:type timezone: str\\n:return: The current time in the specified timezone.', 'signature': '(timezone: str) -> str', 'output': \"\"}, llm=OpenAILLM(name='gpt-3.5-turbo', client=, temperature=0.01, max_tokens=200), score_threshold=None)" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Get a route by name\n", "layer_config.get(\"get_time\")" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "\u001b[32m2024-05-07 20:59:18 INFO semantic_router.utils.logger Removed route `get_weather`\u001b[0m\n" ] }, { "data": { "text/plain": [ "{'encoder_type': 'openai',\n", " 'encoder_name': 'text-embedding-ada-002',\n", " 'routes': [{'name': 'get_time',\n", " 'utterances': ['What time is it in New York?',\n", " 'Can you tell me the current time in London?',\n", " 'Get the time in Tokyo.',\n", " \"What's the time in Sydney?\",\n", " 'Tell me the current time in Los Angeles.'],\n", " 'description': None,\n", " 'function_schema': {'name': 'get_time',\n", " 'description': 'Finds the current time in a specific timezone.\\n\\n:param timezone: The timezone to find the current time in, should\\n be a valid timezone from the IANA Time Zone Database like\\n \"America/New_York\" or \"Europe/London\".\\n:type timezone: str\\n:return: The current time in the specified timezone.',\n", " 'signature': '(timezone: str) -> str',\n", " 'output': \"\"},\n", " 'llm': {'module': 'semantic_router.llms.openai',\n", " 'class': 'OpenAILLM',\n", " 'model': 'gpt-3.5-turbo'},\n", " 'score_threshold': None},\n", " {'name': 'get_news',\n", " 'utterances': [\"What's the latest news in the United States?\",\n", " 'Tell me about the top headlines in Canada',\n", " 'Get me the news for technology in Japan',\n", " 'Show me the news for sports in Australia',\n", " 'Any updates on politics in the United Kingdom?'],\n", " 'description': None,\n", " 'function_schema': {'name': 'get_news',\n", " 'description': 'Useful to get the news in a specific country',\n", " 'signature': '(category: str, country: str) -> str',\n", " 'output': \"\"},\n", " 'llm': {'module': 'semantic_router.llms.openai',\n", " 'class': 'OpenAILLM',\n", " 'model': 'gpt-3.5-turbo'},\n", " 'score_threshold': None}]}" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Remove a route by name\n", "layer_config.remove(\"get_weather\")\n", "layer_config.to_dict()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Save config to a file (.json or .yaml)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "\u001b[32m2024-05-07 20:59:18 INFO semantic_router.utils.logger Saving route config to output/layer_config.json\u001b[0m\n" ] } ], "source": [ "layer_config.to_file(\"output/layer_config.json\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Define routing layer" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Load config from local file" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "\u001b[32m2024-05-07 20:59:18 INFO semantic_router.utils.logger Loading route config from output/layer_config.json\u001b[0m\n" ] } ], "source": [ "from semantic_router.layer import LayerConfig\n", "\n", "layer_config = LayerConfig.from_file(\"output/layer_config.json\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Initialize routing layer" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "\u001b[32m2024-05-07 20:59:18 INFO semantic_router.utils.logger local\u001b[0m\n" ] } ], "source": [ "import os\n", "from getpass import getpass\n", "from semantic_router import RouteLayer\n", "\n", "# https://dashboard.cohere.com/\n", "os.environ[\"COHERE_API_KEY\"] = os.getenv(\"COHERE_API_KEY\") or getpass(\n", " \"Enter Cohere API Key: \"\n", ")\n", "\n", "layer = RouteLayer.from_config(config=layer_config)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Do a function call with functions as tool" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "\u001b[32m2024-05-07 20:59:19 INFO semantic_router.utils.logger Extracting function input...\u001b[0m\n", "\u001b[32m2024-05-07 20:59:20 INFO semantic_router.utils.logger LLM output: {\n", "\t\"timezone\": \"Europe/Stockholm\"\n", "}\u001b[0m\n", "\u001b[32m2024-05-07 20:59:20 INFO semantic_router.utils.logger Function inputs: {'timezone': 'Europe/Stockholm'}\u001b[0m\n" ] }, { "data": { "text/plain": [ "RouteChoice(name='get_time', function_call={'timezone': 'Europe/Stockholm'}, similarity_score=None)" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "layer(\"What is the time in Stockholm?\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Define function execution method" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Query: What is the time in Stockholm?\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "\u001b[32m2024-05-07 21:13:23 INFO semantic_router.utils.logger Extracting function input...\u001b[0m\n", "\u001b[32m2024-05-07 21:13:24 INFO semantic_router.utils.logger LLM output: {\n", "\t\"timezone\": \"Europe/Stockholm\"\n", "}\u001b[0m\n", "\u001b[32m2024-05-07 21:13:24 INFO semantic_router.utils.logger Function inputs: {'timezone': 'Europe/Stockholm'}\u001b[0m\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Invoked `get_time` function with timezone: `Europe/Stockholm`\n", "19:13\n", "Query: What are the tech news in the US?\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "\u001b[32m2024-05-07 21:13:24 INFO semantic_router.utils.logger Extracting function input...\u001b[0m\n", "\u001b[32m2024-05-07 21:13:25 INFO semantic_router.utils.logger LLM output: {\n", "\t\"category\": \"tech\",\n", "\t\"country\": \"US\"\n", "}\u001b[0m\n", "\u001b[32m2024-05-07 21:13:25 INFO semantic_router.utils.logger Function inputs: {'category': 'tech', 'country': 'US'}\u001b[0m\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Invoked: `get_news` function with category: `tech` and country: `US`\n", "Results from dummy news API\n", "Query: The capital of France?\n", "##################################################\n", "query\n", "The capital of France?\n", "##################################################\n", "Paris\n" ] } ], "source": [ "from semantic_router.schema import RouteChoice\n", "from semantic_router.schema import Message\n", "\n", "llm = OpenAILLM()\n", "\n", "\n", "def route_and_execute(query, functions, layer):\n", " route_choice: RouteChoice = layer(query)\n", "\n", " for function in functions:\n", " if function.__name__ == route_choice.name:\n", " if route_choice.function_call:\n", " return function(**route_choice.function_call)\n", "\n", " # If no function is found, use the LLM for general queries\n", " msgs = [Message(role=\"user\", content=query)]\n", " return llm(msgs)\n", "\n", "\n", "queries = [\n", " \"What is the time in Stockholm?\",\n", " \"What are the tech news in the US?\",\n", " \"The capital of France?\",\n", "]\n", "\n", "for query in queries:\n", " print(f\"Query: {query}\")\n", " print(route_and_execute(query, functions, layer))" ] }, { "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.11.4" } }, "nbformat": 4, "nbformat_minor": 2 }