diff --git a/core/agent/__pycache__/agent.cpython-310.pyc b/core/agent/__pycache__/agent.cpython-310.pyc index e006a52..7206d64 100644 Binary files a/core/agent/__pycache__/agent.cpython-310.pyc and b/core/agent/__pycache__/agent.cpython-310.pyc differ diff --git a/core/agent/agent.py b/core/agent/agent.py index c519050..4367d0e 100644 --- a/core/agent/agent.py +++ b/core/agent/agent.py @@ -97,17 +97,15 @@ User`s name: {self.user_name} summary = self.llm.generate_stream(messages) - # сохраняем summary self.summary = summary - # 🔥 очищаем историю (оставляем последние 2-4 сообщения) try: self.chat_history = self.chat_history[-2:] except IndexError: pass def save_memory(self): - # Используем абсолютный путь от корня проекта + # абсолютный путь от корня проекта project_root = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) memory_dir = os.path.join(project_root, "data", "memory") os.makedirs(memory_dir, exist_ok=True) @@ -123,7 +121,7 @@ User`s name: {self.user_name} json.dump(data, f, ensure_ascii=False, indent=2) def load_memory(self): - # Используем абсолютный путь от корня проекта + # абсолютный путь от корня проекта project_root = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) path = os.path.join(project_root, "data", "memory", f"{self.character.id}.json") @@ -188,6 +186,7 @@ User`s name: {self.user_name} def stream_responce(self, user_input, temperature=None, max_tokens=None): #В ollamaapi нужно будет создать новый метод чисто на стриминг, пока что закомментил строки с цельным ответом messages = self.build_messages(user_input) + full_response = "" if temperature is not None: self.character.temperature = temperature if max_tokens is not None: @@ -199,6 +198,12 @@ User`s name: {self.user_name} max_tokens=self.character.max_tokens ) + for token in response: + + full_response += token + + yield token + # сохраняем историю self.chat_history.append({ "role": "user", @@ -207,11 +212,13 @@ User`s name: {self.user_name} self.chat_history.append({ "role": "assistant", - "content": response + "content": full_response }) if len(self.chat_history) > 20: self.summarize_history() + + self.save_memory() return response diff --git a/core/flaskui/__pycache__/flask.cpython-310.pyc b/core/flaskui/__pycache__/flask.cpython-310.pyc index dafa56c..ae67c2f 100644 Binary files a/core/flaskui/__pycache__/flask.cpython-310.pyc and b/core/flaskui/__pycache__/flask.cpython-310.pyc differ diff --git a/core/flaskui/flask.py b/core/flaskui/flask.py index b9434b4..f1a6abf 100644 --- a/core/flaskui/flask.py +++ b/core/flaskui/flask.py @@ -49,9 +49,10 @@ def stream(): def generate(): for content in agent.stream_responce(last_prompt): yield f"data: {content}\n\n" - agent.save_memory() + return Response(generate(), mimetype="text/event-stream") + #список персонажей @ui.route("/characters", methods=["GET"]) def get_characters():