-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot_algo.py
47 lines (37 loc) · 1.43 KB
/
bot_algo.py
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
42
43
44
45
46
47
import textwrap
import google.generativeai as genai
from IPython.display import display, Markdown
from dotenv import load_dotenv
import os
load_dotenv()
# Function to convert text to Markdown format
def bot_gemini(user_input):
def to_markdown(text):
text = text.replace('•', ' *')
return Markdown(textwrap.indent(text, '> ', predicate=lambda _: True))
# Set your API key directly (replace with your actual API key)
GOOGLE_API_KEY = os.getenv('GOOGLE_API_KEY')
genai.configure(api_key=GOOGLE_API_KEY)
# List available models (optional)
try:
for m in genai.list_models():
if 'generateContent' in m.supported_generation_methods:
# print(m.name)
pass
except Exception as e:
print(f"Error listing models: {e}")
# Initialize the generative model
model = genai.GenerativeModel('gemini-1.5-flash')
# Prompt user for input
# user_input = input("Enter the data that Gemini should generate: ")
try:
# Generate content based on user input
response = model.generate_content(user_input)
# Extract and display the generated text content
content = response.candidates[0].content.parts[0].text
return content
# display(to_markdown(content))
except Exception as e:
print(f"Error generating content: {e}")
if __name__ == "__main__":
print(bot_gemini("What is water?"))