Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
104 lines (88 loc) · 3.58 KB
/
Copy pathmain.py
File metadata and controls
104 lines (88 loc) · 3.58 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
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
from typing import List
from fastapi import FastAPI, HTTPException
from fastapi.responses import StreamingResponse
from pydantic import BaseModel
from rag import FileBasedRAG
app = FastAPI()
Rag = FileBasedRAG()
@app.get('/')
def read_root():
return {'message': 'Hello from Rag system'}
class Document(BaseModel):
file_path: str
chunk_size: int = 500
chunk_overlap: int = 50
@app.post('/add_document')
def load_document(DocumentList: Document):
try:
Rag.load_document(DocumentList.file_path, DocumentList.chunk_size, DocumentList.chunk_overlap)
return {'status': 'success', 'message': DocumentList.file_path + " has successfully loaded into the rag"}
except Exception as e:
raise HTTPException(status_code=500, detail=f"Unable to load the file: {e}")
class DirectoryLoad(BaseModel):
directory: str
chunk_size: int = 500
chunk_overlap: int = 50
@app.post('/load_documents_from_directory')
def load_documents_from_directory(DirectoryLoad: DirectoryLoad):
try:
Rag.load_documents_from_directory(DirectoryLoad.directory, DirectoryLoad.chunk_size, DirectoryLoad.chunk_overlap)
return {'status': 'success', 'message': f"Documents from {DirectoryLoad.directory} have been loaded"}
except Exception as e:
raise HTTPException(status_code=500, detail=f"Unable to load documents from directory: {e}")
class AddDocs(BaseModel):
docs: List[str]
@app.post('/add_documents')
def add_documents(AddDocs: AddDocs):
try:
Rag.add_documents(AddDocs.docs)
return {'status': 'success', 'message': f"Added {len(AddDocs.docs)} documents"}
except Exception as e:
raise HTTPException(status_code=500, detail=f"Unable to add documents: {e}")
@app.post('/build_index')
def build_index():
try:
Rag.build_index()
return {'status': 'success', 'message': 'Index built successfully'}
except Exception as e:
raise HTTPException(status_code=500, detail=f"Unable to build index: {e}")
@app.get('/get_document_count')
def get_document_count():
try:
count = Rag.get_document_count()
return {'status': 'success', 'document_count': count}
except Exception as e:
raise HTTPException(status_code=500, detail=f"Unable to get document count: {e}")
@app.post('/clear_documents')
def clear_documents():
try:
Rag.clear_documents()
return {'status': 'success', 'message': 'All documents cleared'}
except Exception as e:
raise HTTPException(status_code=500, detail=f"Unable to clear documents: {e}")
@app.post('/clear_conversation')
def clear_conversation():
try:
Rag.clear_conversation()
return {'status': 'success', 'message': 'Conversation history cleared'}
except Exception as e:
raise HTTPException(status_code=500, detail=f"Unable to clear conversation: {e}")
class SetPrompt(BaseModel):
prompt: str
@app.post('/set_system_prompt')
def set_system_prompt(SetPrompt: SetPrompt):
try:
Rag.set_system_prompt(SetPrompt.prompt)
return {'status': 'success', 'message': 'System prompt updated'}
except Exception as e:
raise HTTPException(status_code=500, detail=f"Unable to set system prompt: {e}")
class Question(BaseModel):
question: str
top_k: int = 3
@app.post("/query/")
def query_model(Question: Question):
try:
print(Question.question)
return StreamingResponse(Rag.query(question=Question.question, top_k=Question.top_k), media_type="text/plain")
except Exception as e:
raise HTTPException(status_code=500, detail=f'Error occurred while querying the request: {e}')