File size: 1,738 Bytes
51cba96 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | import chainlit as cl
from llama_cpp import Llama
# Load the highly compressed 1.5B Qwen model
model_filename = "little_yulya.gguf"
print("Loading Yulya into memory...")
llm = Llama(
model_path=model_filename,
n_ctx=2048,
verbose=False
)
print("Brain loaded successfully!")
@cl.on_chat_start
async def start():
await cl.Message(content="Hello! I am Yulya. [Pose: Smug] Try not to ask stupid questions.").send()
# Initialize the history array for the user session
cl.user_session.set("history", [])
@cl.on_message
async def main(message: cl.Message):
history = cl.user_session.get("history")
# Format the prompt using Qwen's ChatML format
prompt = "<|im_start|>system\nYou are Yulya, a smug, tsundere AI coding assistant.<|im_end|>\n"
# Add history
for user_msg, ai_msg in history:
prompt += f"<|im_start|>user\n{user_msg}<|im_end|>\n"
prompt += f"<|im_start|>assistant\n{ai_msg}<|im_end|>\n"
# Add new user message
prompt += f"<|im_start|>user\n{message.content}<|im_end|>\n<|im_start|>assistant\n"
# Send a "typing..." indicator to the UI
msg = cl.Message(content="")
await msg.send()
# Generate the response via Llama CPP
response = llm(
prompt,
max_tokens=256,
stop=["<|im_end|>", "<|im_start|>"],
echo=False
)
bot_reply = response['choices'][0]['text'].strip()
# Update history (keep last 5 interactions to save context)
history.append((message.content, bot_reply))
if len(history) > 5:
history.pop(0)
cl.user_session.set("history", history)
# Send the final response to the UI
msg.content = bot_reply
await msg.update()
|