|
|
|
|
|
from fastapi import FastAPI |
|
|
from pydantic import BaseModel |
|
|
from fastapi.middleware.cors import CORSMiddleware |
|
|
from bot_instance import gemini_bot, llama_bot |
|
|
from typing import List, Optional |
|
|
|
|
|
app = FastAPI(title="ErrorBot API") |
|
|
|
|
|
|
|
|
app.add_middleware( |
|
|
CORSMiddleware, |
|
|
allow_origins=["*"], |
|
|
allow_credentials=True, |
|
|
allow_methods=["*"], |
|
|
allow_headers=["*"], |
|
|
) |
|
|
|
|
|
|
|
|
class MessageItem(BaseModel): |
|
|
role: str |
|
|
content: str |
|
|
|
|
|
class ChatRequest(BaseModel): |
|
|
message: str |
|
|
history: Optional[List[MessageItem]] = [] |
|
|
|
|
|
|
|
|
@app.get("/") |
|
|
def root(): |
|
|
return {"status": "ok"} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@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} |