import json
import os
from typing import List
from openai import OpenAI # openai==1.52.2
import pandas as pd
client = OpenAI(
api_key="UPSTAGE_API_KEY_INJECT",
base_url="https://api.upstage.ai/v1"
)
# Step 1. Setup and create structured data
# This is a hard coded dummy data to demonstrate the tool call capability
# In production, this could be your internal database
# Create data(MAU and revenue)
mau_data = [
{"Month": 1, "Service": "LLM", "MAU": 100},
{"Month": 1, "Service": "Embedding", "MAU": 50},
{"Month": 2, "Service": "LLM", "MAU": 150},
{"Month": 2, "Service": "Embedding", "MAU": 70},
{"Month": 3, "Service": "LLM", "MAU": 300},
{"Month": 3, "Service": "Embedding", "MAU": 80},
{"Month": 4, "Service": "LLM", "MAU": 350},
{"Month": 4, "Service": "Embedding", "MAU": 150},
]
mau = pd.DataFrame(mau_data)
revenue_data = [
{"Month": 1, "Service": "LLM", "Revenue": 1000},
{"Month": 1, "Service": "Embedding", "Revenue": 500},
{"Month": 2, "Service": "LLM", "Revenue": 1500},
{"Month": 2, "Service": "Embedding", "Revenue": 700},
{"Month": 3, "Service": "LLM", "Revenue": 3000},
{"Month": 3, "Service": "Embedding", "Revenue": 800},
{"Month": 4, "Service": "LLM", "Revenue": 3500},
{"Month": 4, "Service": "Embedding", "Revenue": 1500},
]
revenue = pd.DataFrame(revenue_data)
# Step 2. Define the function
# This function query_data is designed to filter data from either the MAU or Revenue tables based on the months and service specified.
# It returns a filtered DataFrame based on these criteria.
# This function will be later utilized by the LLM as part of the tool call mechanism to dynamically answer queries.
# Example question
question = "Use the query_data tool to find Revenue for service LLM in months 1, 2, and 3."
def query_data(table_name: str, month: List[int], service: str) -> pd.DataFrame:
"""
Query data from the given table based on month and service.
"""
if table_name == "MAU":
data = mau
elif table_name == "Revenue":
data = revenue
else:
raise ValueError(f"Table name {table_name} not found.")
return data[(data["Month"].isin(month)) & (data["Service"] == service)]
# Step 3. Define tools for tool calling
# This block defines a tools array that registers the query_data function as an available option for the LLM.
# It provides the model with information about the available functions and its expected parameters, enabling the model to dynamically call the functions during conversations.
tools = [
{
"type": "function",
"function": {
"name": "query_data",
"description": "Query MAU or Revenue data for specified months and service.",
"parameters": {
"type": "object",
"properties": {
"table_name": {
"type": "string",
"description": "The table name to query.",
"enum": ["MAU", "Revenue"],
},
"month": {
"type": "array",
"items": {"type": "integer"},
"description": "The list of months to query.",
},
"service": {
"type": "string",
"description": "Which service to query.",
"enum": ["LLM", "Embedding"],
},
},
"required": ["table_name", "month", "service"],
"additionalProperties": False,
},
},
}
]
# Step 4. User message and initial LLM response
# The LLM receives a user message inquiring about the revenue in the 1st quarter for LLM. Along with this, the tools list (including query_data) provided, enabling the model to decide whether a tool call is needed.
# If the model determines that the query_data function should be used, it will generate a request, which will be handled in the next step.
messages = [
{
"role": "user",
"content": question,
}
]
response = client.chat.completions.create(
model="solar-pro-4",
messages=messages,
tools=tools,
tool_choice={"type": "function", "function": {"name": "query_data"}},
parallel_tool_calls=True, # Enable parallel tool calls
)
# Step 5. Function execution and tool call handling
# The LLM’s response includes a tool call (tool_call), indicating the model has chosen to call the query_data function.
# The tool call contains the function name and arguments, which are extracted and parsed.
# The query_data function is executed using the extracted arguments.
response_message = response.choices[0].message
tool_calls = response_message.tool_calls
if not tool_calls:
raise RuntimeError("The model did not return the required tool call")
tool_call = tool_calls[0]
# Define the available functions
available_functions = {
"query_data": query_data,
}
messages.append(response_message)
# Retrieve the function name and corresponding callable function
function_name = tool_call.function.name
function_to_call = available_functions.get(function_name)
if function_to_call is None:
raise ValueError(f"Tool not allowed: {function_name}")
# Parse the function arguments from the tool call
try:
function_args = json.loads(tool_call.function.arguments)
except json.JSONDecodeError as exc:
raise ValueError("Tool arguments must be valid JSON") from exc
if not isinstance(function_args, dict):
raise ValueError("Tool arguments must be a JSON object")
unexpected = set(function_args) - {"table_name", "month", "service"}
if unexpected:
raise ValueError(f"Unexpected tool arguments: {sorted(unexpected)}")
table_name = function_args.get("table_name")
month = function_args.get("month")
service = function_args.get("service")
if table_name not in {"MAU", "Revenue"}:
raise ValueError("table_name must be MAU or Revenue")
if (
not isinstance(month, list)
or not month
or not all(type(value) is int for value in month)
):
raise ValueError("month must be a non-empty array of integers")
if service not in {"LLM", "Embedding"}:
raise ValueError("service must be LLM or Embedding")
# Execute the function with the parsed arguments
function_response = function_to_call(
table_name=table_name,
month=month,
service=service,
)
function_response = json.dumps(function_response.to_dict(orient="records"))
# Step 6. Extend the conversation with function output
# The function’s output is appended to the conversation as a message.
# The model is then prompted to continue the conversation, now informed by the function's results.
# Append the function's response to the conversation history
messages.append(
{
"tool_call_id": tool_call.id,
"role": "tool",
"name": function_name,
"content": function_response,
}
)
# Continue the conversation by sending the updated message history to the LLM
second_response = client.chat.completions.create(
model="solar-pro-4",
messages=messages,
)
print(second_response.choices[0].message.content)