> ## 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.

# Knowledge bases via AI Gateway

> Integrate knowledge bases through the AI Gateway. Enable RAG retrieval in LLM calls with automatic context injection for enhanced AI responses.

**Use Cases**

* Grounding responses in proprietary documents without fine-tuning.
* Internal Q\&A bots over company handbooks, policies, or codebases.
* Adding domain-specific knowledge to a general model without prompt stuffing.
* Reducing hallucinations by giving the model access to authoritative sources at query time.

***

## Prerequisite

[Knowledge Bases](/docs/ai-studio/ai-engineering/knowledge-bases-memory-stores) are made to provide relevant and specific information for an LLM to use. To get started, see [Creating a Knowledge Base](/docs/ai-studio/ai-engineering/knowledge-bases-memory-stores), Knowledge Bases need to be enriched with the sources documents and configured to expose chunks fitting your use case.

<Info>
  The **name** of the Knowledge Base will be used as `knowledge_id` in the model generation.
</Info>

<Note>
  The `knowledge_bases` field is available on the Chat Completions endpoint (`/v3/router/chat/completions`) only, via the `orq` extension object. It is not supported on the Responses API (`/v3/router/responses`).
</Note>

## Quick Start

Using the created [Knowledge Base](/docs/ai-studio/ai-engineering/knowledge-bases-memory-stores), and its `id`, include the `knowledge_bases` payload within your model generation call.

> The knowledge\_bases payload contains query configuration and search type, to learn more, see [Search Modes](/docs/ai-studio/ai-engineering/knowledge-bases-memory-stores#search-modes) and [Chunking Strategy](/docs/ai-studio/ai-engineering/knowledge-bases-memory-stores#datasource-and-chunking).

<CodeGroup>
  ```typescript TypeScript (Chat Completions) theme={"theme":{"light":"github-light","dark":"github-dark"}}
  import OpenAI from "openai";

  const openai = new OpenAI({
    apiKey: process.env.ORQ_API_KEY,
    baseURL: "https://api.orq.ai/v3/router",
  });

  const response = await openai.chat.completions.create({
    model: "openai/gpt-4o",
    messages: [{ role: "user", content: "How can I upgrade my account?" }],
    orq: {
      knowledge_bases: [
        {
          knowledge_id: "api-documentation",
          top_k: 5,
          threshold: 0.7,
          search_type: "hybrid_search",
        },
      ],
    },
  });
  ```

  ```python Python (Chat Completions) theme={"theme":{"light":"github-light","dark":"github-dark"}}
  from openai import OpenAI
  import os

  client = OpenAI(
      api_key=os.environ.get("ORQ_API_KEY"),
      base_url="https://api.orq.ai/v3/router",
  )

  response = client.chat.completions.create(
      model="openai/gpt-4o",
      messages=[{"role": "user", "content": "How can I upgrade my account?"}],
      extra_body={
          "orq": {
              "knowledge_bases": [
                  {
                      "knowledge_id": "api-documentation",
                      "top_k": 5,
                      "threshold": 0.7,
                      "search_type": "hybrid_search",
                  }
              ]
          }
      },
  )
  ```

  ```bash cURL (Chat Completions) theme={"theme":{"light":"github-light","dark":"github-dark"}}
  curl -X POST https://api.orq.ai/v3/router/chat/completions \
    -H "Authorization: Bearer $ORQ_API_KEY" \
    -H "Content-Type: application/json" \
    -d '{
      "model": "openai/gpt-4o",
      "messages": [{"role": "user", "content": "How can I upgrade my account?"}],
      "orq": {
        "knowledge_bases": [
          {
            "knowledge_id": "api-documentation",
            "top_k": 5,
            "threshold": 0.7,
            "search_type": "hybrid_search"
          }
        ]
      }
    }'
  ```
</CodeGroup>

<Check>
  **Orq** will automatically enrich the model generation with the given context and query to the knowledge base
</Check>

<Frame caption="Within the Traces, you can see the knowledge retrieval step Orq.ai automatically injects into your call to enrich the model context with the linked Knowledge Base.">
  <img src="https://mintcdn.com/orqai/E8L3R46ivX7g9-QI/images/docs/9a9e1e0325d8b216f57440026d7006bfc3bf9d0da01828ebfc072b6ab97554d7-Screenshot_2025-10-13_at_22.16.38.png?fit=max&auto=format&n=E8L3R46ivX7g9-QI&q=85&s=8567abc13e87f09ac8e1e4357fd12c65" alt="Within the Traces, you can see the knowledge retrieval step Orq.ai automatically injects into your call to enrich the model context with the linked Knowledge Base." width="491" height="477" data-path="images/docs/9a9e1e0325d8b216f57440026d7006bfc3bf9d0da01828ebfc072b6ab97554d7-Screenshot_2025-10-13_at_22.16.38.png" />

  [Knowledge Base](/docs/ai-studio/ai-engineering/knowledge-bases-memory-stores)
</Frame>
