AnatoliiG commited on
Commit
2b63295
·
1 Parent(s): 9700948
Files changed (2) hide show
  1. src/core/engine.py +39 -22
  2. src/ui/components.py +1 -3
src/core/engine.py CHANGED
@@ -59,32 +59,49 @@ class ModelEngine:
59
  if kwargs.get("tool_choice"):
60
  llama_kwargs["tool_choice"] = kwargs["tool_choice"]
61
 
62
- acquired = False
63
- while not acquired:
64
- if abort_event and abort_event.is_set():
65
- print("Request aborted while waiting in queue.")
66
- return
67
- acquired = self.lock.acquire(timeout=0.5)
68
 
69
- try:
70
- if abort_event and abort_event.is_set():
71
- return
72
-
73
- response = self.llm.create_chat_completion(**llama_kwargs)
 
 
74
 
75
- # Если просят стрим - отдаем генератор
76
- if stream_mode:
77
- for chunk in response:
78
  if abort_event and abort_event.is_set():
79
- print("Request aborted during generation.")
80
- break
81
- yield chunk
82
- # Если просят целый ответ (Агенты) - отдаем сразу словарь
83
- else:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84
  return response
85
-
86
- finally:
87
- self.lock.release()
88
 
89
 
90
  engine = ModelEngine()
 
59
  if kwargs.get("tool_choice"):
60
  llama_kwargs["tool_choice"] = kwargs["tool_choice"]
61
 
62
+ # Если включен стриминг, выносим yield во внутреннюю функцию-генератор
63
+ if stream_mode:
 
 
 
 
64
 
65
+ def stream_generator():
66
+ acquired = False
67
+ while not acquired:
68
+ if abort_event and abort_event.is_set():
69
+ print("Request aborted while waiting in queue.")
70
+ return
71
+ acquired = self.lock.acquire(timeout=0.5)
72
 
73
+ try:
 
 
74
  if abort_event and abort_event.is_set():
75
+ return
76
+
77
+ response = self.llm.create_chat_completion(**llama_kwargs)
78
+ for chunk in response:
79
+ if abort_event and abort_event.is_set():
80
+ print("Request aborted during generation.")
81
+ break
82
+ yield chunk
83
+ finally:
84
+ self.lock.release()
85
+
86
+ return stream_generator()
87
+
88
+ # Если стриминг выключен (режим Агента), возвращаем обычный словарь
89
+ else:
90
+ acquired = False
91
+ while not acquired:
92
+ if abort_event and abort_event.is_set():
93
+ print("Request aborted while waiting in queue.")
94
+ return
95
+ acquired = self.lock.acquire(timeout=0.5)
96
+
97
+ try:
98
+ if abort_event and abort_event.is_set():
99
+ return
100
+
101
+ response = self.llm.create_chat_completion(**llama_kwargs)
102
  return response
103
+ finally:
104
+ self.lock.release()
 
105
 
106
 
107
  engine = ModelEngine()
src/ui/components.py CHANGED
@@ -49,9 +49,7 @@ def create_ui():
49
 
50
  # Основная область чата
51
  with gr.Column(elem_id="col-chat-main"):
52
- chatbot = gr.Chatbot(
53
- elem_id="chatbot", show_label=False, bubble_full_width=False
54
- )
55
 
56
  with gr.Row(elem_id="input-area"):
57
  msg = gr.Textbox(
 
49
 
50
  # Основная область чата
51
  with gr.Column(elem_id="col-chat-main"):
52
+ chatbot = gr.Chatbot(elem_id="chatbot", show_label=False)
 
 
53
 
54
  with gr.Row(elem_id="input-area"):
55
  msg = gr.Textbox(