73 lines
1.7 KiB
Python
73 lines
1.7 KiB
Python
from flask import Flask, Response, render_template, request, jsonify
|
|
from core.agent.agent import Agent
|
|
from core.llm.ollamaapi import OllamaProvider
|
|
from core.character.character import Character
|
|
|
|
ui = Flask(__name__)
|
|
|
|
#блок инициализации объектов
|
|
|
|
llm = OllamaProvider("gemma3:4b")
|
|
|
|
character = Character.load("test_char_001")
|
|
|
|
agent = Agent(character, llm, user_name="Alex")
|
|
|
|
agent.load_memory()
|
|
agent.ensure_first_message()
|
|
|
|
#база
|
|
@ui.route("/")
|
|
@ui.route("/index")
|
|
def index():
|
|
return render_template('index.html')
|
|
|
|
#история чата
|
|
@ui.route("/init", methods=["GET"])
|
|
def init():
|
|
return jsonify({
|
|
"messages": agent.chat_history,
|
|
"user_name": agent.user_name,
|
|
"character": {
|
|
"name": agent.character.name,
|
|
"avatar": agent.character.avatar_path
|
|
}
|
|
})
|
|
|
|
#запрос на генерацию
|
|
@ui.route("/chat", methods=["POST"])
|
|
#@ui.route("/stream", methods=["POST"])
|
|
def chat():
|
|
|
|
global last_prompt
|
|
last_prompt = request.json["message"]
|
|
return jsonify({"status": "ok"})
|
|
|
|
@ui.route("/stream")
|
|
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():
|
|
return Agent.get_all_char_info()
|
|
|
|
"""
|
|
#выбор персонажа (не робит)
|
|
@ui.route("/select_character", methods=["POST"])
|
|
def select_character():
|
|
pass
|
|
'''data = request.json
|
|
char_id = data["id"]
|
|
|
|
global agent
|
|
agent = Agent(characters[char_id], llm)
|
|
|
|
return jsonify({"status": "ok"})'''"""
|
|
|