Code_LLM / src /ui /callbacks.py
AnatoliiG
arch restruct
f9aca5d
raw
history blame
900 Bytes
import gradio as gr
from src.core.engine import engine
from src.utils.helpers import get_clean_text
def bot_response(history, system_prompt, temperature, max_tokens):
messages = [{"role": "system", "content": system_prompt}]
for msg in history[-7:]:
messages.append(
{"role": msg["role"], "content": get_clean_text(msg["content"])}
)
history.append({"role": "assistant", "content": ""})
try:
stream = engine.generate_stream(messages, max_tokens, temperature)
partial_text = ""
for chunk in stream:
delta = chunk["choices"][0]["delta"]
if "content" in delta:
partial_text += delta["content"]
history[-1]["content"] = partial_text
yield history
except Exception as e:
history[-1]["content"] += f"\n\n❌ Error: {str(e)}"
yield history