" \"Could you tell me the time in New York?\",\n",
" \"May I know the current time in Paris?\",\n",
" \"Can you tell me what time is it in Singapore?\"\n",
" ]\n",
"}\n",
"Getting route layer...\n",
"Getting function name for queries:\n",
"\n",
"(None, 'What is the weather like in Barcelona?')\n",
"('get_time', 'What time is it in Taiwan?')\n",
"(None, 'What is happening in the world?')\n",
"('get_time', 'what is the time in Kaunas?')\n",
"(None, 'Im bored')\n",
"(None, 'I want to play a game')\n",
"(None, 'Banana')\n"
]
}
],
"source": [
"print(generated_config)"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Route config: {'name': 'get_time', 'utterances': ['What is the current time in San Francisco?', 'What time is it in New York?', 'Current time in London?']}\n"
]
}
],
"source": [
"import json\n",
"\n",
"example_specification = (\n",
" {\n",
" \"type\": \"function\",\n",
" \"function\": {\n",
" \"name\": \"get_current_weather\",\n",
" \"description\": \"Get the current weather\",\n",
" \"parameters\": {\n",
" \"type\": \"object\",\n",
" \"properties\": {\n",
" \"location\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"The city and state, e.g. San Francisco, CA\",\n",
" },\n",
" \"format\": {\n",
" \"type\": \"string\",\n",
" \"enum\": [\"celsius\", \"fahrenheit\"],\n",
" \"description\": \"The temperature unit to use. Infer this from the users location.\",\n",
" },\n",
"specification = {\n",
" \"type\": \"function\",\n",
" \"function\": {\n",
" \"name\": \"get_time\",\n",
" \"description\": \"Get the current time\",\n",
" \"parameters\": {\n",
" \"type\": \"object\",\n",
" \"properties\": {\n",
" \"location\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"The city and state\",\n",
" },\n",
" \"required\": [\"location\", \"format\"],\n",
" },\n",
" \"required\": [\"location\"],\n",
" },\n",
" },\n",
")\n",
"\n",
"example_config = {\n",
" \"name\": \"get_weather\",\n",
" \"utterances\": [\n",
" \"What is the weather like in SF?\",\n",
" \"What is the weather in Cyprus?\",\n",
" \"weather in London?\",\n",
" ],\n",
"}\n",
"\n",
"specification = (\n",
" {\n",
" \"type\": \"function\",\n",
" \"function\": {\n",
" \"name\": \"get_time\",\n",
" \"description\": \"Get the current time\",\n",
" \"parameters\": {\n",
" \"type\": \"object\",\n",
" \"properties\": {\n",
" \"location\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"The city and state, e.g. San Francisco, CA\",\n",
" },\n",
" },\n",
" \"required\": [\"location\"],\n",
" },\n",
" },\n",
" },\n",
")\n",
"\n",
"prompt = f\"\"\"\n",
" Given the following specification, generate a config in JSON format\n",
"description":"The city and state, e.g. San Francisco, CA",
},
"format":{
"type":"string",
"enum":["celsius","fahrenheit"],
"description":"The temperature unit to use. Infer this from the users location.",
},
},
"required":["location","format"],
},
},
},
)
example_config={
"name":"get_weather",
"utterances":[
"What is the weather like in SF?",
"What is the weather in Cyprus?",
"weather in London?",
],
}
specification=(
{
"type":"function",
"function":{
"name":"get_time",
"description":"Get the current time",
"parameters":{
"type":"object",
"properties":{
"location":{
"type":"string",
"description":"The city and state, e.g. San Francisco, CA",
},
},
"required":["location"],
},
},
},
)
prompt=f"""
Given the following specification, generate a config in JSON format
prompt=f"""
Given the following specification, generate a config in a valid JSON format,
Example:
SPECIFICATION:
{example_specification}
CONFIG:
{example_config}
GIVEN SPECIFICATION:
{specification}
GENERATED CONFIG:
"""
"""
response=openai.chat.completions.create(
model="gpt-4",
messages=[
{"role":"system","content":f"{prompt}"},
],
)
ai_message=response.choices[0].message.content
ifai_message:
route_config=json.loads(ai_message)
print(f"Route config: {route_config}")
try:
response=openai.chat.completions.create(
model="gpt-4",
messages=[
{"role":"system","content":f"{prompt}"},
],
)
ai_message=response.choices[0].message.content
print("AI message:",ai_message)
route_config=json.loads(ai_message)
returnroute_config
exceptjson.JSONDecodeErrorasjson_error:
raiseException("JSON parsing error",json_error)
exceptExceptionase:
raiseException("Error generating config from Openai",e)
```
%% Output
%% Cell type:code id: tags:
Route config: {'name': 'get_time', 'utterances': ['What is the current time in San Francisco?', 'What time is it in New York?', 'Current time in London?']}