forked from AI_team/Philosophy-RAG-demo
Uses environment variables for suggestions (chainlit starters).
This commit is contained in:
parent
32c8774a0a
commit
9a05fd62f8
@ -1,5 +1,7 @@
|
||||
import argparse
|
||||
import json
|
||||
import logging
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
||||
import chainlit as cl
|
||||
@ -62,15 +64,11 @@ def generate(state: State):
|
||||
|
||||
@cl.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)
|
||||
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,
|
||||
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)
|
||||
|
||||
_ = vector_store.add_documents(documents=pdf_splits + web_splits)
|
||||
@ -86,31 +84,31 @@ async def on_chat_start():
|
||||
|
||||
cl.user_session.set("graph", graph)
|
||||
|
||||
await cl.Message(content="Ready for chatting!").send()
|
||||
|
||||
|
||||
@cl.on_message
|
||||
async def on_message(message: cl.Message):
|
||||
graph = cl.user_session.get("graph")
|
||||
response = graph.invoke({"question": message.content})
|
||||
# Send the final answer.
|
||||
await cl.Message(content=response).send()
|
||||
|
||||
|
||||
@cl.set_starters
|
||||
async def set_starters():
|
||||
return [cl.Starter(label="Morning routine ideation",
|
||||
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 "
|
||||
"activities energize me in the morning.", ),
|
||||
cl.Starter(label="Explain superconductors",
|
||||
message="Explain superconductors like I'm five years old.", ),
|
||||
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 "
|
||||
"how I would set it up.", ),
|
||||
cl.Starter(label="Text inviting friend to wedding",
|
||||
message="Write a text asking a friend to be my plus-one at a wedding next month. I want to keep "
|
||||
"it super short and casual, and offer an out.", )]
|
||||
chainlit_starters = os.environ["CHAINLIT_STARTERS"]
|
||||
|
||||
if chainlit_starters is None:
|
||||
return
|
||||
|
||||
dict_list = json.loads(chainlit_starters)
|
||||
|
||||
starters = []
|
||||
for starter in dict_list:
|
||||
try:
|
||||
starters.append(cl.Starter(label=starter["label"], message=starter["message"]))
|
||||
except KeyError:
|
||||
logging.warning("CHAINLIT_STARTERS environment is not a list with "
|
||||
"dictionaries containing 'label' and 'message' keys.")
|
||||
|
||||
return starters
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Loading…
Reference in New Issue
Block a user