File size: 1,437 Bytes
a53be7e
 
 
 
 
 
 
 
f42134c
a53be7e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from __future__ import annotations

import os
from typing import Optional
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from rag_core import RAGEngine, InMemorySessionStore
from utils import load_settings
import pandas as pd

class ChatRequest(BaseModel):
    message: str
    session_id: Optional[str] = None
    max_words_memory: int = 5000


class ChatResponse(BaseModel):
    answer: str
    session_id: str


def _prompt_path() -> str:
    here = os.path.dirname(os.path.abspath(__file__))
    return os.path.join(here, "prompts", "system.md")


settings = load_settings()
engine = RAGEngine(settings, prompt_path=_prompt_path())
store = InMemorySessionStore()

app = FastAPI(
    title="Session 1 RAG API",
    description="FastAPI wrapper for the session_1 RAG engine.",
    version="0.1.0",
)


@app.get("/health", summary="Health check")
def health():
    return {"status": "ok"}


@app.post("/chat", response_model=ChatResponse, summary="Chat with the RAG engine")
def chat(req: ChatRequest):
    message = (req.message or "").strip()
    if not message:
        raise HTTPException(status_code=400, detail="message cannot be empty")

    session_id = (req.session_id or "default").strip() or "default"
    history = store.get_history(session_id)

    answer = engine.ask(message, history=history, max_words_memory=req.max_words_memory)
    return ChatResponse(answer=answer, session_id=session_id)