DoublewordDoubleword

Tool Calling and Structured Outputs

Tool Calling

Tool calling (also known as function calling) allows models to intelligently select and use external tools based on user input. Our API is fully compataible with the OpenAI API, so you can build agents that access APIs, retrieve real-time data, or perform actions—all through OpenAI-compatible tool specifications.

Example

import json
from openai import OpenAI

client = OpenAI(
    base_url="https://api.doubleword.ai/v1",
    api_key="{{apiKey}}"
)

# Define the weather tool
tools = [{
    "type": "function",
    "function": {
        "name": "get_current_weather",
        "description": "Get the current weather in a given location",
        "parameters": {
            "type": "object",
            "properties": {
                "location": {
                    "type": "string",
                    "description": "The city and state, e.g. San Francisco, CA"
                },
                "unit": {
                    "type": "string",
                    "enum": ["celsius", "fahrenheit"]
                }
            },
            "required": ["location"]
        }
    }
}]

# Example batch request 
{
        "custom_id": f"weather",
        "method": "POST",
        "url": "/v1/chat/completions",
        "body": {
            "model": "{{selectedModel.id}}",
            "messages": [
                {
                    "role": "user",
                    "content": f"What's the weather like in {city}?"
                }
            ],
            "tools": tools, ## pass in tools
            "tool_choice": "auto"
        }
    })

Structured Outputs

Structured Outputs is a features that ensures that the model will always genearte responses that adhere to your supplied JSON Schema, so you don't need to worry about the model omitting a required key, or hallucinating an invalid enum value. Doubleword is compatible with the OpenAI schema to enforce Structured Outputs.

Example

import json
from openai import OpenAI

client = OpenAI(
    base_url="https://api.doubleword.ai/v1",
    api_key="{{apiKey}}"
)

# Define the structured output schema
contact_schema = {
    "type": "json_schema",
    "json_schema": {
        "name": "contact_extraction",
        "strict": True,
        "schema": {
            "type": "object",
            "properties": {
                "name": {
                    "type": "string",
                    "description": "The person's full name"
                },
                "email": {
                    "type": "string",
                    "description": "Email address"
                },
                "phone": {
                    "type": ["string", "null"],
                    "description": "Phone number if available"
                },
            },
            "required": ["name", "email"],
            "additionalProperties": False
        }
    }
}

# Example request
{
        "custom_id": f"extract",
        "method": "POST",
        "url": "/v1/chat/completions",
        "body": {
            "model": "{{selectedModel.id}}",
            "messages": [
                {
                    "role": "system",
                    "content": "Extract contact information from the provided text."
                },
                {
                    "role": "user",
                    "content": text
                }
            ],
            "response_format": contact_schema ## pass in structured output
        }
    })