Skip to content
This repository was archived by the owner on Apr 22, 2025. It is now read-only.
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 17 additions & 14 deletions src/app.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
import chainlit as cl
from config import (
get_content_for_starter,
get_model_for_profile,
prompt_dict,
CHAT_SETTINGS,
LLM,
)
from config import (get_content_for_starter,
get_model_for_profile,
prompt_dict,
CHAT_SETTINGS,
LLM)
from utils.logger import logger
from chainlit.config import config

# setting default tag and history
@cl.on_chat_start
def start_chat():
cl.user_session.set("is_new_session", True)

# Construct a new conversation setting system prompt.
async def handle_new_session(user_input: str):
matched = False
Expand All @@ -23,28 +21,33 @@ async def handle_new_session(user_input: str):
content = get_content_for_starter(starter.label)
cl.user_session.set("message_history", [{"role": "system", "content": content}])
break

if not matched:
default_prompt = next(iter(prompt_dict.values()), "Can I help you?")
cl.user_session.set("message_history", [{"role": "system", "content": default_prompt}])

cl.user_session.set("is_new_session", False)

# main logic ,receive message will call this function
@cl.on_message
async def main(message: cl.Message):
is_new_session = cl.user_session.get("is_new_session", False)

# Get and update message history
message_history = cl.user_session.get("message_history")


# If it is a new session, get prompt
if is_new_session:
if message_history is None or is_new_session:
await handle_new_session(message.content)
message_history = cl.user_session.get("message_history")
logger.info("Start a new session or initialize if not yet.")

# Get and update message history
message_history = cl.user_session.get("message_history")

# Append user_message to history
message_history.append({"role": "user", "content": message.content})
logger.info(f"Message History: {message_history}")

# ready stream message
msg = cl.Message(content="")
await msg.send()

Expand Down