File size: 5,732 Bytes
c02c9d3
 
 
 
 
 
69a353a
c02c9d3
 
 
 
58d4ba0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
---
title: Rag Agent Workbench Api
emoji: πŸ‘
colorFrom: indigo
colorTo: red
sdk: docker
app_port: 7860
pinned: false
license: mit
---

# 🧠 RAG Agent Workbench β€” Backend API (FastAPI on Docker Spaces)

A production-style Retrieval-Augmented Generation (RAG) backend deployed on **Hugging Face Spaces (Docker)**.

- **Vector DB:** Pinecone (serverless)  
- **LLM:** Groq (OpenAI-compatible)  
- **Web fallback:** Tavily (optional)  
- **Orchestration:** LangGraph / LangChain  
- **Observability:** LangSmith tracing (optional)  
- **Security:** API Key required (Swagger + OpenAPI + endpoints)

> This Space is public, but the API is protected via `X-API-Key` so it can’t be used without your key.

---

## βœ… API Endpoints

Typical endpoints you can expect:

- `GET /health` β€” health check  
- `POST /chat` β€” RAG chat (optionally web fallback)  
- `POST /search` β€” semantic retrieval  
- `POST /documents/upload-text` β€” upload raw text for indexing  
- `POST /ingest/wiki` β€” ingest from Wikipedia REST API  
- `POST /ingest/arxiv` β€” ingest from arXiv API  
- `POST /ingest/openalex` β€” ingest from OpenAlex API  
- `GET /documents/stats` β€” namespace/vector stats  
- `GET /metrics` β€” in-app metrics (latency, counts, cache hits, etc.)

Swagger/OpenAPI:
- `/docs` (Swagger UI)
- `/openapi.json`

> If API key protection is enabled, `/docs` and `/openapi.json` are also protected.

---

## πŸ” Authentication (API Key)

All protected endpoints require:

- Header: `X-API-Key: <YOUR_API_KEY>`

If `API_KEY` is set in Space secrets, anonymous access is blocked.

---

## βš™οΈ Configuration (Space Secrets & Variables)

Hugging Face Spaces lets you set **Secrets/Variables in Space Settings**; inside the container they are available as environment variables. :contentReference[oaicite:1]{index=1}

Go to:
**Space β†’ Settings β†’ Variables and secrets**

### Required (Pinecone)
- `PINECONE_API_KEY` (Secret)
- `PINECONE_HOST` (Secret or Variable)
- `PINECONE_INDEX_NAME` (Variable) β€” e.g. `rag-agent-workbench`
- `PINECONE_NAMESPACE` (Variable) β€” e.g. `dev`
- `PINECONE_TEXT_FIELD` (Variable) β€” `content` (for integrated embedding field mapping)

### Required (Groq)
Groq supports OpenAI-compatible clients via `base_url = https://api.groq.com/openai/v1`. :contentReference[oaicite:2]{index=2}

- `GROQ_API_KEY` (Secret)
- `GROQ_BASE_URL` (Variable) β€” `https://api.groq.com/openai/v1`
- `GROQ_MODEL` (Variable) β€” choose your Groq model (e.g., `llama-3.1-8b-instant`)

### Optional (Tavily web fallback)
- `TAVILY_API_KEY` (Secret)

### Optional (LangSmith tracing)
LangSmith commonly uses environment variables like `LANGCHAIN_API_KEY` and `LANGCHAIN_TRACING_V2=true`; project can be set via `LANGCHAIN_PROJECT` (or `LANGSMITH_PROJECT` depending on SDK usage). :contentReference[oaicite:3]{index=3}

- `LANGCHAIN_TRACING_V2` (Variable) β€” `true`
- `LANGCHAIN_API_KEY` (Secret)
- `LANGCHAIN_PROJECT` (Variable) β€” e.g. `rag-agent-workbench`

### Strongly recommended (Security)
- `API_KEY` (Secret) β€” required to call API endpoints

### Optional (Ops toggles)
- `LOG_LEVEL` β€” `INFO`
- `CACHE_ENABLED` β€” `true/false`
- `RATE_LIMIT_ENABLED` β€” `true/false`
- `ALLOWED_ORIGINS` β€” comma-separated list (if you need strict CORS)

> Any change to Space secrets/variables triggers a restart. :contentReference[oaicite:4]{index=4}

---

## πŸš€ How to Test This Space

Replace:
- `SPACE_URL = https://<your-space-subdomain>.hf.space`
- `KEY = your API key`

### 1) Health
curl -i "$SPACE_URL/health"

### 2) OpenAPI / Swagger (requires key if protected)
curl -i "$SPACE_URL/openapi.json" -H "X-API-Key: $KEY"

### 3) Search
curl -X POST "$SPACE_URL/search" \
  -H "Content-Type: application/json" \
  -H "X-API-Key: $KEY" \
  -d '{"query":"What is RAG?","top_k":5,"namespace":"dev"}'

### 4) Chat
curl -X POST "$SPACE_URL/chat" \
  -H "Content-Type: application/json" \
  -H "X-API-Key: $KEY" \
  -d '{"query":"Explain RAG in 5 bullets.","namespace":"dev","top_k":5,"use_web_fallback":false}'

### 5) Upload text document
curl -X POST "$SPACE_URL/documents/upload-text" \
  -H "Content-Type: application/json" \
  -H "X-API-Key: $KEY" \
  -d '{
        "title":"Demo doc",
        "source":"space-readme",
        "namespace":"dev",
        "text":"RAG = retrieval augmented generation ...",
        "metadata":{"tag":"demo"}
      }'

---

## 🐳 Local Docker Run (same image behavior as Spaces)

Docker Spaces is configured via YAML front-matter (`sdk: docker`, `app_port`) and expects the app to listen on the configured port (commonly 7860). :contentReference[oaicite:5]{index=5}

Build:
docker build -t rag-agent-workbench-api:local .

Run:
docker run --rm -p 7860:7860 \
  -e API_KEY="your_key" \
  -e PINECONE_API_KEY="..." \
  -e PINECONE_HOST="..." \
  -e PINECONE_INDEX_NAME="rag-agent-workbench" \
  -e PINECONE_NAMESPACE="dev" \
  -e PINECONE_TEXT_FIELD="content" \
  -e GROQ_API_KEY="..." \
  -e GROQ_BASE_URL="https://api.groq.com/openai/v1" \
  -e GROQ_MODEL="llama-3.1-8b-instant" \
  rag-agent-workbench-api:local

Then open:
- http://127.0.0.1:7860/health
- http://127.0.0.1:7860/docs (add X-API-Key if protected)

---

## 🧯 Troubleshooting

### Space starts but endpoints 404 / not reachable
- Confirm `app_port: 7860` in YAML and container listens on that port. :contentReference[oaicite:6]{index=6}

### 403 on everything
- You enabled `API_KEY` but forgot to send `X-API-Key` header.

### Web fallback not working
- Ensure `TAVILY_API_KEY` is set in Space secrets.

### LangSmith traces missing
- Ensure `LANGCHAIN_TRACING_V2=true` and `LANGCHAIN_API_KEY` are set. :contentReference[oaicite:7]{index=7}

---

## πŸ“„ License
MIT recommended for open-source portfolios