Skip to content
Snippets Groups Projects
Commit 418a0c8b authored by Simonas's avatar Simonas
Browse files

WIP: config generation

parent 68c70823
No related branches found
No related tags found
No related merge requests found
%% Cell type:code id: tags:
``` python
# https://platform.openai.com/docs/guides/function-calling
```
%% Cell type:code id: tags:
``` python
from semantic_router.schema import Route
import json
import openai
from semantic_router.encoders import CohereEncoder
from semantic_router.layer import RouteLayer
encoder = CohereEncoder()
def generate_config(specification: dict) -> dict:
print("Generating config...")
example_specification = (
{
"type": "function",
"function": {
"name": "get_current_weather",
"description": "Get the current weather",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"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"],
},
},
},
)
config = [
{
example_config = {
"name": "get_weather",
"utterances": [
"What is the weather like in SF?",
"What is the weather in Cyprus?",
"weather in London?",
"Tell me the weather in New York",
"what is the current weather in Paris?",
],
},
{
"name": "get_time",
"utterances": [
"What time is it in New York?",
"What time is it in London?",
"What is the time in Paris?",
],
},
{
"name": "get_news",
"utterances": [
"What is happening in the world?",
"What is the latest news?",
"What is the latest news in the US?",
],
},
]
routes = [Route(name=route["name"], utterances=route["utterances"]) for route in config]
}
route_layer = RouteLayer(encoder=encoder, routes=routes)
queries = [
"What is the weather like in Barcelona?",
"What time is it in Taiwan?",
"What is happening in the world?",
]
for query in queries:
function_name = route_layer(query)
print(function_name)
```
%% Output
get_weather
get_time
get_news
%% Cell type:code id: tags:
``` python
def get_weather(location: str):
print(f"getting weather for {location}")
def extract_function_parameters(query: str, function: Callable):
# llm(
# query=query,
# function=function,
# prompt="What are the parameters for this function?",
# )
print("Extracting function parameters..")
if category == "get_weather":
print(f"Category is `{category}`")
params = extract_function_parameters(query, get_weather)
print("Getting weather..")
# get_weather(**params)
```
%% Cell type:code id: tags:
``` python
print(generated_config)
```
%% Output
None
%% Cell type:code id: tags:
``` python
import json
example_specification = (
{
"type": "function",
"function": {
"name": "get_current_weather",
"description": "Get the current weather",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"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
if ai_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)
return route_config
except json.JSONDecodeError as json_error:
raise Exception("JSON parsing error", json_error)
except Exception as e:
raise Exception("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?']}
``` python
from semantic_router.schema import Route
from semantic_router.encoders import CohereEncoder
from semantic_router.layer import RouteLayer
def get_route_layer(config: list[dict]) -> RouteLayer:
print("Getting route layer...")
encoder = CohereEncoder()
routes = [
Route(name=route["name"], utterances=route["utterances"]) for route in config
]
return RouteLayer(encoder=encoder, routes=routes)
```
%% Cell type:code id: tags:
``` python
routes = [Route(name=route["name"], utterances=route["utterances"]) for route in [route_config]]
specification = {
"type": "function",
"function": {
"name": "get_time",
"description": "Get the current time",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state",
},
},
"required": ["location"],
},
},
}
route_layer = RouteLayer(encoder=encoder, routes=routes)
route_config = generate_config(specification)
route_layer = get_route_layer([route_config])
queries = [
"What is the weather like in Barcelona?",
"What time is it in Taiwan?",
"What is happening in the world?",
"what is the time in Kaunas?"
"what is the time in Kaunas?",
"Im bored",
"I want to play a game",
"Banana"
"Banana",
]
print("Getting function name for queries:\n")
for query in queries:
function_name = route_layer(query)
print(function_name)
print((function_name, query))
```
%% Output
None
get_time
get_time
get_time
None
None
%% Cell type:code id: tags:
``` python
```
Generating config...
AI message: {
"name": "get_time",
"utterances": [
"What is the current time in SF?",
"Tell me the time in London",
"Could you tell me the time in New York?",
"May I know the current time in Paris?",
"Can you tell me what time is it in Singapore?"
]
}
Getting route layer...
Getting function name for queries:
(None, 'What is the weather like in Barcelona?')
('get_time', 'What time is it in Taiwan?')
(None, 'What is happening in the world?')
('get_time', 'what is the time in Kaunas?')
(None, 'Im bored')
(None, 'I want to play a game')
(None, 'Banana')
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment