Skip to content
Snippets Groups Projects
Commit 68c70823 authored by Simonas's avatar Simonas
Browse files

WIP

parent 6a0001d5
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
from semantic_router.encoders import CohereEncoder
from semantic_router.layer import RouteLayer
encoder = CohereEncoder()
config = [
{
"name": "get_weather",
"utterances": [
"What is the weather like in SF?",
"What is the weather in Cyprus?",
"weather in London?",
],
},
{
"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
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}")
```
%% Output
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?']}
%% Cell type:code id: tags:
``` python
routes = [Route(name=route["name"], utterances=route["utterances"]) for route in [route_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?",
"what is the time in Kaunas?"
"Im bored",
"I want to play a game",
"Banana"
]
for query in queries:
function_name = route_layer(query)
print(function_name)
```
%% Output
None
get_time
get_time
get_time
None
None
%% Cell type:code id: tags:
``` python
```
......@@ -36,7 +36,7 @@ requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
[tool.ruff.per-file-ignores]
"*.ipynb" = ["E402"]
"*.ipynb" = ["ALL"]
[tool.mypy]
ignore_missing_imports = true
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment