Skip to main content

Router.Chat.Completions

Create a Completion

Creates a model response for the given chat conversation with support for retries, fallbacks, prompts, and variables.
from orq_ai_sdk import Orq
import os

with Orq(
    api_key=os.getenv("ORQ_API_KEY", ""),
) as orq:

    res = orq.router.chat.completions.create(messages=[], model="Model 3", fallbacks=[
        {
            "model": "openai/gpt-4o-mini",
        },
    ], retry={
        "on_codes": [
            429,
            500,
            502,
            503,
            504,
        ],
    }, cache={
        "ttl": 3600,
        "type": "exact_match",
    }, load_balancer={
        "type": "weight_based",
        "models": [
            {
                "model": "openai/gpt-4o",
                "weight": 0.7,
            },
            {
                "model": "anthropic/claude-3-5-sonnet",
                "weight": 0.3,
            },
        ],
    }, timeout={
        "call_timeout": 30000,
    }, variables={
        "customer_name": "John Smith",
        "product_name": "Premium Plan",
    }, stream=False)

    with res as event_stream:
        for event in event_stream:
            # handle event
            print(event, flush=True)