🌓
Examples
1. Chaining Function Calls
from tiny_fnc_engine import FunctionCallingEngine
import random
def get_random_city():
cities = ["New York", "London", "Tokyo", "Paris", "Sydney"]
return random.choice(cities)
def get_weather_forecast(city):
weather_conditions = ["Sunny", "Rainy", "Cloudy", "Snowy"]
temperature = random.randint(0, 35)
return {
"city": city,
"condition": random.choice(weather_conditions),
"temperature": temperature
}
engine = FunctionCallingEngine()
engine.add_functions([get_random_city, get_weather_forecast])
function_calls = [
{
"name": "get_random_city",
"parameters": {},
"returns": [{"name": "random_city", "type": "str"}]
},
{
"name": "get_weather_forecast",
"parameters": {"city": "random_city"},
"returns": [{"name": "forecast", "type": "dict"}]
}
]
results = engine.parse_and_call_functions(function_calls)
print(f"Random city: {results[0]}")
print(f"Weather forecast: {results[1]}")
2. Non-chained Function Calls
def add(a, b):
return a + b
def multiply(a, b):
return a * b
engine = FunctionCallingEngine()
engine.add_functions([add, multiply])
function_calls = [
{
"name": "add",
"parameters": {"a": 5, "b": 3}
},
{
"name": "multiply",
"parameters": {"a": 4, "b": 7}
}
]
results = engine.parse_and_call_functions(function_calls)
print(f"Sum: {results[0]}, Product: {results[1]}")
3. Using JSON String Input
import json
from tiny_fnc_engine import FunctionCallingEngine
def greet(name):
return f"Hello, {name}!"
def calculate_sum(a, b):
return a + b
engine = FunctionCallingEngine()
engine.add_functions([greet, calculate_sum])
json_string = json.dumps([
{
"name": "greet",
"parameters": {"name": "Alice"},
"returns": [{"name": "greeting", "type": "str"}]
},
{
"name": "calculate_sum",
"parameters": {"a": 5, "b": 3},
"returns": [{"name": "sum", "type": "int"}]
}
])
results = engine.parse_and_call_functions(json_string, verbose=True)
print(f"Greeting: {results[0]}")
print(f"Sum: {results[1]}")
4. Using OpenAI Tool Calls Format
from tiny_fnc_engine import FunctionCallingEngine
def get_weather_forecast(location: str) -> str:
# This is a mock function. In a real scenario, you'd call an actual weather API.
return f"The weather in {location} is sunny with a high of 75°F."
engine = FunctionCallingEngine()
engine.add_functions([get_weather_forecast])
tool_calls = [
{
"id": "call_123",
"function": {
"arguments": '{"location": "San Francisco, CA"}',
"name": "get_weather_forecast"
},
"type": "function"
}
]
results = engine.parse_and_call_functions(tool_calls)
print(f"Weather forecast: {results[0]}")