8
8
# Configure the API key for the Gemini model
9
9
api_key = os .environ .get ("GEMINI_API_KEY" )
10
10
if not api_key :
11
- raise ValueError ("API key not found! Ensure GEMINI_API_KEY is set in the environment variables." )
11
+ raise ValueError (
12
+ "API key not found! Ensure GEMINI_API_KEY is "
13
+ "set in the environment variables."
14
+ )
12
15
13
16
genai .configure (api_key = api_key )
14
17
15
18
# Initialize the Generative Model
16
19
model = genai .GenerativeModel ("gemini-1.5-flash" )
17
20
21
+
18
22
def generate_response (user_input ):
19
23
"""
20
- This function interacts with the Gemini model to generate creative and intelligent responses.
21
-
24
+ This function interacts with the Gemini model
25
+ to generate creative and intelligent responses.
26
+
22
27
:param user_input: The user's input to the chatbot.
23
28
:return: A response generated by the AI.
24
29
"""
25
30
try :
26
- # Generate content based on the user input
27
31
response = model .generate_content (user_input )
28
- return response .text if response else "Sorry, I couldn't generate a response."
32
+ return (
33
+ response .text
34
+ if response
35
+ else "Sorry, I couldn't generate a response."
36
+ )
29
37
except Exception as e :
30
38
return f"An error occurred: { e } "
31
39
40
+
32
41
def chatbot ():
33
42
"""
34
43
Main function to handle continuous interaction with the chatbot.
35
44
It allows the user to ask anything from blog posts to code explanations.
36
45
"""
37
46
print ("🚀 Welcome to the most amazing AI Bot! 🌟" )
38
- print ("💬 You can ask me anything from writing blog posts, to explaining code, to creative writing.\n " )
47
+ print (
48
+ "💬 You can ask me anything from writing blog posts, "
49
+ "to explaining code, to creative writing.\n "
50
+ )
39
51
print ("✨ Just type 'exit' anytime to quit.\n " )
40
52
41
53
while True :
42
54
# Get user input
43
55
user_input = input ("You: " )
44
-
56
+
45
57
# Check for exit condition
46
58
if user_input .lower () == "exit" :
47
59
print ("👋 Goodbye! Thanks for chatting!" )
@@ -51,6 +63,7 @@ def chatbot():
51
63
ai_response = generate_response (user_input )
52
64
print (f"AI Bot: { ai_response } \n " )
53
65
66
+
54
67
if __name__ == "__main__" :
55
68
# Run the chatbot
56
69
chatbot ()
0 commit comments