Uses environment variables for suggestions (chainlit starters).

This commit is contained in:
Nielson Janné 2025-03-12 14:45:51 +01:00
parent 32c8774a0a
commit 9a05fd62f8

View File

@ -1,5 +1,7 @@
import argparse import argparse
import json
import logging import logging
import os
from pathlib import Path from pathlib import Path
import chainlit as cl import chainlit as cl
@ -62,15 +64,11 @@ def generate(state: State):
@cl.on_chat_start @cl.on_chat_start
async def on_chat_start(): async def on_chat_start():
await cl.Message(author="System", content="Starting up application").send()
embedding = get_embedding_model(args.back_end) embedding = get_embedding_model(args.back_end)
vector_store = InMemoryVectorStore(embedding) vector_store = InMemoryVectorStore(embedding)
await cl.Message(author="System", content="Processing PDF files.").send()
pdf_splits = await cl.make_async(process_local_files)(args.pdf_data, args.pdf_chunk_size, pdf_splits = await cl.make_async(process_local_files)(args.pdf_data, args.pdf_chunk_size,
args.pdf_chunk_overlap, args.pdf_add_start_index) args.pdf_chunk_overlap, args.pdf_add_start_index)
await cl.Message(author="System", content="Processing web sites.").send()
web_splits = await cl.make_async(process_web_sites)(args.web_data, args.web_chunk_size) web_splits = await cl.make_async(process_web_sites)(args.web_data, args.web_chunk_size)
_ = vector_store.add_documents(documents=pdf_splits + web_splits) _ = vector_store.add_documents(documents=pdf_splits + web_splits)
@ -86,31 +84,31 @@ async def on_chat_start():
cl.user_session.set("graph", graph) cl.user_session.set("graph", graph)
await cl.Message(content="Ready for chatting!").send()
@cl.on_message @cl.on_message
async def on_message(message: cl.Message): async def on_message(message: cl.Message):
graph = cl.user_session.get("graph") graph = cl.user_session.get("graph")
response = graph.invoke({"question": message.content}) response = graph.invoke({"question": message.content})
# Send the final answer.
await cl.Message(content=response).send() await cl.Message(content=response).send()
@cl.set_starters @cl.set_starters
async def set_starters(): async def set_starters():
return [cl.Starter(label="Morning routine ideation", chainlit_starters = os.environ["CHAINLIT_STARTERS"]
message="Can you help me create a personalized morning routine that would help increase my "
"productivity throughout the day? Start by asking me about my current habits and what " if chainlit_starters is None:
"activities energize me in the morning.", ), return
cl.Starter(label="Explain superconductors",
message="Explain superconductors like I'm five years old.", ), dict_list = json.loads(chainlit_starters)
cl.Starter(label="Python script for daily email reports",
message="Write a script to automate sending daily email reports in Python, and walk me through " starters = []
"how I would set it up.", ), for starter in dict_list:
cl.Starter(label="Text inviting friend to wedding", try:
message="Write a text asking a friend to be my plus-one at a wedding next month. I want to keep " starters.append(cl.Starter(label=starter["label"], message=starter["message"]))
"it super short and casual, and offer an out.", )] except KeyError:
logging.warning("CHAINLIT_STARTERS environment is not a list with "
"dictionaries containing 'label' and 'message' keys.")
return starters
if __name__ == "__main__": if __name__ == "__main__":