KevinHuSh
commited on
Commit
·
09e1055
1
Parent(s):
5707f56
fix stream chat for ollama (#816)
Browse files### What problem does this PR solve?
#709
### Type of change
- [x] Bug Fix (non-breaking change which fixes an issue)
- api/apps/api_app.py +44 -3
- rag/llm/chat_model.py +2 -2
api/apps/api_app.py
CHANGED
@@ -21,12 +21,15 @@ from flask import request, Response
|
|
21 |
from flask_login import login_required, current_user
|
22 |
|
23 |
from api.db import FileType, ParserType
|
24 |
-
from api.db.db_models import APIToken, API4Conversation
|
25 |
from api.db.services import duplicate_name
|
26 |
from api.db.services.api_service import APITokenService, API4ConversationService
|
27 |
from api.db.services.dialog_service import DialogService, chat
|
28 |
from api.db.services.document_service import DocumentService
|
|
|
|
|
29 |
from api.db.services.knowledgebase_service import KnowledgebaseService
|
|
|
30 |
from api.db.services.user_service import UserTenantService
|
31 |
from api.settings import RetCode
|
32 |
from api.utils import get_uuid, current_timestamp, datetime_format
|
@@ -267,6 +270,13 @@ def upload():
|
|
267 |
if file.filename == '':
|
268 |
return get_json_result(
|
269 |
data=False, retmsg='No file selected!', retcode=RetCode.ARGUMENT_ERROR)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
270 |
try:
|
271 |
if DocumentService.get_doc_count(kb.tenant_id) >= int(os.environ.get('MAX_FILE_NUM_PER_USER', 8192)):
|
272 |
return get_data_error_result(
|
@@ -298,11 +308,42 @@ def upload():
|
|
298 |
"size": len(blob),
|
299 |
"thumbnail": thumbnail(filename, blob)
|
300 |
}
|
|
|
|
|
|
|
|
|
|
|
301 |
if doc["type"] == FileType.VISUAL:
|
302 |
doc["parser_id"] = ParserType.PICTURE.value
|
303 |
if re.search(r"\.(ppt|pptx|pages)$", filename):
|
304 |
doc["parser_id"] = ParserType.PRESENTATION.value
|
305 |
-
|
306 |
-
|
|
|
307 |
except Exception as e:
|
308 |
return server_error_response(e)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
from flask_login import login_required, current_user
|
22 |
|
23 |
from api.db import FileType, ParserType
|
24 |
+
from api.db.db_models import APIToken, API4Conversation, Task
|
25 |
from api.db.services import duplicate_name
|
26 |
from api.db.services.api_service import APITokenService, API4ConversationService
|
27 |
from api.db.services.dialog_service import DialogService, chat
|
28 |
from api.db.services.document_service import DocumentService
|
29 |
+
from api.db.services.file2document_service import File2DocumentService
|
30 |
+
from api.db.services.file_service import FileService
|
31 |
from api.db.services.knowledgebase_service import KnowledgebaseService
|
32 |
+
from api.db.services.task_service import queue_tasks, TaskService
|
33 |
from api.db.services.user_service import UserTenantService
|
34 |
from api.settings import RetCode
|
35 |
from api.utils import get_uuid, current_timestamp, datetime_format
|
|
|
270 |
if file.filename == '':
|
271 |
return get_json_result(
|
272 |
data=False, retmsg='No file selected!', retcode=RetCode.ARGUMENT_ERROR)
|
273 |
+
|
274 |
+
root_folder = FileService.get_root_folder(tenant_id)
|
275 |
+
pf_id = root_folder["id"]
|
276 |
+
FileService.init_knowledgebase_docs(pf_id, tenant_id)
|
277 |
+
kb_root_folder = FileService.get_kb_folder(tenant_id)
|
278 |
+
kb_folder = FileService.new_a_file_from_kb(kb.tenant_id, kb.name, kb_root_folder["id"])
|
279 |
+
|
280 |
try:
|
281 |
if DocumentService.get_doc_count(kb.tenant_id) >= int(os.environ.get('MAX_FILE_NUM_PER_USER', 8192)):
|
282 |
return get_data_error_result(
|
|
|
308 |
"size": len(blob),
|
309 |
"thumbnail": thumbnail(filename, blob)
|
310 |
}
|
311 |
+
|
312 |
+
form_data=request.form
|
313 |
+
if "parser_id" in form_data.keys():
|
314 |
+
if request.form.get("parser_id").strip() in list(vars(ParserType).values())[1:-3]:
|
315 |
+
doc["parser_id"] = request.form.get("parser_id").strip()
|
316 |
if doc["type"] == FileType.VISUAL:
|
317 |
doc["parser_id"] = ParserType.PICTURE.value
|
318 |
if re.search(r"\.(ppt|pptx|pages)$", filename):
|
319 |
doc["parser_id"] = ParserType.PRESENTATION.value
|
320 |
+
|
321 |
+
doc_result = DocumentService.insert(doc)
|
322 |
+
FileService.add_file_from_kb(doc, kb_folder["id"], kb.tenant_id)
|
323 |
except Exception as e:
|
324 |
return server_error_response(e)
|
325 |
+
|
326 |
+
if "run" in form_data.keys():
|
327 |
+
if request.form.get("run").strip() == "1":
|
328 |
+
try:
|
329 |
+
info = {"run": 1, "progress": 0}
|
330 |
+
info["progress_msg"] = ""
|
331 |
+
info["chunk_num"] = 0
|
332 |
+
info["token_num"] = 0
|
333 |
+
DocumentService.update_by_id(doc["id"], info)
|
334 |
+
# if str(req["run"]) == TaskStatus.CANCEL.value:
|
335 |
+
tenant_id = DocumentService.get_tenant_id(doc["id"])
|
336 |
+
if not tenant_id:
|
337 |
+
return get_data_error_result(retmsg="Tenant not found!")
|
338 |
+
|
339 |
+
#e, doc = DocumentService.get_by_id(doc["id"])
|
340 |
+
TaskService.filter_delete([Task.doc_id == doc["id"]])
|
341 |
+
e, doc = DocumentService.get_by_id(doc["id"])
|
342 |
+
doc = doc.to_dict()
|
343 |
+
doc["tenant_id"] = tenant_id
|
344 |
+
bucket, name = File2DocumentService.get_minio_address(doc_id=doc["id"])
|
345 |
+
queue_tasks(doc, bucket, name)
|
346 |
+
except Exception as e:
|
347 |
+
return server_error_response(e)
|
348 |
+
|
349 |
+
return get_json_result(data=doc_result.to_json())
|
rag/llm/chat_model.py
CHANGED
@@ -248,8 +248,8 @@ class OllamaChat(Base):
|
|
248 |
)
|
249 |
for resp in response:
|
250 |
if resp["done"]:
|
251 |
-
|
252 |
-
ans
|
253 |
yield ans
|
254 |
except Exception as e:
|
255 |
yield ans + "\n**ERROR**: " + str(e)
|
|
|
248 |
)
|
249 |
for resp in response:
|
250 |
if resp["done"]:
|
251 |
+
yield resp.get("prompt_eval_count", 0) + resp.get("eval_count", 0)
|
252 |
+
ans += resp["message"]["content"]
|
253 |
yield ans
|
254 |
except Exception as e:
|
255 |
yield ans + "\n**ERROR**: " + str(e)
|