" 'description': 'just produce a direction from the starting point to the library\\n:param start: the starting address\\n:type start: str\\n\\n\\n:return: the direction',\n",
" 'signature': '(start) -> str',\n",
" 'output': \"<class 'str'>\"}"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"direction_schema"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"direction = Route(\n",
" name=\"get_direction\",\n",
" utterances=[\n",
" \"How do I get to the closes park?\",\n",
" \"I want to go to the nearest restaurant\",\n",
" \"I want to go to the nearest restaurant from my location\",\n",
" \"I want to go to the nearest restaurant from my location to my home\",\n",
" \"I want to go to a library in city of Dallas\",\n",
" \"How do I get to the mall?\",\n",
" \"find me the best route to the closes Chines restaurant\",\n",
" \"How far is the drive from my mom's house to the nearest grocery store\",\n",
" ],\n",
" function_schema=direction_schema,\n",
")\n",
"\n",
"fuzzycase = Route(\n",
" name=\"do_fuzzy_case\",\n",
" utterances=[\n",
" \"I want to go to the nearest restaurant from my location to my home but I want to stop by the grocery store\",\n",
" \"How do I get to a different continent from my home\",\n",
" \"I want to go to the libary but I want to stop by the grocery store and then go to the nearest restaurant and come back\",\n",
" \"City of Austin to Los Angeles\",\n",
" \"Ski Slopes\",\n",
" ],\n",
" function_schema=fuzzy_schema,\n",
")\n",
"\n",
"irrelevant = Route(\n",
" name=\"do_irrelevant\",\n",
" utterances=[\n",
" \"How do I make grilled lobster\",\n",
" \"What is the best book for finding the driving directions\",\n",
" \"What is the best song\",\n",
" \"Can you tell me the most popular color in the world\",\n",
" \"Tell me a joke\", \n",
" ],\n",
" function_schema=irrelevant_schema,\n",
")\n",
"\n",
"routes = [direction, fuzzycase, irrelevant ]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"\u001b[32m2024-01-16 15:24:32 INFO semantic_router.utils.logger Initializing RouteLayer\u001b[0m\n"
"\u001b[32m2024-01-16 15:24:33 INFO semantic_router.utils.logger LLM `name='gpt-35-turbo' client=<openai.lib.azure.AzureOpenAI object at 0x000002324D651630> temperature=0.01 max_tokens=200` is chosen\u001b[0m\n",
"\u001b[32m2024-01-16 15:24:33 INFO semantic_router.utils.logger this is the llm passed to route object name='gpt-35-turbo' client=<openai.lib.azure.AzureOpenAI object at 0x000002324D651630> temperature=0.01 max_tokens=200\u001b[0m\n",
"\u001b[32m2024-01-16 15:24:33 INFO semantic_router.utils.logger Extracting function input...\u001b[0m\n",
"\u001b[32m2024-01-16 15:24:33 INFO semantic_router.utils.logger LLM output: {\n",
" \"start\": \"my home\"\n",
" }\u001b[0m\n",
"\u001b[32m2024-01-16 15:24:33 INFO semantic_router.utils.logger Function inputs: {'start': 'my home'}\u001b[0m\n",
"\u001b[32m2024-01-16 15:24:33 INFO semantic_router.utils.logger param info ['start) -> st']\u001b[0m\n",
"\u001b[32m2024-01-16 15:24:33 INFO semantic_router.utils.logger param names ['start) -> st']\u001b[0m\n",
"\u001b[31m2024-01-16 15:24:33 ERROR semantic_router.utils.logger Input validation error: list index out of range\u001b[0m\n"
"Cell \u001b[1;32mIn[8], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m out \u001b[38;5;241m=\u001b[39m \u001b[43mrl\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mhow do I get to the nearest gas station from my home?\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\n",
"File \u001b[1;32mc:\\Users\\armoshar\\OneDrive - Microsoft\\Projects\\OpenAI\\semantic-router\\semantic_router\\layer.py:203\u001b[0m, in \u001b[0;36mRouteLayer.__call__\u001b[1;34m(self, text)\u001b[0m\n\u001b[0;32m 201\u001b[0m route\u001b[38;5;241m.\u001b[39mllm \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mllm\n\u001b[0;32m 202\u001b[0m logger\u001b[38;5;241m.\u001b[39minfo(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mLLM `\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mroute\u001b[38;5;241m.\u001b[39mllm\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m` is chosen\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m--> 203\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mroute\u001b[49m\u001b[43m(\u001b[49m\u001b[43mtext\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 204\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m 205\u001b[0m \u001b[38;5;66;03m# if no route passes threshold, return empty route choice\u001b[39;00m\n\u001b[0;32m 206\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m RouteChoice()\n",
"File \u001b[1;32mc:\\Users\\armoshar\\OneDrive - Microsoft\\Projects\\OpenAI\\semantic-router\\semantic_router\\route.py:57\u001b[0m, in \u001b[0;36mRoute.__call__\u001b[1;34m(self, query)\u001b[0m\n\u001b[0;32m 52\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\n\u001b[0;32m 53\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mLLM is required for dynamic routes. Please ensure the `llm` \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m 54\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mattribute is set.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m 55\u001b[0m )\n\u001b[0;32m 56\u001b[0m \u001b[38;5;66;03m# if a function schema is provided we generate the inputs\u001b[39;00m\n\u001b[1;32m---> 57\u001b[0m extracted_inputs \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mllm\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mextract_function_inputs\u001b[49m\u001b[43m(\u001b[49m\n\u001b[0;32m 58\u001b[0m \u001b[43m \u001b[49m\u001b[43mquery\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mquery\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mfunction_schema\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mfunction_schema\u001b[49m\n\u001b[0;32m 59\u001b[0m \u001b[43m\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 60\u001b[0m logger\u001b[38;5;241m.\u001b[39minfo(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mextracted inputs \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mextracted_inputs\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m 61\u001b[0m func_call \u001b[38;5;241m=\u001b[39m extracted_inputs\n",
"out = rl(\"how do I get to the nearest gas station from my home?\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[Message(role='user', content='\\nYou are a helpful assistant designed to output JSON.\\nGiven the following function schema\\n<< {\\'name\\': \\'do_irrelevant\\', \\'description\\': \\'Handle the irrelevant questions \\\\n\\\\nreturn: the text\\', \\'signature\\': \\'() -> str\\', \\'output\\': \"<class \\'str\\'>\"} >>\\nand query\\n<< \\n \"How do I make grilled lobster\",\\n \"What is the best book for finding the driving directions\",\\n \"What is the best song\",\\n \"Can you tell me the most popular color in the world\",\\n \"Tell me a joke\", \\n >>\\nextract the parameters values from the query, in a valid JSON format.\\nExample:\\nInput:\\nquery: \"How is the weather in Hawaii right now in International units?\"\\nschema:\\n{\\n \"name\": \"get_weather\",\\n \"description\": \"Useful to get the weather in a specific location\",\\n \"signature\": \"(location: str, degree: str) -> str\",\\n \"output\": \"<class \\'str\\'>\",\\n}\\n\\nResult: {\\n \"location\": \"London\",\\n \"degree\": \"Celsius\",\\n}\\n\\nInput:\\nquery: \\n \"How do I make grilled lobster\",\\n \"What is the best book for finding the driving directions\",\\n \"What is the best song\",\\n \"Can you tell me the most popular color in the world\",\\n \"Tell me a joke\", \\n \\nschema: {\\'name\\': \\'do_irrelevant\\', \\'description\\': \\'Handle the irrelevant questions \\\\n\\\\nreturn: the text\\', \\'signature\\': \\'() -> str\\', \\'output\\': \"<class \\'str\\'>\"}\\nResult:\\n')]\n"
]
},
{
"ename": "AttributeError",
"evalue": "'list' object has no attribute 'replace'",
'description': 'just produce a direction from the starting point to the library\n:param start: the starting address\n:type start: str\n\n\n:return: the direction',
'signature': '(start) -> str',
'output': "<class 'str'>"}
%% Cell type:code id: tags:
``` python
direction=Route(
name="get_direction",
utterances=[
"How do I get to the closes park?",
"I want to go to the nearest restaurant",
"I want to go to the nearest restaurant from my location",
"I want to go to the nearest restaurant from my location to my home",
"I want to go to a library in city of Dallas",
"How do I get to the mall?",
"find me the best route to the closes Chines restaurant",
"How far is the drive from my mom's house to the nearest grocery store",
],
function_schema=direction_schema,
)
fuzzycase=Route(
name="do_fuzzy_case",
utterances=[
"I want to go to the nearest restaurant from my location to my home but I want to stop by the grocery store",
"How do I get to a different continent from my home",
"I want to go to the libary but I want to stop by the grocery store and then go to the nearest restaurant and come back",
"City of Austin to Los Angeles",
"Ski Slopes",
],
function_schema=fuzzy_schema,
)
irrelevant=Route(
name="do_irrelevant",
utterances=[
"How do I make grilled lobster",
"What is the best book for finding the driving directions",
"What is the best song",
"Can you tell me the most popular color in the world",
[32m2024-01-16 15:24:32 INFO semantic_router.utils.logger Initializing RouteLayer[0m
%% Cell type:code id: tags:
``` python
out = rl("how do I get to the nearest gas station from my home?")
```
%% Output
[32m2024-01-16 15:24:33 INFO semantic_router.utils.logger LLM `name='gpt-35-turbo' client=<openai.lib.azure.AzureOpenAI object at 0x000002324D651630> temperature=0.01 max_tokens=200` is chosen[0m
[32m2024-01-16 15:24:33 INFO semantic_router.utils.logger this is the llm passed to route object name='gpt-35-turbo' client=<openai.lib.azure.AzureOpenAI object at 0x000002324D651630> temperature=0.01 max_tokens=200[0m
[32m2024-01-16 15:24:33 INFO semantic_router.utils.logger Extracting function input...[0m
[32m2024-01-16 15:24:33 INFO semantic_router.utils.logger LLM output: {
"start": "my home"
}[0m
[32m2024-01-16 15:24:33 INFO semantic_router.utils.logger Function inputs: {'start': 'my home'}[0m
[32m2024-01-16 15:24:33 INFO semantic_router.utils.logger param info ['start) -> st'][0m
[32m2024-01-16 15:24:33 INFO semantic_router.utils.logger param names ['start) -> st'][0m
[31m2024-01-16 15:24:33 ERROR semantic_router.utils.logger Input validation error: list index out of range[0m
[Message(role='user', content='\nYou are a helpful assistant designed to output JSON.\nGiven the following function schema\n<< {\'name\': \'do_irrelevant\', \'description\': \'Handle the irrelevant questions \\n\\nreturn: the text\', \'signature\': \'() -> str\', \'output\': "<class \'str\'>"} >>\nand query\n<< \n "How do I make grilled lobster",\n "What is the best book for finding the driving directions",\n "What is the best song",\n "Can you tell me the most popular color in the world",\n "Tell me a joke", \n >>\nextract the parameters values from the query, in a valid JSON format.\nExample:\nInput:\nquery: "How is the weather in Hawaii right now in International units?"\nschema:\n{\n "name": "get_weather",\n "description": "Useful to get the weather in a specific location",\n "signature": "(location: str, degree: str) -> str",\n "output": "<class \'str\'>",\n}\n\nResult: {\n "location": "London",\n "degree": "Celsius",\n}\n\nInput:\nquery: \n "How do I make grilled lobster",\n "What is the best book for finding the driving directions",\n "What is the best song",\n "Can you tell me the most popular color in the world",\n "Tell me a joke", \n \nschema: {\'name\': \'do_irrelevant\', \'description\': \'Handle the irrelevant questions \\n\\nreturn: the text\', \'signature\': \'() -> str\', \'output\': "<class \'str\'>"}\nResult:\n')]