> ## Documentation Index
> Fetch the complete documentation index at: https://docs.orq.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Router.Completions SDK Reference

> Send requests to legacy completion models through the Orq.ai AI Gateway using the create completion method in the Node.js and Python SDKs.

## Router.Completions

### Create a Completion

For sending requests to legacy completion models

<CodeGroup>
  ```python Python theme={"theme":{"light":"github-light","dark":"github-dark"}}
  from orq_ai_sdk import Orq
  import os

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

      res = orq.router.completions.create(model="openai/gpt-3.5-turbo-instruct", prompt="<value>", echo=False, frequency_penalty=0, max_tokens=16, presence_penalty=0, temperature=1, top_p=1, n=1, retry={
          "on_codes": [
              429,
              500,
              502,
              503,
              504,
          ],
      }, cache={
          "ttl": 3600,
          "type": "exact_match",
      }, load_balancer={
          "type": "weight_based",
          "models": [
              {
                  "model": "openai/gpt-6-astra",
                  "weight": 0.7,
              },
          ],
      }, timeout={
          "call_timeout": 30000,
      }, stream=False)

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

  ```

  ```typescript Node.js theme={"theme":{"light":"github-light","dark":"github-dark"}}
  import { Orq } from "@orq-ai/node";

  const orq = new Orq({
    apiKey: process.env["ORQ_API_KEY"] ?? "",
  });

  async function run() {
    const result = await orq.router.completions.create({
      model: "openai/gpt-3.5-turbo-instruct",
      prompt: "<value>",
      retry: {
        onCodes: [
          429,
          500,
          502,
          503,
          504,
        ],
      },
      cache: {
        ttl: 3600,
        type: "exact_match",
      },
      loadBalancer: {
        type: "weight_based",
        models: [
          {
            model: "openai/gpt-6-astra",
            weight: 0.7,
          },
        ],
      },
      timeout: {
        callTimeout: 30000,
      },
    });

    console.log(result);
  }

  run();
  ```
</CodeGroup>

<Expandable title="Parameters">
  <CodeGroup>
    ```python Python theme={"theme":{"light":"github-light","dark":"github-dark"}}
    {
        "model": str,  # required
        "prompt": str,  # required
        "echo": Optional[bool],
        "frequency_penalty": Optional[float],
        "max_tokens": Optional[int],
        "presence_penalty": Optional[float],
        "seed": Optional[int],
        "stop": Union[str, List[str]],  # optional
        "temperature": Optional[float],
        "top_p": Optional[float],
        "n": Optional[int],
        "user": Optional[str],
        "name": Optional[str],
        "fallbacks": [{  # optional
            "model": str,  # required
        }],
        "retry": {  # optional
            "count": Optional[float],
            "on_codes": List[float],  # optional
        },
        "cache": {  # optional
            "ttl": Optional[float],
            "type": Literal["exact_match"],  # required
        },
        "load_balancer": Union[CreateCompletionLoadBalancer1],  # optional
        "timeout": {  # optional
            "call_timeout": float,  # required
        },
        "thinking": Union[ThinkingConfigDisabledSchema, ThinkingConfigEnabledSchema, ThinkingConfigAdaptiveSchema],  # optional
        "plugins": [{  # optional
            "id": Literal["pii_redaction"],  # required
            "language": Optional[str],
            "regions": List[str],  # optional
            "entities": List[str],  # optional
            "entity_thresholds": Dict[str, float],  # optional
            "threshold": Optional[float],
            "on_failure": Optional[Literal["block", "passthrough"]],
        }],
        "orq": {  # optional
            "name": Optional[str],
            "retry": {  # optional
                "count": Optional[float],
                "on_codes": List[float],  # optional
            },
            "fallbacks": [{  # optional
                "model": str,  # required
            }],
            "prompt": {  # optional
                "id": str,  # required
                "version": Literal["latest"],  # required
            },
            "identity": {  # optional
                "id": str,  # required
                "display_name": Optional[str],
                "email": Optional[str],
                "metadata": List[Dict[str, Any]],  # optional
                "logo_url": Optional[str],
                "tags": List[str],  # optional
            },
            "contact": {  # optional
                "id": str,  # required
                "display_name": Optional[str],
                "email": Optional[str],
                "metadata": List[Dict[str, Any]],  # optional
                "logo_url": Optional[str],
                "tags": List[str],  # optional
            },
            "thread": {  # optional
                "id": str,  # required
                "tags": List[str],  # optional
            },
            "inputs": Union[Dict[str, Any], List[CreateCompletionInputs2]],  # optional
            "cache": {  # optional
                "ttl": Optional[float],
                "type": Literal["exact_match"],  # required
            },
            "knowledge_bases": [{  # optional
                "top_k": Optional[int],
                "threshold": Optional[float],
                "search_type": Optional[Literal["vector_search", "keyword_search", "hybrid_search"]],
                "filter_by": Union[Dict[str, CreateCompletionFilterBy1], CreateCompletionFilterByAnd, CreateCompletionFilterByOr],  # optional
                "search_options": {  # optional
                    "include_vectors": Optional[bool],
                    "include_metadata": Optional[bool],
                    "include_scores": Optional[bool],
                },
                "rerank_config": {  # optional
                    "model": str,  # required
                    "threshold": Optional[float],
                    "top_k": Optional[int],
                },
                "agentic_rag_config": {  # optional
                    "model": str,  # required
                },
                "knowledge_id": str,  # required
                "query": Optional[str],
            }],
            "load_balancer": Union[CreateCompletionLoadBalancerRouterCompletions1],  # optional
            "timeout": {  # optional
                "call_timeout": float,  # required
            },
        },
        "stream": Optional[bool],
    }
    ```

    ```typescript Node.js theme={"theme":{"light":"github-light","dark":"github-dark"}}
    {
      model: string;  // required
      prompt: string;  // required
      echo?: boolean;
      frequencyPenalty?: number;
      maxTokens?: number;
      presencePenalty?: number;
      seed?: number;
      stop?: string | string[];
      temperature?: number;
      topP?: number;
      n?: number;
      user?: string;
      name?: string;
      fallbacks?: {
        model: string;  // required
      }[];
      retry?: {
        count?: number;
        onCodes?: number[];
      };
      cache?: {
        ttl?: number;
        type: "exact_match";  // required
      };
      loadBalancer?: CreateCompletionLoadBalancer1;
      timeout?: {
        callTimeout: number;  // required
      };
      thinking?: ThinkingConfigDisabledSchema | ThinkingConfigEnabledSchema | ThinkingConfigAdaptiveSchema;
      plugins?: {
        id: "pii_redaction";  // required
        language?: string;
        regions?: string[];
        entities?: string[];
        entityThresholds?: Record<string, number>;
        threshold?: number;
        onFailure?: "block" | "passthrough";
      }[];
      orq?: {
        name?: string;
        retry?: {
          count?: number;
          onCodes?: number[];
        };
        fallbacks?: {
          model: string;  // required
        }[];
        prompt?: {
          id: string;  // required
          version: "latest";  // required
        };
        identity?: {
          id: string;  // required
          displayName?: string;
          email?: string;
          metadata?: Record<string, any>[];
          logoUrl?: string;
          tags?: string[];
        };
        contact?: {
          id: string;  // required
          displayName?: string;
          email?: string;
          metadata?: Record<string, any>[];
          logoUrl?: string;
          tags?: string[];
        };
        thread?: {
          id: string;  // required
          tags?: string[];
        };
        inputs?: { [k: string]: any } | CreateCompletionInputs2[];
        cache?: {
          ttl?: number;
          type: "exact_match";  // required
        };
        knowledgeBases?: {
          topK?: number;
          threshold?: number;
          searchType?: "vector_search" | "keyword_search" | "hybrid_search";
          filterBy?: { [k: string]: CreateCompletionFilterBy1 } | CreateCompletionFilterByAnd | CreateCompletionFilterByOr;
          searchOptions?: {
            includeVectors?: boolean;
            includeMetadata?: boolean;
            includeScores?: boolean;
          };
          rerankConfig?: {
            model: string;  // required
            threshold?: number;
            topK?: number;
          };
          agenticRagConfig?: {
            model: string;  // required
          };
          knowledgeId: string;  // required
          query?: string;
        }[];
        loadBalancer?: CreateCompletionLoadBalancerRouterCompletions1;
        timeout?: {
          callTimeout: number;  // required
        };
      };
      stream?: boolean;
    }
    ```
  </CodeGroup>
</Expandable>

<Expandable title="Response">
  <CodeGroup>
    ```python Python theme={"theme":{"light":"github-light","dark":"github-dark"}}
    {
        "id": str,
        "choices": [{
            "finish_reason": Literal["stop", "length", "content_filter", "tool_calls"],
            "index": float,
            "text": str,
        }],
        "created": Union[str, float],  # optional
        "model": str,
        "system_fingerprint": Optional[str],
        "object": str,
        "usage": {  # optional
            "completion_tokens": Optional[float],
            "prompt_tokens": Optional[float],
            "total_tokens": Optional[float],
            "prompt_tokens_details": {  # optional
                "cached_tokens": Optional[int],
                "cache_creation_tokens": Optional[int],
                "audio_tokens": Optional[int],
            },
            "completion_tokens_details": {  # optional
                "reasoning_tokens": Optional[float],
                "accepted_prediction_tokens": Optional[float],
                "rejected_prediction_tokens": Optional[float],
                "audio_tokens": Optional[int],
            },
        },
    }
    ```

    ```typescript Node.js theme={"theme":{"light":"github-light","dark":"github-dark"}}
    {
      id: string;
      choices: {
        finishReason: "stop" | "length" | "content_filter" | "tool_calls";
        index: number;
        text: string;
      }[];
      created?: string | number;
      model: string;
      systemFingerprint?: string;
      object: string;
      usage?: {
        completionTokens?: number;
        promptTokens?: number;
        totalTokens?: number;
        promptTokensDetails?: {
          cachedTokens?: number;
          cacheCreationTokens?: number;
          audioTokens?: number;
        };
        completionTokensDetails?: {
          reasoningTokens?: number;
          acceptedPredictionTokens?: number;
          rejectedPredictionTokens?: number;
          audioTokens?: number;
        };
      };
    }
    ```
  </CodeGroup>
</Expandable>


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.