File size: 1,758 Bytes
cd55ee8 11ff83b 4815889 cd55ee8 11ff83b cd55ee8 1d80ba8 cd55ee8 4815889 cd55ee8 4815889 40fce64 4815889 cd55ee8 11ff83b cd55ee8 11ff83b cd55ee8 b34ee0a cd55ee8 |
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 |
# app.py
from fastapi import FastAPI
from pydantic import BaseModel
from fastapi.middleware.cors import CORSMiddleware
from bot_instance import gemini_bot, llama_bot # singleton ErrorBot instance
from typing import List, Optional
app = FastAPI(title="ErrorBot API")
# ✅ Allow all origins (adjust in production)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# ---------------- Request Models ----------------
class MessageItem(BaseModel):
role: str # "user" or "bot"
content: str
class ChatRequest(BaseModel):
message: str
history: Optional[List[MessageItem]] = [] # optional conversation history
# ---------------- Endpoints ----------------
@app.get("/")
def root():
return {"status": "ok"}
# @app.post("/chat")
# def chat(request: ChatRequest):
# """
# Main chat endpoint:
# - Accepts a message and optional conversation history
# - Uses ErrorBot with RAG + LLM
# """
# history_list = [
# {"role": msg.role, "content": msg.content} for msg in request.history
# ]
# # Ask bot with history
# answer = bot.ask(request.message, history=history_list)
# return {"reply": answer}
@app.post("/gemini/chat")
def gemini_chat(request: ChatRequest):
history_list = [{"role": msg.role, "content": msg.content} for msg in request.history]
answer = gemini_bot.ask(request.message, history=history_list)
return {"reply": answer}
@app.post("/llama/chat")
def llama_chat(request: ChatRequest):
history_list = [{"role": msg.role, "content": msg.content} for msg in request.history]
answer = llama_bot.ask(request.message, history=history_list)
return {"reply": answer} |