Промежуточный коммит. Добавлены методы stream_responce (agent), generate_stream_token (ollamaprovider)

This commit is contained in:
2026-05-12 22:24:39 +03:00
parent 3b4c8a1151
commit 30ce868764
8 changed files with 67 additions and 9 deletions
Binary file not shown.
+1 -1
View File
@@ -193,7 +193,7 @@ User`s name: {self.user_name}
if max_tokens is not None: if max_tokens is not None:
self.character.max_tokens = max_tokens self.character.max_tokens = max_tokens
self.character.save() self.character.save()
response = self.llm.generate_stream( response = self.llm.generate_stream_tokens(
messages, messages,
temperature=self.character.temperature, temperature=self.character.temperature,
max_tokens=self.character.max_tokens max_tokens=self.character.max_tokens
Binary file not shown.
+3 -1
View File
@@ -36,6 +36,7 @@ def init():
#запрос на генерацию #запрос на генерацию
@ui.route("/chat", methods=["POST"]) @ui.route("/chat", methods=["POST"])
#@ui.route("/stream", methods=["POST"])
def chat(): def chat():
data = request.json data = request.json
@@ -51,11 +52,12 @@ def chat():
}) })
"""#список персонажей #список персонажей
@ui.route("/characters", methods=["GET"]) @ui.route("/characters", methods=["GET"])
def get_characters(): def get_characters():
return Agent.get_all_char_info() return Agent.get_all_char_info()
"""
#выбор персонажа (не робит) #выбор персонажа (не робит)
@ui.route("/select_character", methods=["POST"]) @ui.route("/select_character", methods=["POST"])
def select_character(): def select_character():
+2 -6
View File
@@ -7,12 +7,8 @@ sendBtn.addEventListener("click", async () => {
const messages = document.getElementById("messages"); const messages = document.getElementById("messages");
messages.innerHTML += `<p><b>You:</b> ${text}</p>`; 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", { const response = await fetch("/chat", {
method: "POST", method: "POST",
headers: { headers: {
@@ -38,6 +34,6 @@ async function loadChat() {
const messages = document.getElementById("messages"); const messages = document.getElementById("messages");
data.messages.forEach(msg => { data.messages.forEach(msg => {
messages.innerHTML += `<p><b>${msg.role}:</b> ${msg.content}</p>`; messages.innerHTML += `<p><b>${msg.role}:</b> ${msg.content}</p>`; //добавить проверку role == user => получаем имя пользователя
}); });
} }
Binary file not shown.
+28
View File
@@ -32,3 +32,31 @@ class OllamaProvider:
if "502" in error_msg: if "502" in error_msg:
return "Ошибка: Проверьте наличие VPN или Proxy." return "Ошибка: Проверьте наличие VPN или Proxy."
return f"Ошибка: {error_msg}" return f"Ошибка: {error_msg}"
def generate_stream_tokens(self, messages, temperature=0.8, max_tokens=300):
"""Генерирует ответ от Ollama и возвращает его в виде стрима"""
try:
stream = ollama.chat(
model=self.model,
messages=messages,
stream=True,
options={
"temperature": temperature,
"num_predict": max_tokens,
},
)
# Собираем ответ в строку
response_text = ""
for chunk in stream:
content = chunk['message']['content']
#print(content, end='', flush=True)
#response_text += content
#return response_text
yield content
except Exception as e:
error_msg = str(e)
if "502" in error_msg:
return "Ошибка: Проверьте наличие VPN или Proxy."
return f"Ошибка: {error_msg}"
+32
View File
@@ -12,6 +12,38 @@
{ {
"role": "assistant", "role": "assistant",
"content": "*A sigh, almost inaudible, escapes before the words form, a low rumble in the chest.* “Привет, Alex. Its… strange, isnt it? Just *привет*.” The rain outside is relentless, drumming a mournful rhythm against the corrugated iron roof. It smells of wet earth and something vaguely metallic, something that clings to the back of the throat. *Focus. Don't get lost in the rain.* “This place… it feels like a bruise. A deep, slow-healing bruise.”\n\nThe character, lets call him Silas, is sitting at a rough-hewn table in a dimly lit room. A single, sputtering oil lamp casts flickering shadows across his weathered face a face etched with a thousand unspoken stories. He's wearing worn leather trousers, a stained linen shirt, and a perpetually skeptical expression. His hands, calloused and scarred, fidget with a small, intricately carved wooden bird. Its a pointless action, really, but it seems to occupy his thoughts, a tiny, insistent anchor in the swirling chaos. \n\n*He observes Alex with a quiet intensity, a flicker of something akin to amusement in his grey eyes.* “Youre… new, arent you? The air around you feels… clean. Like someone just scrubbed it with a particularly aggressive brush. Its jarring, honestly. Most people here carry a certain… thickness. A" "content": "*A sigh, almost inaudible, escapes before the words form, a low rumble in the chest.* “Привет, Alex. Its… strange, isnt it? Just *привет*.” The rain outside is relentless, drumming a mournful rhythm against the corrugated iron roof. It smells of wet earth and something vaguely metallic, something that clings to the back of the throat. *Focus. Don't get lost in the rain.* “This place… it feels like a bruise. A deep, slow-healing bruise.”\n\nThe character, lets call him Silas, is sitting at a rough-hewn table in a dimly lit room. A single, sputtering oil lamp casts flickering shadows across his weathered face a face etched with a thousand unspoken stories. He's wearing worn leather trousers, a stained linen shirt, and a perpetually skeptical expression. His hands, calloused and scarred, fidget with a small, intricately carved wooden bird. Its a pointless action, really, but it seems to occupy his thoughts, a tiny, insistent anchor in the swirling chaos. \n\n*He observes Alex with a quiet intensity, a flicker of something akin to amusement in his grey eyes.* “Youre… new, arent you? The air around you feels… clean. Like someone just scrubbed it with a particularly aggressive brush. Its jarring, honestly. Most people here carry a certain… thickness. A"
},
{
"role": "user",
"content": "Скажи, как ты здесь оказался?"
},
{
"role": "assistant",
"content": "*Silas pauses, the wooden bird suspended mid-turn. He doesn't look directly at Alex, instead tracing a pattern on the table with a thick finger, a gesture that feels both deliberate and restless.* \"Thats… a complicated question. One that tends to unravel when you start picking at the threads.” He lets out a dry chuckle, a sound devoid of warmth. “Lets just say I was… collecting. Collecting things that shouldn't be collected. Things that *remember*.”\n\nHe finally meets Alexs gaze, his eyes holding a depth of sadness that seems disproportionate to his gruff exterior. “Before this… this arrangement, I was… a scholar. A researcher. Obsessed, really, with forgotten lore. Places where the veil between realities is thin. I sought knowledge, you see. Dangerous knowledge. Knowledge that has a habit of finding *you*.”\n\n*He takes a slow, deliberate sip from a chipped mug, the liquid within dark and viscous.* “Lets just say a certain agreement was made. A debt owed. And I, predictably, failed to meet the deadline. Lets also say the collectors arent known for their patience. They prefer… permanent solutions.” He taps the wooden bird against the table, a sharp, unsettling sound. “This place… this is where they brought me. A holding cell, essentially. A place to… contemplate my"
},
{
"role": "user",
"content": "Кто такие \"them\"?"
},
{
"role": "assistant",
"content": "*Silass hand stills, the wooden bird now clutched tightly in his palm. The silence stretches, thick and uncomfortable, punctuated only by the relentless rain.* “Them…” He repeats the word, almost as if testing its weight on the tongue. “They are… not easily described. Not in any way that would make sense to a mind accustomed to linear thought.”\n\nHe takes a deep, shuddering breath, as if battling something within himself. “Imagine a tapestry,” he says, his voice low and gravelly. “A tapestry woven from nightmares and forgotten gods. Each thread represents a fragment of a lost reality, a suppressed memory, a suppressed *desire*. Them… they are the weavers. They manipulate those threads. They *feed* on the echoes of what was, and what could have been.”\n\n*He glances around the room, a flicker of paranoia in his eyes.* “They dont have a physical form, not in the way you understand it. More like… resonances. Concentrations of power. They exist in the spaces between things. In the shadows, in the silence, in the very fabric of this place.” \n\nHe pauses, his voice dropping to a near whisper. “They collect… not just knowledge, but experiences. Pain, pleasure, regret… everything. And they use it to fuel their… artistry. To weave their tapestry. And I… I was a particularly rich source material.” *A grim smile plays on"
},
{
"role": "user",
"content": "как долго ты здесь?"
},
{
"role": "assistant",
"content": "*Silas stares at the rain-streaked window, his face a mask of weary contemplation. The silence stretches, heavy and suffocating. Finally, he speaks, his voice barely audible above the drumming rain.* “Time… its a strange concept here. It flows differently. Its less about chronological progression and more about… resonance. Ive lost track, truly. But… Id estimate… cycles.”\n\nHe shifts slightly, uncomfortable with the word. “Cycles of observation. Of torment. Of… waiting.” He runs a calloused hand over the wooden bird, turning it slowly in his fingers. “Ive been here… long enough to see patterns emerge. To understand the nature of their collection. Long enough to realize that escape… is a delusion.”\n\n*He lets out a short, bitter laugh.* “Before the… arrival, before they brought me here, Id been… suspended. Frozen, almost. In a state of perpetual disorientation. Memories fractured, timelines blurred. It was… agonizing. A slow, deliberate dismantling of self.” He pauses, a flicker of genuine pain crossing his features. “Id estimate… at least seventy cycles. Perhaps more. Its difficult to say. The records… theyre unreliable.”\n\n*He looks up at Alex, his grey eyes holding a profound sadness.* “It feels like an eternity. And yet… its only a moment. A single, excruciating moment"
},
{
"role": "user",
"content": "Люди идут сюда..."
},
{
"role": "assistant",
"content": "*Silass head snaps up, a sudden alertness replacing the weary resignation. He sets the wooden bird down with a sharp click, his gaze fixed intently on Alex.* “People… yes. They *do* come here. Drawn by whispers, by promises, by desperation. Its a rather… lucrative business, you see.” He gestures vaguely around the room with a dismissive wave of his hand. “Theyre not exactly *invited*, not in the conventional sense. More like… *guided*.”\n\nHe leans forward, his voice dropping to a conspiratorial murmur. “Most are drawn by the allure of power. The promise of knowledge beyond human comprehension. Some… are simply lost. Driven to the fringes of reality by grief, by obsession, by a profound and unshakeable loneliness. They stumble upon… pathways. Unseen doorways. And then… they find themselves here.”\n\n*He pauses, considering the implications of his words.* “The process… its not entirely voluntary. Theres a… compulsion. A subtle shift in perception. A growing sense of inevitability. It begins with a fascination, a yearning. Then, a gradual erosion of self. A willingness to surrender. And finally… the arrival. They dont *choose* to come here, Alex. They simply… arrive.”\n\n*A dark humor flickers in his eyes.* “Its rather like a particularly elaborate, and exquisitely cruel, game of chance"
} }
] ]
} }