Mume AIMume AI
DocsModelsPricingChat
OverviewChat CompletionsResponses APIStreamingFunction CallingWeb SearchMCP ServersModelsError HandlingAuthentication

Function Calling / Tools

Extend AI capabilities by letting models call your custom functions. This powerful feature enables dynamic interactions with external systems, databases, APIs, and more.

How It Works

  1. Define your tools — Describe the functions the model can call using JSON Schema
  2. Make the initial request — The model decides if it needs to call a function
  3. Execute the function — Run your code with the provided arguments
  4. Return results — Send the function output back to get the final response

Complete Example

Python

Python
import openai import json client = openai.OpenAI( api_key="your-api-key", base_url="https://mume.ai/api/v1", ) # 1. Define callable tools tools = [ { "type": "function", "name": "get_horoscope", "description": "Get today's horoscope for an astrological sign.", "parameters": { "type": "object", "properties": { "sign": { "type": "string", "description": "An astrological sign like Taurus or Aquarius", }, }, "required": ["sign"], }, }, ] def get_horoscope(sign): return f"{sign}: Next Tuesday you will befriend a baby otter." # 2. Initial request with tools input_list = [ { "type": "message", "role": "user", "content": "What is my horoscope? I am a Capricorn.", } ] response = client.responses.create( model="openai/gpt-4.1-mini", tools=tools, input=input_list, stream=True, ) final_tool_calls = {} for chunk in response: if chunk.type == "response.output_text.delta": print(chunk.delta, end="", flush=True) if chunk.type == "response.completed": final_tool_calls = chunk.response.output # 3. Process function calls for item in final_tool_calls: if item.type == "function_call": call_id = getattr(item, "id", None) or getattr(item, "call_id", None) input_list.append({ "type": "function_call", "call_id": call_id, "name": item.name, "arguments": item.arguments, }) if item.name == "get_horoscope": args = json.loads(item.arguments) result = get_horoscope(args.get("sign")) # 4. Return function results input_list.append({ "type": "function_call_output", "call_id": call_id, "output": json.dumps({"horoscope": result}), }) # 5. Get final response with function results response = client.responses.create( model="openai/gpt-4.1-mini", input=input_list, stream=True, ) for chunk in response: if chunk.type == "response.output_text.delta": print(chunk.delta, end="", flush=True)

JavaScript

JavaScript
import OpenAI from "openai"; const client = new OpenAI({ apiKey: "your-api-key", baseURL: "https://mume.ai/api/v1", }); // 1. Define callable tools const tools = [ { type: "function", name: "get_horoscope", description: "Get today's horoscope for an astrological sign.", parameters: { type: "object", properties: { sign: { type: "string", description: "An astrological sign like Taurus or Aquarius", }, }, required: ["sign"], }, }, ]; function getHoroscope(sign) { return `${sign}: Next Tuesday you will befriend a baby otter.`; } const inputList = [ { type: "message", role: "user", content: "What is my horoscope? I am a Capricorn.", }, ]; // 2. Initial request with tools const stream = await client.responses.create({ model: "openai/gpt-4.1-mini", tools, input: inputList, stream: true, }); let finalToolCalls = []; for await (const chunk of stream) { if (chunk.type === "response.output_text.delta") { process.stdout.write(chunk.delta); } if (chunk.type === "response.completed") { finalToolCalls = chunk.response.output; } } // 3. Process function calls for (const item of finalToolCalls) { if (item.type === "function_call") { const callId = item.id || item.callId || item.call_id; inputList.push({ type: "function_call", id: callId, callId: callId, name: item.name, arguments: item.arguments, }); if (item.name === "get_horoscope") { const args = JSON.parse(item.arguments); const result = getHoroscope(args.sign); // 4. Return function results inputList.push({ type: "function_call_output", callId: callId, id: callId, output: JSON.stringify({ horoscope: result }), }); } } } // 5. Get final response with function results const finalStream = await client.responses.create({ model: "openai/gpt-4.1-mini", input: inputList, stream: true, }); for await (const chunk of finalStream) { if (chunk.type === "response.output_text.delta") { process.stdout.write(chunk.delta); } }

cURL

Step 1: Initial request with tools defined

Bash
curl https://mume.ai/api/v1/responses \ -H "Content-Type: application/json" \ -H "Authorization: Bearer $MUME_API_KEY" \ -d '{ "model": "openai/gpt-4.1-mini", "tools": [ { "type": "function", "name": "get_horoscope", "description": "Get today'\''s horoscope for an astrological sign.", "parameters": { "type": "object", "properties": { "sign": { "type": "string", "description": "An astrological sign like Taurus or Aquarius" } }, "required": ["sign"] } } ], "input": [ { "type": "message", "role": "user", "content": "What is my horoscope? I am a Capricorn." } ] }'

Step 2: Send function results back

Bash
curl https://mume.ai/api/v1/responses \ -H "Content-Type: application/json" \ -H "Authorization: Bearer $MUME_API_KEY" \ -d '{ "model": "openai/gpt-4.1-mini", "input": [ { "type": "message", "role": "user", "content": "What is my horoscope? I am a Capricorn." }, { "type": "function_call", "id": "call_abc123", "callId": "call_abc123", "name": "get_horoscope", "arguments": "{\"sign\": \"Capricorn\"}" }, { "type": "function_call_output", "callId": "call_abc123", "id": "call_abc123", "output": "{\"horoscope\": \"Capricorn: Next Tuesday you will befriend a baby otter.\"}" } ] }'

Tool Schema

Tools are defined using JSON Schema. Each tool must have a type, name, description, and parameters object.

JSON
{ "type": "function", "name": "your_function_name", "description": "What the function does", "parameters": { "type": "object", "properties": { "param1": { "type": "string", "description": "Description of param1" } }, "required": ["param1"] } }

← StreamingNext: Web Search →