Spaces:
Sleeping
Sleeping
Ilyas KHIAT
commited on
Commit
·
d069333
1
Parent(s):
d40f467
memory up
Browse files
main.py
CHANGED
@@ -13,6 +13,7 @@ import json
|
|
13 |
from prompts import *
|
14 |
from typing import Literal
|
15 |
from models import *
|
|
|
16 |
|
17 |
load_dotenv()
|
18 |
|
@@ -77,6 +78,7 @@ models = ["gpt-4o","gpt-4o-mini","mistral-large-latest"]
|
|
77 |
class UserInput(BaseModel):
|
78 |
prompt: str
|
79 |
enterprise_id: str
|
|
|
80 |
stream: Optional[bool] = False
|
81 |
messages: Optional[list[dict]] = []
|
82 |
style_tonality: Optional[StyleWriter] = None
|
@@ -159,6 +161,17 @@ def get_documents(enterprise_id: str):
|
|
159 |
except Exception as e:
|
160 |
raise HTTPException(status_code=500, detail=f"An error occurred: {str(e)}")
|
161 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
162 |
@app.delete("/documents/{enterprise_id}/{filename_id}")
|
163 |
def delete_document(enterprise_id: str, filename_id: str):
|
164 |
try:
|
@@ -168,6 +181,24 @@ def delete_document(enterprise_id: str, filename_id: str):
|
|
168 |
except Exception as e:
|
169 |
raise HTTPException(status_code=500, detail=f"An error occurred: {str(e)}")
|
170 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
171 |
@app.delete("/documents/all/{enterprise_id}")
|
172 |
def delete_all_documents(enterprise_id: str):
|
173 |
try:
|
@@ -181,13 +212,13 @@ import asyncio
|
|
181 |
|
182 |
GENERATION_TIMEOUT_SEC = 60
|
183 |
|
184 |
-
async def stream_generator(response, prompt):
|
185 |
async with async_timeout.timeout(GENERATION_TIMEOUT_SEC):
|
186 |
try:
|
187 |
async for chunk in response:
|
188 |
if isinstance(chunk, bytes):
|
189 |
chunk = chunk.decode('utf-8') # Convert bytes to str if needed
|
190 |
-
yield json.dumps({"prompt": prompt, "content": chunk})
|
191 |
except asyncio.TimeoutError:
|
192 |
raise HTTPException(status_code=504, detail="Stream timed out")
|
193 |
|
@@ -204,6 +235,11 @@ def generate_answer(user_input: UserInput):
|
|
204 |
context = get_retreive_answer(enterprise_id, prompt, index, common_namespace)
|
205 |
|
206 |
#final_prompt_simplified = prompt_formatting(prompt,template,context)
|
|
|
|
|
|
|
|
|
|
|
207 |
|
208 |
if not context:
|
209 |
context = ""
|
@@ -237,12 +273,13 @@ def generate_answer(user_input: UserInput):
|
|
237 |
index=index)
|
238 |
|
239 |
if user_input.stream:
|
240 |
-
return StreamingResponse(stream_generator(answer,prompt_formated), media_type="application/json")
|
241 |
|
242 |
return {
|
243 |
"prompt": prompt_formated,
|
244 |
"answer": answer,
|
245 |
"context": context,
|
|
|
246 |
}
|
247 |
|
248 |
except Exception as e:
|
|
|
13 |
from prompts import *
|
14 |
from typing import Literal
|
15 |
from models import *
|
16 |
+
from fastapi.middleware.cors import CORSMiddleware
|
17 |
|
18 |
load_dotenv()
|
19 |
|
|
|
78 |
class UserInput(BaseModel):
|
79 |
prompt: str
|
80 |
enterprise_id: str
|
81 |
+
user_id: Optional[str] = None
|
82 |
stream: Optional[bool] = False
|
83 |
messages: Optional[list[dict]] = []
|
84 |
style_tonality: Optional[StyleWriter] = None
|
|
|
161 |
except Exception as e:
|
162 |
raise HTTPException(status_code=500, detail=f"An error occurred: {str(e)}")
|
163 |
|
164 |
+
@app.get("/documents/memory/{enterprise_id}/{user_id}")
|
165 |
+
def get_documents(enterprise_id: str, user_id: str):
|
166 |
+
try:
|
167 |
+
docs_names = []
|
168 |
+
for ids in index.list(prefix=f"kb_{user_id}_", namespace=enterprise_id):
|
169 |
+
for id in ids:
|
170 |
+
docs_names.append(id)
|
171 |
+
return docs_names
|
172 |
+
except Exception as e:
|
173 |
+
raise HTTPException(status_code=500, detail=f"An error occurred: {str(e)}")
|
174 |
+
|
175 |
@app.delete("/documents/{enterprise_id}/{filename_id}")
|
176 |
def delete_document(enterprise_id: str, filename_id: str):
|
177 |
try:
|
|
|
181 |
except Exception as e:
|
182 |
raise HTTPException(status_code=500, detail=f"An error occurred: {str(e)}")
|
183 |
|
184 |
+
@app.delete("/documents/memory/all/{enterprise_id}/{user_id}/")
|
185 |
+
def delete_document(enterprise_id: str, user_id: str):
|
186 |
+
try:
|
187 |
+
for ids in index.list(prefix=f"kb_{user_id}_", namespace=enterprise_id):
|
188 |
+
index.delete(ids=ids, namespace=enterprise_id)
|
189 |
+
return {"message": "Document deleted", "chunks_deleted": ids}
|
190 |
+
except Exception as e:
|
191 |
+
raise HTTPException(status_code=500, detail=f"An error occurred: {str(e)}")
|
192 |
+
|
193 |
+
@app.delete("/documents/memory/{enterprise_id}/{user_id}/{info_id}")
|
194 |
+
def delete_document(enterprise_id: str, user_id: str, info_id: str):
|
195 |
+
try:
|
196 |
+
for ids in index.list(prefix=f"{info_id}", namespace=enterprise_id):
|
197 |
+
index.delete(ids=ids, namespace=enterprise_id)
|
198 |
+
return {"message": "Document deleted", "chunks_deleted": ids}
|
199 |
+
except Exception as e:
|
200 |
+
raise HTTPException(status_code=500, detail=f"An error occurred: {str(e)}")
|
201 |
+
|
202 |
@app.delete("/documents/all/{enterprise_id}")
|
203 |
def delete_all_documents(enterprise_id: str):
|
204 |
try:
|
|
|
212 |
|
213 |
GENERATION_TIMEOUT_SEC = 60
|
214 |
|
215 |
+
async def stream_generator(response, prompt, info_memoire):
|
216 |
async with async_timeout.timeout(GENERATION_TIMEOUT_SEC):
|
217 |
try:
|
218 |
async for chunk in response:
|
219 |
if isinstance(chunk, bytes):
|
220 |
chunk = chunk.decode('utf-8') # Convert bytes to str if needed
|
221 |
+
yield json.dumps({"prompt": prompt, "content": chunk, "info_memoire":info_memoire})
|
222 |
except asyncio.TimeoutError:
|
223 |
raise HTTPException(status_code=504, detail="Stream timed out")
|
224 |
|
|
|
235 |
context = get_retreive_answer(enterprise_id, prompt, index, common_namespace)
|
236 |
|
237 |
#final_prompt_simplified = prompt_formatting(prompt,template,context)
|
238 |
+
infos_added_to_kb = handle_calling_add_to_knowledge_base(prompt,enterprise_id,index,getattr(user_input,"marque",""),user_id=getattr(user_input,"user_id",""))
|
239 |
+
if infos_added_to_kb:
|
240 |
+
prompt = prompt + "l'information a été ajoutée à la base de connaissance: " + infos_added_to_kb['item']
|
241 |
+
else:
|
242 |
+
infos_added_to_kb = {}
|
243 |
|
244 |
if not context:
|
245 |
context = ""
|
|
|
273 |
index=index)
|
274 |
|
275 |
if user_input.stream:
|
276 |
+
return StreamingResponse(stream_generator(answer,prompt_formated,info_memoire), media_type="application/json")
|
277 |
|
278 |
return {
|
279 |
"prompt": prompt_formated,
|
280 |
"answer": answer,
|
281 |
"context": context,
|
282 |
+
"info_memoire": infos_added_to_kb
|
283 |
}
|
284 |
|
285 |
except Exception as e:
|
rag.py
CHANGED
@@ -83,7 +83,7 @@ def get_vectorstore(text_chunks,filename, file_type,namespace,index,enterprise_n
|
|
83 |
return False
|
84 |
|
85 |
|
86 |
-
def add_to_knowledge_base(enterprise_id,information,index,enterprise_name):
|
87 |
''' Add information to the knowledge base
|
88 |
Args:
|
89 |
enterprise_id (str): the enterprise id
|
@@ -96,13 +96,13 @@ def add_to_knowledge_base(enterprise_id,information,index,enterprise_name):
|
|
96 |
|
97 |
document = Document(
|
98 |
page_content=information,
|
99 |
-
metadata={"filename":"knowledge_base","file_type":"text", "filename_id":"knowledge_base", "entreprise_name":enterprise_name},
|
100 |
)
|
101 |
|
102 |
-
uuid = f"
|
103 |
|
104 |
vector_store.add_documents(documents=[document], id=uuid)
|
105 |
-
return
|
106 |
|
107 |
except Exception as e:
|
108 |
print(e)
|
@@ -158,7 +158,7 @@ def get_retreive_answer(enterprise_id,prompt,index,common_id):
|
|
158 |
print(e)
|
159 |
return False
|
160 |
|
161 |
-
def handle_calling_add_to_knowledge_base(query,enterprise_id = "",index = "",enterprise_name = "",llm = None):
|
162 |
''' Handle the calling of the add_to_knowledge_base function
|
163 |
if the user, in his query, wants to add information to the knowledge base, the function will be called
|
164 |
'''
|
@@ -167,7 +167,7 @@ def handle_calling_add_to_knowledge_base(query,enterprise_id = "",index = "",ent
|
|
167 |
|
168 |
Determine if the user wants to add something to the knowledge base.
|
169 |
|
170 |
-
- If the user wants to add something, output 'add' followed by the
|
171 |
- If the user does not want to add something, output 'no action'.
|
172 |
|
173 |
Ensure your response is only 'add <content>' or 'no action'.
|
@@ -180,7 +180,7 @@ def handle_calling_add_to_knowledge_base(query,enterprise_id = "",index = "",ent
|
|
180 |
prompt = PromptTemplate.from_template(template)
|
181 |
|
182 |
if not llm:
|
183 |
-
llm = ChatOpenAI(model="gpt-4o
|
184 |
|
185 |
llm_with_tool = llm.bind_tools([AddToKnowledgeBase])
|
186 |
|
@@ -193,10 +193,12 @@ def handle_calling_add_to_knowledge_base(query,enterprise_id = "",index = "",ent
|
|
193 |
if response.startswith("add"):
|
194 |
item = response[len("add"):].strip()
|
195 |
if item:
|
196 |
-
add_to_knowledge_base(enterprise_id,item,index,enterprise_name)
|
197 |
print("added to knowledge base")
|
198 |
-
|
199 |
-
|
|
|
|
|
200 |
print(response)
|
201 |
return False
|
202 |
|
|
|
83 |
return False
|
84 |
|
85 |
|
86 |
+
def add_to_knowledge_base(enterprise_id,information,index,enterprise_name,user_id=""):
|
87 |
''' Add information to the knowledge base
|
88 |
Args:
|
89 |
enterprise_id (str): the enterprise id
|
|
|
96 |
|
97 |
document = Document(
|
98 |
page_content=information,
|
99 |
+
metadata={"filename":"knowledge_base","file_type":"text", "filename_id":"knowledge_base", "entreprise_name":enterprise_name, "user_id":user_id},
|
100 |
)
|
101 |
|
102 |
+
uuid = f"kb_{user_id}_{uuid4()}"
|
103 |
|
104 |
vector_store.add_documents(documents=[document], id=uuid)
|
105 |
+
return uuid
|
106 |
|
107 |
except Exception as e:
|
108 |
print(e)
|
|
|
158 |
print(e)
|
159 |
return False
|
160 |
|
161 |
+
def handle_calling_add_to_knowledge_base(query,enterprise_id = "",index = "",enterprise_name = "",user_id = "",llm = None):
|
162 |
''' Handle the calling of the add_to_knowledge_base function
|
163 |
if the user, in his query, wants to add information to the knowledge base, the function will be called
|
164 |
'''
|
|
|
167 |
|
168 |
Determine if the user wants to add something to the knowledge base.
|
169 |
|
170 |
+
- If the user wants to add something, extract the valuable information, reformulate and output 'add' followed by the information.
|
171 |
- If the user does not want to add something, output 'no action'.
|
172 |
|
173 |
Ensure your response is only 'add <content>' or 'no action'.
|
|
|
180 |
prompt = PromptTemplate.from_template(template)
|
181 |
|
182 |
if not llm:
|
183 |
+
llm = ChatOpenAI(model="gpt-4o",temperature=0)
|
184 |
|
185 |
llm_with_tool = llm.bind_tools([AddToKnowledgeBase])
|
186 |
|
|
|
193 |
if response.startswith("add"):
|
194 |
item = response[len("add"):].strip()
|
195 |
if item:
|
196 |
+
item_id = add_to_knowledge_base(enterprise_id,item,index,enterprise_name,user_id)
|
197 |
print("added to knowledge base")
|
198 |
+
|
199 |
+
print(item)
|
200 |
+
return {"item_id":item_id,"item":item}
|
201 |
+
|
202 |
print(response)
|
203 |
return False
|
204 |
|