Skip to main content
Function calling allows Incredible to execute custom code and tools. Here’s how to use it:

πŸš€ Basic Function Calling

Simple Example

curl -X POST "https://api.incredible.one/v1/chat-completion" \
-H "Content-Type: application/json" \
-d '{
  "model": "small-1",
  "messages": [{"role": "user", "content": "Calculate 25 + 17"}],
  "functions": [{
    "name": "add_numbers",
    "description": "Add two numbers together",
    "parameters": {
      "type": "object",
      "properties": {
        "a": {"type": "number"},
        "b": {"type": "number"}
      },
      "required": ["a", "b"]
    }
  }]
}'
Response:
{
  "result": {
    "response": [
      {
        "content": "I'll use the available function to add those two numbers for you.",
        "role": "assistant"
      },
      {
        "function_call_id": "ce9ea390-450f-4a7f-8872-2f0aa69c3467",
        "function_calls": [
          {
            "input": {"a": 25, "b": 17},
            "name": "add_numbers"
          }
        ],
        "type": "function_call"
      }
    ]
  }
}

πŸ”§ Function Schema

Required Fields

{
  "name": "function_name",
  "description": "What this function does",
  "parameters": {
    "type": "object",
    "properties": {
      "param1": {
        "type": "string|number|boolean|array|object",
        "description": "Parameter description"
      }
    },
    "required": ["param1"]
  }
}

Parameter Types

  • string: Text values
  • number: Numeric values
  • boolean: True/false values
  • array: Lists of values
  • object: Structured data

🎭 Multiple Functions

Provide multiple functions and let Incredible choose:
{
  "functions": [
    {
      "name": "get_weather",
      "description": "Get current weather for a location",
      "parameters": {
        "type": "object",
        "properties": {
          "location": {"type": "string"}
        },
        "required": ["location"]
      }
    },
    {
      "name": "calculate_distance",
      "description": "Calculate distance between two points",
      "parameters": {
        "type": "object",
        "properties": {
          "lat1": {"type": "number"},
          "lon1": {"type": "number"},
          "lat2": {"type": "number"},
          "lon2": {"type": "number"}
        },
        "required": ["lat1", "lon1", "lat2", "lon2"]
      }
    }
  ]
}

πŸ”„ Function Calling in Conversations

Include function results in subsequent messages:
{
  "messages": [
    {"role": "user", "content": "What's the weather in New York?"},
    {"role": "assistant", "content": "Let me check the weather for you."},
    {"role": "function", "name": "get_weather", "content": "{\"temperature\": 72, \"condition\": \"sunny\"}"},
    {"role": "user", "content": "What about tomorrow?"}
  ]
}

🚨 Error Handling

Common Errors

Invalid Function Schema:
{
  "error": "Invalid function schema"
}
Function Not Found:
{
  "error": "Function 'unknown_function' not found"
}

πŸ’‘ Best Practices

  1. Clear Names: Use descriptive function names
  2. Detailed Descriptions: Explain what the function does
  3. Parameter Validation: Define required vs optional clearly
  4. Test Functions: Ensure functions work independently

🎯 What’s Next?

Need Help? Check our GitHub cookbook for examples.

πŸ”„ Complete Function Calling Workflow

Function calling follows a specific workflow. Here’s the complete process:

Step 1: AI Decides to Call Function

The AI announces a function call in its response:
curl -X POST "https://api.incredible.one/v1/chat-completion" \
-H "Content-Type: application/json" \
-d '{
  "model": "small-1",
  "messages": [{"role": "user", "content": "Calculate 25 + 17"}],
  "functions": [{
    "name": "add_numbers",
    "description": "Add two numbers together",
    "parameters": {
      "type": "object",
      "properties": {
        "a": {"type": "number"},
        "b": {"type": "number"}
      },
      "required": ["a", "b"]
    }
  }]
}'
Response with Function Call:
{
  "result": {
    "thinking": "The user wants to calculate 25 + 17. I have an add_numbers function available.",
    "response": [
      {
        "role": "assistant",
        "content": "I'll calculate that for you using the add_numbers function."
      },
      {
        "type": "function_call",
        "function_call_id": "abc-123",
        "function_calls": [
          {
            "name": "add_numbers",
            "input": {"a": 25, "b": 17}
          }
        ]
      }
    ]
  }
}

Step 2: Client Executes Function

Execute the function locally with the provided parameters:
def add_numbers(a, b):
    return a + b

result = add_numbers(25, 17)  # Returns 42

Step 3: Client Sends Results

Send the function results back to continue the conversation:
curl -X POST "https://api.incredible.one/v1/chat-completion" \
-H "Content-Type: application/json" \
-d '{
  "model": "small-1",
  "messages": [
    {"role": "user", "content": "Calculate 25 + 17"},
    {"role": "assistant", "content": "I'\''ll calculate that for you using the add_numbers function."},
    {
      "type": "function_call",
      "function_call_id": "abc-123",
      "function_calls": [{"name": "add_numbers", "input": {"a": 25, "b": 17}}]
    },
    {
      "type": "function_call_result",
      "function_call_id": "abc-123",
      "function_call_results": [42]
    }
  ],
  "functions": [{
    "name": "add_numbers",
    "description": "Add two numbers together",
    "parameters": {
      "type": "object",
      "properties": {
        "a": {"type": "number"},
        "b": {"type": "number"}
      },
      "required": ["a", "b"]
    }
  }]
}'
Final Response:
{
  "result": {
    "thinking": "The user provided the result of the addition. Now I can give them the final answer.",
    "response": [
      {
        "role": "assistant",
        "content": "The sum of 25 and 17 is 42."
      }
    ]
  }
}