Spaces:
Paused
Paused
Daniel Marques
commited on
Commit
•
b1f4ef7
1
Parent(s):
cad43a2
feat: add message tamplate
Browse files- SOURCE_DOCUMENTS/dataset.txt +0 -0
- main.py +45 -24
SOURCE_DOCUMENTS/dataset.txt
CHANGED
The diff for this file is too large to render.
See raw diff
|
|
main.py
CHANGED
@@ -85,32 +85,13 @@ api_app = FastAPI(title="api app")
|
|
85 |
app.mount("/api", api_app, name="api")
|
86 |
app.mount("/", StaticFiles(directory="static",html = True), name="static")
|
87 |
|
88 |
-
@api_app.post('/predict')
|
89 |
-
async def predict(data: Predict):
|
90 |
-
user_prompt = data.prompt
|
91 |
-
if user_prompt:
|
92 |
-
# print(f'User Prompt: {user_prompt}')
|
93 |
-
# Get the answer from the chain
|
94 |
-
res = QA(user_prompt)
|
95 |
-
answer, docs = res["result"], res["source_documents"]
|
96 |
-
|
97 |
-
prompt_response_dict = {
|
98 |
-
"Prompt": user_prompt,
|
99 |
-
"Answer": answer,
|
100 |
-
}
|
101 |
-
|
102 |
-
prompt_response_dict["Sources"] = []
|
103 |
-
for document in docs:
|
104 |
-
prompt_response_dict["Sources"].append(
|
105 |
-
(os.path.basename(str(document.metadata["source"])), str(document.page_content))
|
106 |
-
)
|
107 |
-
|
108 |
-
return {"response": prompt_response_dict}
|
109 |
-
else:
|
110 |
-
raise HTTPException(status_code=400, detail="Prompt Incorrect")
|
111 |
|
112 |
@api_app.get("/run_ingest")
|
113 |
def run_ingest_route():
|
|
|
|
|
|
|
|
|
114 |
try:
|
115 |
if os.path.exists(PERSIST_DIRECTORY):
|
116 |
try:
|
@@ -121,6 +102,7 @@ def run_ingest_route():
|
|
121 |
raise HTTPException(status_code=500, detail="The directory does not exist")
|
122 |
|
123 |
run_langest_commands = ["python", "ingest.py"]
|
|
|
124 |
if DEVICE_TYPE == "cpu":
|
125 |
run_langest_commands.append("--device_type")
|
126 |
run_langest_commands.append(DEVICE_TYPE)
|
@@ -136,6 +118,7 @@ def run_ingest_route():
|
|
136 |
embedding_function=EMBEDDINGS,
|
137 |
client_settings=CHROMA_SETTINGS,
|
138 |
)
|
|
|
139 |
RETRIEVER = DB.as_retriever()
|
140 |
prompt, memory = get_prompt_template(promptTemplate_type="llama", history=False)
|
141 |
|
@@ -145,15 +128,53 @@ def run_ingest_route():
|
|
145 |
retriever=RETRIEVER,
|
146 |
return_source_documents=SHOW_SOURCES,
|
147 |
chain_type_kwargs={
|
148 |
-
"prompt":
|
149 |
},
|
150 |
)
|
151 |
|
152 |
response = "Script executed successfully: {}".format(result.stdout.decode("utf-8"))
|
|
|
153 |
return {"response": response}
|
154 |
except Exception as e:
|
155 |
raise HTTPException(status_code=500, detail=f"Error occurred: {str(e)}")
|
156 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
157 |
@api_app.post("/save_document/")
|
158 |
async def create_upload_file(file: Union[UploadFile, None] = None):
|
159 |
try:
|
|
|
85 |
app.mount("/api", api_app, name="api")
|
86 |
app.mount("/", StaticFiles(directory="static",html = True), name="static")
|
87 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
88 |
|
89 |
@api_app.get("/run_ingest")
|
90 |
def run_ingest_route():
|
91 |
+
global DB
|
92 |
+
global RETRIEVER
|
93 |
+
global QA
|
94 |
+
|
95 |
try:
|
96 |
if os.path.exists(PERSIST_DIRECTORY):
|
97 |
try:
|
|
|
102 |
raise HTTPException(status_code=500, detail="The directory does not exist")
|
103 |
|
104 |
run_langest_commands = ["python", "ingest.py"]
|
105 |
+
|
106 |
if DEVICE_TYPE == "cpu":
|
107 |
run_langest_commands.append("--device_type")
|
108 |
run_langest_commands.append(DEVICE_TYPE)
|
|
|
118 |
embedding_function=EMBEDDINGS,
|
119 |
client_settings=CHROMA_SETTINGS,
|
120 |
)
|
121 |
+
|
122 |
RETRIEVER = DB.as_retriever()
|
123 |
prompt, memory = get_prompt_template(promptTemplate_type="llama", history=False)
|
124 |
|
|
|
128 |
retriever=RETRIEVER,
|
129 |
return_source_documents=SHOW_SOURCES,
|
130 |
chain_type_kwargs={
|
131 |
+
"prompt": QA_CHAIN_PROMPT,
|
132 |
},
|
133 |
)
|
134 |
|
135 |
response = "Script executed successfully: {}".format(result.stdout.decode("utf-8"))
|
136 |
+
|
137 |
return {"response": response}
|
138 |
except Exception as e:
|
139 |
raise HTTPException(status_code=500, detail=f"Error occurred: {str(e)}")
|
140 |
|
141 |
+
@api_app.get("/api/delete_source", methods=["GET"])
|
142 |
+
def delete_source_route():
|
143 |
+
folder_name = "SOURCE_DOCUMENTS"
|
144 |
+
|
145 |
+
if os.path.exists(folder_name):
|
146 |
+
shutil.rmtree(folder_name)
|
147 |
+
|
148 |
+
os.makedirs(folder_name)
|
149 |
+
|
150 |
+
return jsonify({"message": f"Folder '{folder_name}' successfully deleted and recreated."})
|
151 |
+
|
152 |
+
|
153 |
+
@api_app.post('/predict')
|
154 |
+
async def predict(data: Predict):
|
155 |
+
global QA
|
156 |
+
user_prompt = data.prompt
|
157 |
+
if user_prompt:
|
158 |
+
# print(f'User Prompt: {user_prompt}')
|
159 |
+
# Get the answer from the chain
|
160 |
+
res = QA(user_prompt)
|
161 |
+
answer, docs = res["result"], res["source_documents"]
|
162 |
+
|
163 |
+
prompt_response_dict = {
|
164 |
+
"Prompt": user_prompt,
|
165 |
+
"Answer": answer,
|
166 |
+
}
|
167 |
+
|
168 |
+
prompt_response_dict["Sources"] = []
|
169 |
+
for document in docs:
|
170 |
+
prompt_response_dict["Sources"].append(
|
171 |
+
(os.path.basename(str(document.metadata["source"])), str(document.page_content))
|
172 |
+
)
|
173 |
+
|
174 |
+
return {"response": prompt_response_dict}
|
175 |
+
else:
|
176 |
+
raise HTTPException(status_code=400, detail="Prompt Incorrect")
|
177 |
+
|
178 |
@api_app.post("/save_document/")
|
179 |
async def create_upload_file(file: Union[UploadFile, None] = None):
|
180 |
try:
|