chatbot / app.py
syedfarith's picture
Upload 3 files
b3e77db verified
raw
history blame contribute delete
No virus
2.52 kB
import chainlit as cl
from groq import Groq
from langdetect import detect
from deep_translator import GoogleTranslator
# Initialize the Groq client
client = Groq(api_key="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")
LITERAL_API_KEY="lsk_A8YDyASs7LnDbfIoACnbXAgkSL0i2lC27htdkXUD0k"
import chainlit as cl
@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.",
)
]
@cl.on_message
async def main(message: cl.Message):
# Detect the language of the input message
detected_language = detect(message.content)
# If the detected language is not English, translate the message to English
if detected_language != "en":
input_text = GoogleTranslator(source=detected_language, target="en").translate(message.content)
else:
input_text = message.content
# Create a chat completion request
chat_completion = client.chat.completions.create(
messages=[
{
"role": "user",
"content": input_text,
}
],
model="llama3-8b-8192",
)
# Get the response from the model
response_text = chat_completion.choices[0].message.content
# If the input was translated to English, translate the response back to the detected language
if detected_language != "en":
response_text = GoogleTranslator(source="en", target=detected_language).translate(response_text)
# Send the response back to the user
await cl.Message(
content=response_text
).send()