Edited: 1. agent.py - добавление базового функционала метода stream_responce. 2. Добавлены/отредактированы chat.js, style.css. 3. flask.py добавлен блок инициализации объектов OllamaProvider, Character, Agent
This commit is contained in:
Binary file not shown.
+61
-4
@@ -1,13 +1,70 @@
|
||||
from flask import *
|
||||
from flask import Flask, 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("/chats")
|
||||
def chats():
|
||||
return "<h1>Страница чатов</h1>"
|
||||
#история чата
|
||||
@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"])
|
||||
def chat():
|
||||
|
||||
data = request.json
|
||||
|
||||
user_message = data["message"]
|
||||
|
||||
response = agent.respond(user_message)
|
||||
|
||||
agent.save_memory()
|
||||
|
||||
return jsonify({
|
||||
"response": response
|
||||
})
|
||||
|
||||
|
||||
"""#список персонажей
|
||||
@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"})'''"""
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
const sendBtn = document.getElementById("send-btn");
|
||||
|
||||
loadChat();
|
||||
sendBtn.addEventListener("click", async () => {
|
||||
|
||||
const input = document.getElementById("user-input");
|
||||
@@ -8,7 +8,11 @@ sendBtn.addEventListener("click", async () => {
|
||||
const messages = document.getElementById("messages");
|
||||
|
||||
messages.innerHTML += `<p><b>You:</b> ${text}</p>`;
|
||||
const source = new EventSource("/stream");
|
||||
|
||||
source.onmessage = function(event) {
|
||||
messages.innerHTML += event.data;
|
||||
};
|
||||
const response = await fetch("/chat", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
@@ -24,4 +28,16 @@ sendBtn.addEventListener("click", async () => {
|
||||
messages.innerHTML += `<p><b>AI:</b> ${data.response}</p>`;
|
||||
|
||||
input.value = "";
|
||||
});
|
||||
});
|
||||
|
||||
async function loadChat() {
|
||||
|
||||
const res = await fetch("/init");
|
||||
const data = await res.json();
|
||||
|
||||
const messages = document.getElementById("messages");
|
||||
|
||||
data.messages.forEach(msg => {
|
||||
messages.innerHTML += `<p><b>${msg.role}:</b> ${msg.content}</p>`;
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user