# RAG Agent Demo

A multi-agent RAG system demonstrating plano's agent filter chain with HTTP protocol.

## Architecture

This demo consists of four components:
1. **Input Guards** (HTTP filter) - Validates queries are within TechCorp's domain
2. **Query Rewriter** (HTTP filter) - Rewrites user queries for better retrieval
3. **Context Builder** (HTTP filter) - Retrieves relevant context from knowledge base
4. **RAG Agent** (REST) - Generates final responses based on augmented context

## Components

### Input Guards Filter (HTTP)
- **Port**: 10500
- Validates queries are within TechCorp's domain
- Rejects queries about other companies or unrelated topics

### Query Rewriter Filter (HTTP)
- **Port**: 10501
- Improves queries using LLM before retrieval

### Context Builder Filter (HTTP)
- **Port**: 10502
- Augments queries with relevant passages from knowledge base

### RAG Agent (REST/OpenAI)
- **Port**: 10505
- **Endpoint**: `/v1/chat/completions`
- Generates responses using OpenAI-compatible API

## Quick Start

### 1. Start the demo
```bash
export OPENAI_API_KEY="your-key"
./run_demo.sh
```

This starts Plano natively and runs agents as local processes:
- Input Guards HTTP server on port 10500
- Query Rewriter HTTP server on port 10501
- Context Builder HTTP server on port 10502
- RAG Agent REST server on port 10505

Plano runs natively on the host (port 8001 and 12000).

To also start AnythingLLM (chat UI) and Jaeger (tracing):

```bash
./run_demo.sh --with-ui
```

This additionally starts:
- Jaeger UI for viewing traces at http://localhost:16686
- AnythingLLM at http://localhost:3001 for interactive queries

### 2. Test the system

**Option A: Using curl (recommended)**
```bash
curl -X POST http://localhost:8001/v1/chat/completions \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-4o",
    "messages": [{"role": "user", "content": "What is the guaranteed uptime for TechCorp?"}]
  }'
```

## Configuration

The `config.yaml` defines how agents are connected:

```yaml
filters:
  - id: input_guards
    url: http://localhost:10500
    # type: mcp (default)
    # tool: input_guards (default - same as filter id)

  - id: query_rewriter
    url: http://localhost:10501
    # type: mcp (default)

  - id: context_builder
    url: http://localhost:10502
```

## How It Works

1. User sends request to plano listener on port 8001
2. Request passes through HTTP filter chain:
   - **Input Guards** validates the query is within TechCorp's domain
   - **Query Rewriter** rewrites the query for better retrieval
   - **Context Builder** augments query with relevant knowledge base passages
3. Augmented request is forwarded to **RAG Agent** REST endpoint
4. RAG Agent generates final response using LLM

## Additional Configuration

See `config.yaml` for the complete filter chain setup. The HTTP filters use `type: http` to indicate they communicate over plain HTTP POST rather than MCP.

See `sample_queries.md` for example queries to test the RAG system.

Example request:
```bash
curl -X POST http://localhost:8001/v1/chat/completions \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-4o",
    "messages": [
      {
        "role": "user",
        "content": "What is the guaranteed uptime for TechCorp?"
      }
    ]
  }'
```
- `LLM_GATEWAY_ENDPOINT` - Plano endpoint (default: `http://localhost:12000/v1`)
- `OPENAI_API_KEY` - OpenAI API key for model providers

## Additional Resources

- See `sample_queries.md` for more example queries
- See `config.yaml` for complete configuration details
