Исправлен баг с сохранением истории чата
This commit is contained in:
Binary file not shown.
+12
-5
@@ -97,17 +97,15 @@ User`s name: {self.user_name}
|
|||||||
|
|
||||||
summary = self.llm.generate_stream(messages)
|
summary = self.llm.generate_stream(messages)
|
||||||
|
|
||||||
# сохраняем summary
|
|
||||||
self.summary = summary
|
self.summary = summary
|
||||||
|
|
||||||
# 🔥 очищаем историю (оставляем последние 2-4 сообщения)
|
|
||||||
try:
|
try:
|
||||||
self.chat_history = self.chat_history[-2:]
|
self.chat_history = self.chat_history[-2:]
|
||||||
except IndexError:
|
except IndexError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def save_memory(self):
|
def save_memory(self):
|
||||||
# Используем абсолютный путь от корня проекта
|
# абсолютный путь от корня проекта
|
||||||
project_root = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
project_root = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||||
memory_dir = os.path.join(project_root, "data", "memory")
|
memory_dir = os.path.join(project_root, "data", "memory")
|
||||||
os.makedirs(memory_dir, exist_ok=True)
|
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)
|
json.dump(data, f, ensure_ascii=False, indent=2)
|
||||||
|
|
||||||
def load_memory(self):
|
def load_memory(self):
|
||||||
# Используем абсолютный путь от корня проекта
|
# абсолютный путь от корня проекта
|
||||||
project_root = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
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")
|
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 нужно будет создать новый метод чисто на стриминг, пока что закомментил строки с цельным ответом
|
def stream_responce(self, user_input, temperature=None, max_tokens=None): #В ollamaapi нужно будет создать новый метод чисто на стриминг, пока что закомментил строки с цельным ответом
|
||||||
messages = self.build_messages(user_input)
|
messages = self.build_messages(user_input)
|
||||||
|
full_response = ""
|
||||||
if temperature is not None:
|
if temperature is not None:
|
||||||
self.character.temperature = temperature
|
self.character.temperature = temperature
|
||||||
if max_tokens is not None:
|
if max_tokens is not None:
|
||||||
@@ -199,6 +198,12 @@ User`s name: {self.user_name}
|
|||||||
max_tokens=self.character.max_tokens
|
max_tokens=self.character.max_tokens
|
||||||
)
|
)
|
||||||
|
|
||||||
|
for token in response:
|
||||||
|
|
||||||
|
full_response += token
|
||||||
|
|
||||||
|
yield token
|
||||||
|
|
||||||
# сохраняем историю
|
# сохраняем историю
|
||||||
self.chat_history.append({
|
self.chat_history.append({
|
||||||
"role": "user",
|
"role": "user",
|
||||||
@@ -207,11 +212,13 @@ User`s name: {self.user_name}
|
|||||||
|
|
||||||
self.chat_history.append({
|
self.chat_history.append({
|
||||||
"role": "assistant",
|
"role": "assistant",
|
||||||
"content": response
|
"content": full_response
|
||||||
})
|
})
|
||||||
|
|
||||||
if len(self.chat_history) > 20:
|
if len(self.chat_history) > 20:
|
||||||
self.summarize_history()
|
self.summarize_history()
|
||||||
|
|
||||||
|
self.save_memory()
|
||||||
|
|
||||||
return response
|
return response
|
||||||
|
|
||||||
|
|||||||
Binary file not shown.
@@ -49,9 +49,10 @@ def stream():
|
|||||||
def generate():
|
def generate():
|
||||||
for content in agent.stream_responce(last_prompt):
|
for content in agent.stream_responce(last_prompt):
|
||||||
yield f"data: {content}\n\n"
|
yield f"data: {content}\n\n"
|
||||||
agent.save_memory()
|
|
||||||
return Response(generate(), mimetype="text/event-stream")
|
return Response(generate(), mimetype="text/event-stream")
|
||||||
|
|
||||||
|
|
||||||
#список персонажей
|
#список персонажей
|
||||||
@ui.route("/characters", methods=["GET"])
|
@ui.route("/characters", methods=["GET"])
|
||||||
def get_characters():
|
def get_characters():
|
||||||
|
|||||||
Reference in New Issue
Block a user