File size: 1,285 Bytes
81a088e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# Import required libraries
import os  # for environment variables
import chainlit as cl  # for chatbot interface
import google.generativeai as genai  # for google genai api
from dotenv import load_dotenv  # for loading environment variables

# Load environment variables from .env file
load_dotenv()

# Get Gemini API key from environment variables
gemini_api_key = os.getenv("GEMINI_API_KEY")

# Configure Gemini API with the API key
genai.configure(api_key=gemini_api_key)

# Initialize Gemini model
model = genai.GenerativeModel(model_name="gemini-2.0-flash")


# chainlit decorator for when a new chat session starts
@cl.on_chat_start
async def handle_chat_start():
    # Send welcome message to user
    await cl.Message(content="Hello! how can I help you today?").send()


# chainlit decorator for when a new message is received
@cl.on_message
async def handle_message(message: cl.Message):
    # Get the message content from user
    prompt = message.content

    # Generate response using Gemini model
    response = model.generate_content(prompt)

    # Extract text from response, or empty string if no text attribute
    response_text = response.text if hasattr(response, "text") else ""

    # Send response back to user
    await cl.Message(content=response_text).send()