# Implement RAG at the Gateway Layer

Retrieval-Augmented Generation (RAG) grounds LLM responses in context retrieved from a knowledge base. API7 AI Gateway can perform this retrieval before proxying a request, which centralizes the enrichment flow instead of requiring each application to implement it. The current `ai-rag` plugin uses Azure OpenAI for embeddings and Azure AI Search for vector search.

## Overview[​](#overview "Direct link to Overview")

A RAG request follows this flow:

1. Client sends a chat request to API7 AI Gateway.
2. Gateway uses `ai-rag` to generate embeddings and run vector search against Azure AI Search.
3. Gateway injects retrieved context into the prompt.
4. Gateway forwards the enriched request to Azure OpenAI through `ai-proxy`.
5. Client receives a grounded answer.

## Prerequisites[​](#prerequisites "Direct link to Prerequisites")

* [cURL](https://curl.se/) is installed on the client used for validation.
* An API7 Gateway instance is running with the `ai-proxy` and `ai-rag` plugins available.
* An Azure OpenAI resource in [Microsoft Foundry](https://ai.azure.com/) with chat and embedding deployments. Record the resource name, API key, and both deployment names.
* An Azure AI Search service has a populated index with a vector field whose name and dimensions match the request. The examples use `contentVector` with 1,024 dimensions, matching Microsoft's [basic vector workflow](https://github.com/Azure/azure-search-vector-samples/blob/ea17a7604d6f4d67fba50c68b3e0a149aa9c05c0/demo-python/code/basic-vector-workflow/azure-search-vector-python-sample.ipynb).
* [ADC is installed and connected](https://docs.api7.ai/api7-gateway/getting-started/quick-start.md#step-3-create-a-service-and-route) to the target Gateway Group if you use the ADC configuration.

## Configure the RAG Plugin[​](#configure-the-rag-plugin "Direct link to Configure the RAG Plugin")

Configure `ai-rag` and `ai-proxy` on the same route so retrieval and generation happen in one request path.

* Admin API
* ADC

Obtain a [Dashboard token](https://docs.api7.ai/api7-gateway/reference/obtain-dashboard-token.md), then export it with the target gateway group and an existing service ID. The quickstart Gateway Group uses `default`. If needed, first [create or reuse a service](https://docs.api7.ai/api7-gateway/ai-gateway/get-started.md#create-or-reuse-a-service).

```
export API_KEY=replace-with-dashboard-token

export GATEWAY_GROUP_ID=default

export SERVICE_ID=replace-with-service-id
```

Create the route through the Admin API:

```
curl -k "https://localhost:7443/apisix/admin/routes/rag-azure?gateway_group_id=${GATEWAY_GROUP_ID}" -X PUT \

  -H "X-API-KEY: ${API_KEY}" \

  -H "Content-Type: application/json" \

  -d @- <<EOF

  {

    "name": "rag-azure",

    "service_id": "$SERVICE_ID",

    "paths": ["/v1/chat/completions"],

    "plugins": {

      "ai-proxy": {

        "provider": "openai-compatible",

        "auth": {

          "header": {

            "api-key": "YOUR_AZURE_OPENAI_KEY"

          }

        },

        "options": {

          "model": "YOUR_CHAT_DEPLOYMENT"

        },

        "override": {

          "endpoint": "https://YOUR-RESOURCE.openai.azure.com/openai/v1/chat/completions"

        }

      },

      "ai-rag": {

        "embeddings_provider": {

          "azure_openai": {

            "endpoint": "https://YOUR-RESOURCE.openai.azure.com/openai/v1/embeddings",

            "api_key": "YOUR_AZURE_OPENAI_KEY"

          }

        },

        "vector_search_provider": {

          "azure_ai_search": {

            "endpoint": "https://YOUR-SEARCH.search.windows.net/indexes/YOUR-INDEX/docs/search?api-version=2024-07-01",

            "api_key": "YOUR_AZURE_SEARCH_KEY"

          }

        }

      }

    }

  }

EOF
```

❶ Set `provider` to `openai-compatible` so the chat deployment name remains in the Azure OpenAI v1 request body.

❷ Set the Azure OpenAI v1 Chat Completions endpoint.

❸ Configure `ai-rag` with Azure OpenAI for embeddings and Azure AI Search for vector retrieval. The embedding endpoint uses the same Azure OpenAI resource at `/openai/v1/embeddings`.

Define the Service and Route in an ADC configuration:

adc.yaml

```
services:

  - name: rag-azure-service

    labels:

      docs-example: api7-rag

    routes:

      - uris:

          - /v1/chat/completions

        name: rag-azure

        plugins:

          ai-proxy:

            provider: openai-compatible

            auth:

              header:

                api-key: "YOUR_AZURE_OPENAI_KEY"

            options:

              model: YOUR_CHAT_DEPLOYMENT

            override:

              endpoint: https://YOUR-RESOURCE.openai.azure.com/openai/v1/chat/completions

          ai-rag:

            embeddings_provider:

              azure_openai:

                endpoint: https://YOUR-RESOURCE.openai.azure.com/openai/v1/embeddings

                api_key: YOUR_AZURE_OPENAI_KEY

            vector_search_provider:

              azure_ai_search:

                endpoint: https://YOUR-SEARCH.search.windows.net/indexes/YOUR-INDEX/docs/search?api-version=2024-07-01

                api_key: YOUR_AZURE_SEARCH_KEY

    upstream:

      type: roundrobin

      nodes:

        - host: httpbin.org

          port: 80

          weight: 1
```

❶ Set `provider` to `openai-compatible` so the chat deployment name remains in the Azure OpenAI v1 request body.

❷ Set the Azure OpenAI v1 Chat Completions endpoint.

❸ Configure `ai-rag` with Azure OpenAI for embeddings and Azure AI Search for vector retrieval. The embedding endpoint uses the same Azure OpenAI resource at `/openai/v1/embeddings`.

Preview changes owned by this example and confirm that the diff contains no unintended updates or deletions:

```
adc diff -f adc.yaml \

  --include-resource-type service \

  --label-selector docs-example=api7-rag
```

Synchronize the reviewed service configuration:

```
adc sync -f adc.yaml \

  --include-resource-type service \

  --label-selector docs-example=api7-rag
```

For the full configuration reference, see [`ai-rag`](https://docs.api7.ai/hub/ai-rag.md) and [`ai-proxy`](https://docs.api7.ai/hub/ai-proxy.md).

## Validate the Configuration[​](#validate-the-configuration "Direct link to Validate the Configuration")

Send a request that includes the `ai_rag` field. This example requests a 1,024-dimension embedding; change it to match the Azure AI Search vector field, or omit `dimensions` when the field uses the embedding deployment's default dimensions.

```
curl "http://127.0.0.1:9080/v1/chat/completions" -X POST \

  -H "Content-Type: application/json" \

  -d '{

    "model": "YOUR_CHAT_DEPLOYMENT",

    "messages": [

      {

        "role": "user",

        "content": "Based on our internal docs, what are the main capabilities of API7 AI Gateway?"

      }

    ],

    "ai_rag": {

      "vector_search": {

        "fields": "contentVector"

      },

      "embeddings": {

        "model": "YOUR_EMBEDDING_DEPLOYMENT",

        "input": "API7 AI Gateway capabilities overview",

        "dimensions": 1024

      }

    }

  }'
```

❶ Use a question that depends on your indexed knowledge base so you can confirm grounding behavior.

❷ `ai_rag.vector_search.fields` identifies the vector field used for retrieval.

❸ `ai_rag.embeddings` identifies the embedding deployment and supplies the text and dimensions used for vector retrieval. The dimensions must match the vector field in the search index.

You should receive a standard chat completion response with an answer grounded in your indexed documents. Compared with a request without `ai_rag`, the answer should be more specific and aligned with your internal knowledge base.

## Best Practices[​](#best-practices "Direct link to Best Practices")

* Keep your knowledge base and index up to date. Stale documents reduce answer quality.
* Keep the embedding dimensions aligned with the Azure AI Search vector field.
* Monitor token usage closely. Added context increases prompt tokens and cost.

## Next Steps[​](#next-steps "Direct link to Next Steps")

* [Track token usage and costs](https://docs.api7.ai/api7-gateway/ai-gateway/use-cases/ai-observability-and-cost-tracking.md) to monitor RAG overhead.
* [Apply token-based budgets](https://docs.api7.ai/api7-gateway/ai-gateway/use-cases/token-rate-limiting-and-quota-management.md) to control spend.
* Review plugin references: [`ai-rag`](https://docs.api7.ai/hub/ai-rag.md), [`ai-proxy`](https://docs.api7.ai/hub/ai-proxy.md).
