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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
"""
RAG Agent FastAPI Server using OpenAI Agents SDK

Provides POST /chat endpoint for grounded Q&A using OpenAI Agents SDK
and retrieval from Qdrant via Spec-2's retrieve.py module.
"""

import os
import sys
import uuid
import asyncio
from datetime import datetime
from typing import List, Dict, Any, Optional

from fastapi import FastAPI, HTTPException
from fastapi.responses import JSONResponse
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel, Field, validator
from dotenv import load_dotenv

# Load environment first
load_dotenv()

from agents import OpenAIChatCompletionsModel
from openai import AsyncOpenAI

# Get OpenRouter API key from environment
OPENROUTER_API_KEY = os.getenv("OPENROUTER_API_KEY")
if not OPENROUTER_API_KEY:
    raise ValueError(
        "OPENROUTER_API_KEY environment variable must be set. "
        "Get a free key from https://openrouter.ai/"
    )

# Configure AsyncOpenAI client for OpenRouter
client = AsyncOpenAI(
    api_key=OPENROUTER_API_KEY,
    base_url="https://openrouter.ai/api/v1",
)

# Use OpenRouter's free model: tencent/hy3-preview:free
third_party_model = OpenAIChatCompletionsModel(
    openai_client=client, model="tencent/hy3-preview:free"
)

# Make backend package importable
current_dir = os.path.dirname(os.path.abspath(__file__))
if current_dir not in sys.path:
    sys.path.insert(0, current_dir)

# Import backend modules
from config import get_config
from retrieve import search as retrieve_search
from logging_config import setup_logging

# Import OpenAI Agents SDK (must be installed separately)
try:
    from agents import Agent, Runner, function_tool, ModelSettings, ToolCallOutputItem
except ImportError:
    raise ImportError(
        "openai-agents package required. Install: pip install openai-agents"
    )

# Setup logging
logger = setup_logging("agent")

# Initialize FastAPI app
app = FastAPI(
    title="RAG Book Chatbot API",
    version="1.0.0",
    description="Chatbot for humanoid robotics book using OpenAI Agents SDK",
)

# ============ CORS Configuration ============

app.add_middleware(
    CORSMiddleware,
    allow_origins=[
        "http://localhost:3000",
        "http://127.0.0.1:3000",
        "https://hackathon-1-humanoid-ai-robotics.vercel.app",
        "https://*.vercel.app",
    ],
    allow_credentials=True,
    allow_methods=["GET", "POST", "PUT", "DELETE", "OPTIONS"],
    allow_headers=["Content-Type", "Authorization"],
)

# ============ Pydantic Models ============


class ChatRequest(BaseModel):
    question: str = Field(..., min_length=1, max_length=1000)

    @validator("question")
    def validate_question(cls, v):
        if not v or not v.strip():
            raise ValueError("Question cannot be empty")
        return v.strip()


class Source(BaseModel):
    url: str
    chunk_index: int
    text_snippet: str


class ChatResponse(BaseModel):
    answer: str
    sources: List[Source]
    tokens_used: int
    agent_trace: Optional[str] = None


class HealthStatus(BaseModel):
    status: str
    qdrant: str
    openai: str
    timestamp: str


# ============ Retrieval Tool ============


@function_tool
def retrieve_chunks(query: str, top_k: int = 5) -> List[Dict[str, Any]]:
    """
    Retrieve relevant book chunks from Qdrant.

    Args:
        query: User's question
        top_k: Number of chunks to retrieve (default: 5, max: 10)

    Returns:
        List of chunks with url, chunk_index, text, score, and source_number
    """
    logger.info(
        f"[Tool] retrieve_chunks called: query='{query[:100]}...', top_k={top_k}"
    )

    try:
        import cohere
        from qdrant_client import QdrantClient

        cfg = get_config()
        cohere_client = cohere.ClientV2(api_key=cfg["cohere_api_key"])
        qdrant_client = QdrantClient(
            url=cfg["qdrant_url"], api_key=cfg["qdrant_api_key"]
        )
        collection_name = cfg["qdrant_collection"]

        results = retrieve_search(
            query_text=query,
            cohere_client=cohere_client,
            qdrant_client=qdrant_client,
            collection_name=collection_name,
            top_k=top_k,
        )

        chunks = []
        for i, result in enumerate(results):
            payload = result.get("payload", {})
            chunks.append(
                {
                    "url": payload.get("url", ""),
                    "chunk_index": payload.get("chunk_index", i),
                    "text": payload.get("text", ""),
                    "score": result.get("score", 0.0),
                    "source_number": i + 1,
                }
            )

        logger.info(f"[Tool] Retrieved {len(chunks)} chunks")
        return chunks

    except Exception as e:
        logger.error(f"[Tool] Retrieval failed: {e}", exc_info=True)
        raise


# ============ Agent Definition ============


def get_agent_instructions() -> str:
    return """You are a helpful assistant answering questions about a humanoid robotics book.

IMPORTANT GROUNDING RULES:
1. Answer ONLY using the retrieved book content provided by the retrieve_chunks tool.
2. Do NOT use external knowledge or make up information.
3. If the retrieved content does not contain relevant information, say "I couldn't find relevant information in the book."
4. Always cite your sources using the format [Source 1], [Source 2], etc. Each source number corresponds to the chunk number from the tool.
5. Be concise and accurate.

Your responses should be helpful, clear, and grounded exclusively in the provided context."""


def create_agent():
    return Agent(
        name="RAG Book Assistant",
        instructions=get_agent_instructions(),
        tools=[retrieve_chunks],
        model=third_party_model,
        model_settings=ModelSettings(temperature=0.7, max_tokens=500),
    )


_agent_instance = None


def get_agent():
    """Lazy singleton agent instance."""
    global _agent_instance
    if _agent_instance is None:
        _agent_instance = create_agent()
    return _agent_instance


# ============ Health Checks ============


def check_qdrant_health() -> str:
    try:
        from qdrant_client import QdrantClient

        cfg = get_config()
        client = QdrantClient(url=cfg["qdrant_url"], api_key=cfg["qdrant_api_key"])
        client.get_collection(cfg["qdrant_collection"])
        return "connected"
    except Exception as e:
        logger.warning(f"Qdrant health check failed: {e}")
        return "disconnected"


def check_openai_health() -> str:
    try:
        api_key = os.getenv("OPENAI_API_KEY")
        if not api_key:
            return "disconnected"
        import openai

        client = openai.OpenAI(api_key=api_key)
        # Simple models.list call to verify API connectivity
        client.models.list()
        return "connected"
    except Exception as e:
        logger.warning(f"OpenAI health check failed: {e}")
        return "disconnected"


# ============ FastAPI Endpoints ============


@app.post("/chat")
async def chat_endpoint(request: ChatRequest):
    request_id = str(uuid.uuid4())[:8]
    question = request.question.strip()

    logger.info(f"[{request_id}] Received chat: {question[:100]}...")

    try:
        logger.info(f"[{request_id}] Initializing agent...")
        agent = get_agent()
        logger.info(f"[{request_id}] Agent initialized successfully")

        # Use async Runner.run (native async, no blocking)
        logger.info(f"[{request_id}] Starting agent run...")
        result = await asyncio.wait_for(
            Runner.run(agent, question),
            timeout=60.0,  # Increased to 60s to handle large questions
        )
        logger.info(f"[{request_id}] Agent run completed")

        # Extract sources from tool call outputs
        sources = []
        if result.new_items:
            for item in result.new_items:
                if isinstance(item, ToolCallOutputItem):
                    output = item.output
                    if isinstance(output, list):
                        for chunk in output:
                            sources.append(
                                Source(
                                    url=chunk.get("url", ""),
                                    chunk_index=chunk.get("chunk_index", 0),
                                    text_snippet=chunk.get("text", "")[:200],
                                )
                            )

        # Get token usage
        tokens_used = 0
        if result.context_wrapper and hasattr(result.context_wrapper, "usage"):
            tokens_used = result.context_wrapper.usage.total_tokens

        response = ChatResponse(
            answer=result.final_output,
            sources=sources,
            tokens_used=tokens_used,
            agent_trace=f"{request_id}: completed",
        )

        logger.info(
            f"[{request_id}] Completed: tokens={tokens_used}, sources={len(sources)}"
        )
        return response

    except asyncio.TimeoutError:
        logger.error(f"[{request_id}] Timeout after 60s")
        return JSONResponse(
            status_code=504,
            content={
                "error": "timeout",
                "message": "The chatbot is taking too long to respond. Please try a shorter or simpler question.",
            },
        )

    except Exception as e:
        logger.error(
            f"[{request_id}] Error during chat: {type(e).__name__}: {e}", exc_info=True
        )

        # Check for specific error types
        error_str = str(e).lower()
        if (
            "api" in error_str
            or "authentication" in error_str
            or "401" in error_str
            or "403" in error_str
        ):
            status_code = 503
            error_code = "api_auth_failed"
            message = "Authentication failed with API provider. Check your OPENROUTER_API_KEY."
        elif "openrouter" in error_str or "openai" in error_str:
            status_code = 503
            error_code = "openai_failed"
            message = f"API service error: {str(e)[:100]}"
        else:
            status_code = 500
            error_code = "internal_error"
            message = f"Internal error: {str(e)[:100]}"

        logger.error(f"[{request_id}] Returning {status_code}: {error_code}")
        return JSONResponse(
            status_code=status_code,
            content={"error": error_code, "message": message, "request_id": request_id},
        )


@app.get("/health", response_model=HealthStatus)
async def health_check():
    request_id = str(uuid.uuid4())[:8]
    qdrant = check_qdrant_health()
    openai = check_openai_health()  # sync call
    status = (
        "healthy" if qdrant == "connected" and openai == "connected" else "degraded"
    )
    return HealthStatus(
        status=status,
        qdrant=qdrant,
        openai=openai,
        timestamp=datetime.utcnow().isoformat() + "Z",
    )


@app.get("/test-agent")
async def test_agent_endpoint():
    """Test endpoint to verify agent can be initialized."""
    request_id = str(uuid.uuid4())[:8]
    try:
        logger.info(f"[{request_id}] Testing agent initialization...")
        agent = get_agent()
        logger.info(f"[{request_id}] Agent initialized successfully")
        return {
            "status": "ok",
            "message": "Agent initialized successfully",
            "agent_name": agent.name if hasattr(agent, "name") else "unknown",
        }
    except Exception as e:
        logger.error(f"[{request_id}] Agent init test failed: {e}", exc_info=True)
        return JSONResponse(
            status_code=500,
            content={
                "status": "error",
                "message": f"Agent initialization failed: {str(e)}",
                "request_id": request_id,
            },
        )


@app.on_event("startup")
async def startup_event():
    logger.info("=" * 60)
    logger.info("RAG Agent FastAPI Server Starting")
    logger.info("=" * 60)

    if not os.getenv("OPENROUTER_API_KEY"):
        logger.error("OPENROUTER_API_KEY not set - chat will fail!")
    else:
        logger.info("OPENROUTER_API_KEY is configured")

    # Test retrieval
    try:
        import cohere
        from qdrant_client import QdrantClient

        cfg = get_config()
        cohere_client = cohere.ClientV2(api_key=cfg["cohere_api_key"])
        qdrant_client = QdrantClient(
            url=cfg["qdrant_url"], api_key=cfg["qdrant_api_key"]
        )
        test_result = retrieve_search(
            query_text="test",
            cohere_client=cohere_client,
            qdrant_client=qdrant_client,
            collection_name=cfg["qdrant_collection"],
            top_k=1,
        )
        logger.info(f"Retrieval test OK: {len(test_result)} results")
    except Exception as e:
        logger.error(f"Retrieval test failed: {e}")

    logger.info("Server startup complete")


if __name__ == "__main__":
    import uvicorn

    uvicorn.run(app, host="0.0.0.0", port=8000)