-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
63 lines (52 loc) · 1.91 KB
/
main.py
File metadata and controls
63 lines (52 loc) · 1.91 KB
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
from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
from typing import Optional
from fastapi.responses import JSONResponse
import os
from dotenv import load_dotenv
from backend.repo_handler import RepoHandler
from backend.document_processor import DocumentProcessor
from backend.vector_database_manager import VectorDatabaseManager
from backend.chatbot_handler import ChatbotHandler
load_dotenv()
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
class RepoRequest(BaseModel):
repo_url: str
class ChatRequest(BaseModel):
question: str
repo_handler = None
chatbot = None
@app.post("/initialize")
async def initialize_repo(request: RepoRequest):
global repo_handler, chatbot
try:
repo_handler = RepoHandler(repo_url=request.repo_url, repo_path="test_repo/")
repo_handler.clone_repository()
doc_processor = DocumentProcessor(repo_path="test_repo/")
documents = doc_processor.process_documents()
vector_db_manager = VectorDatabaseManager(persist_directory="./data")
vector_db = vector_db_manager.initialize_db(documents)
chatbot = ChatbotHandler(vector_db)
return {"status": "success", "message": "Repository initialized successfully"}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@app.post("/chat")
async def chat(request: ChatRequest):
if not chatbot:
raise HTTPException(status_code=400, detail="Please initialize a repository first")
try:
answer = chatbot.get_response(request.question)
return {"answer": answer}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@app.get("/status")
async def status():
return {"initialized": chatbot is not None}