# # You can find this code for Chainlit python streaming here (https://docs.chainlit.io/concepts/streaming/python) import chainlit as cl # Define a dictionary to keep track of user states from openai import AsyncOpenAI # importing openai for API usage client = AsyncOpenAI() settings = { "model": "gpt-3.5-turbo", "temperature": 0.7, "max_tokens": 500, "top_p": 1, "frequency_penalty": 0, "presence_penalty": 0, } @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.", icon="/public/idea.svg", ), cl.Starter( label="Explain superconductors", message="Explain superconductors like I'm five years old.", icon="/public/learn.svg", ), 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.", icon="/public/terminal.svg", ), 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.", icon="/public/write.svg", ) ] @cl.on_message async def on_message(message): content=message.content if "morning routine" in content: await cl.Message(content="Sure! Let's start with your current habits. What time do you usually wake up, and what are the first things you do after waking up?").send() elif "superconductors" in content: await cl.Message(content="Superconductors are special materials that can carry electricity without any resistance, like a super-fast slide where nothing slows you down!").send() elif "Python script for daily email reports" in content: await cl.Message(content="To automate sending daily email reports, you can use the smtplib library in Python. Here's a basic example:").send() await cl.Message(content=""" ```python import smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart def send_email(subject, body, to_email): from_email = "your_email@example.com" from_password = "your_password" msg = MIMEMultipart() msg['From'] = from_email msg['To'] = to_email msg['Subject'] = subject msg.attach(MIMEText(body, 'plain')) server = smtplib.SMTP('smtp.example.com', 587) server.starttls() server.login(from_email, from_password) text = msg.as_string() server.sendmail(from_email, to_email, text) server.quit() send_email("Daily Report", "Here is the daily report...", "recipient@example.com") """).send() elif "Text inviting friend to wedding" in content: await cl.Message("Hey! I have a wedding to attend next month and I'd love for you to be my plus-one. No worries if you can't make it, just thought I'd ask!").send() #********************************** prompt_messages = [ {"role": "user", "content": content} ] stream = await client.chat.completions.create( messages=prompt_messages, stream=True, **settings ) msg = cl.Message(content="") async for part in stream: if token := part.choices[0].delta.content: await msg.stream_token(token) # Send the final message await msg.send()