-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
150 lines (114 loc) · 4.6 KB
/
main.py
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
import os
import logging
from typing import List
from fastapi import FastAPI, HTTPException, Request, File, UploadFile, Query
from fastapi.responses import HTMLResponse
from fastapi.staticfiles import StaticFiles
from pydantic import BaseModel
from llama_index.core import StorageContext, load_index_from_storage
from fastapi.middleware.cors import CORSMiddleware
import os
from collection_manager import CollectionManager
from collection import Collection
from dotenv import load_dotenv
load_dotenv()
COLLECTIONS_DIR = os.getenv("COLLECTIONS_DIR", "collections")
if not os.path.exists(COLLECTIONS_DIR):
os.makedirs(COLLECTIONS_DIR)
# Set up logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
app = FastAPI()
# CORS middleware setup
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Serve static files
app.mount("/static", StaticFiles(directory="static"), name="static")
class Question(BaseModel):
text: str
class Directory(BaseModel):
path: str
class DeleteDocuments(BaseModel):
doc_ids: List[str]
@app.post("/collections/{collection_name}/query")
async def query(collection_name: str, question: Question):
try:
collection = collection_manager.get_collection(collection_name)
return collection.query(question.text)
except Exception as e:
logger.error(f"Error processing query: {str(e)}")
raise HTTPException(status_code=500, detail=f"Internal server error: {str(e)}")
@app.post("/collections/{collection_name}/upload_files")
async def upload_files(collection_name: str, files: List[UploadFile] = File(...)):
try:
collection = collection_manager.get_collection(collection_name)
return collection.upload_files(files)
except Exception as e:
logger.error(f"Error uploading files: {str(e)}")
raise HTTPException(status_code=500, detail=f"Failed to upload files: {str(e)}")
@app.post("/collections/{collection_name}/update_files")
async def update_files(collection_name: str, files: List[str]):
try:
collection = collection_manager.get_collection(collection_name)
return collection.update_files(files)
except Exception as e:
logger.error(f"Error updating files: {str(e)}")
raise HTTPException(status_code=500, detail=f"Failed to update files: {str(e)}")
@app.get("/collections/{collection_name}/list_documents")
async def list_documents(collection_name: str):
try:
collection = collection_manager.get_collection(collection_name)
return {"documents": collection.list_documents()}
except Exception as e:
logger.error(f"Error listing documents: {str(e)}")
raise HTTPException(
status_code=500, detail=f"Failed to list documents: {str(e)}"
)
@app.delete("/collections/{collection_name}/delete_documents")
async def delete_documents(collection_name: str, delete_request: DeleteDocuments):
try:
collection = collection_manager.get_collection(collection_name)
return collection.delete_documents(delete_request.doc_ids)
except Exception as e:
logger.error(f"Error deleting documents: {str(e)}")
raise HTTPException(
status_code=500, detail=f"Failed to delete documents: {str(e)}"
)
@app.post("/collections")
async def create_collection(request: Request):
data = await request.json()
if "name" not in data:
return jsonify({"error": "Name is required"}), 422
name = data["name"]
if not name:
return jsonify({"error": "Name cannot be empty"}), 422
return collection_manager.create_collection(name)
@app.get("/collections")
async def list_collections():
try:
return list(collection_manager.collections.keys())
except Exception as e:
logger.error(f"Error listing collections: {str(e)}")
raise HTTPException(
status_code=500, detail=f"Failed to list collections: {str(e)}"
)
@app.delete("/collections/{name}")
async def delete_collection(name: str):
return collection_manager.delete_collection(name)
@app.put("/collections/{old_name}")
async def rename_collection(old_name: str, new_name: str = Query(...)):
return collection_manager.rename_collection(old_name, new_name)
@app.get("/", response_class=HTMLResponse)
async def root(request: Request):
with open("static/index.html", "r") as f:
html_content = f.read()
return HTMLResponse(content=html_content)
if __name__ == "__main__":
import uvicorn
collection_manager = CollectionManager()
uvicorn.run(app, host="0.0.0.0", port=8000)