Guides

Tool Calling & Functions

Tool calling enables Cortiqa models like openai/gpt-oss-120b to connect to external systems. The model intelligently decides when to invoke a function, extracts typed arguments, and incorporates the output into its final response.


Overview

You supply a list of function definitions to the API. When a user prompt requires external data (like weather, database records, or math computations), the model returns structured arguments in tool_calls instead of raw text.

High-Speed Function Calling
Cortiqa LPU execution ensures minimal latency when emitting structured JSON arguments for autonomous agent workflows.

Defining Function Tools

Define tools using standard JSON Schema objects:

tools.py
tools = [
    {
        "type": "function",
        "function": {
            "name": "get_weather",
            "description": "Get the current temperature and conditions for a given city.",
            "parameters": {
                "type": "object",
                "properties": {
                    "city": {"type": "string", "description": "Name of the city, e.g. Bengaluru, San Francisco"},
                    "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]}
                },
                "required": ["city"]
            }
        }
    }
]

Python Example

tool_demo.py
1from cortiqa import Cortiqa
2
3client = Cortiqa()
4
5response = client.chat.completions.create(
6 model="openai/gpt-oss-120b",
7 messages=[{"role": "user", "content": "What is the weather in Delhi right now?"}],
8 tools=tools,
9)
10
11message = response.choices[0].message
12
13if message.tool_calls:
14 for tool_call in message.tool_calls:
15 print("Model requested tool:", tool_call.function.name)
16 print("Arguments:", tool_call.function.arguments)

TypeScript Example

tool_demo.ts
1import Cortiqa, { Tool } from "@cortiqa/sdk";
2
3const client = new Cortiqa();
4
5const tools: Tool[] = [
6 {
7 type: "function",
8 function: {
9 name: "get_weather",
10 description: "Get temperature for a city",
11 parameters: {
12 type: "object",
13 properties: {
14 city: { type: "string" },
15 },
16 required: ["city"],
17 },
18 },
19 },
20];
21
22async function run() {
23 const response = await client.messages.create({
24 model: "openai/gpt-oss-120b",
25 messages: [{ role: "user", content: "What is the weather in Mumbai?" }],
26 tools,
27 });
28
29 const toolCalls = response.choices?.[0]?.message?.tool_calls;
30 if (toolCalls && toolCalls.length > 0) {
31 console.log("Tool requested:", toolCalls[0].function.name);
32 console.log("Arguments:", toolCalls[0].function.arguments);
33 }
34}
35
36run();

Returning Tool Results

After executing the function on your server, send the result back with role: "tool" to get the final answer:

followup.py
messages = [
    {"role": "user", "content": "What is the weather in Delhi right now?"},
    message, # the assistant message with tool_calls
    {
        "role": "tool",
        "tool_call_id": message.tool_calls[0].id,
        "content": '{"temperature": "28°C", "condition": "Partly Cloudy"}'
    }
]

final_response = client.chat.completions.create(
    model="openai/gpt-oss-120b",
    messages=messages
)
print(final_response.choices[0].message.content)
Was this page helpful?