Gemini is a class that uses the Gemini API to generate responses to messages.
Docs: - https://ai.google.dev/tutorials/python_quickstart
Source code in src/project/gemini/Gemini.py
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81 | class Gemini:
"""Gemini is a class that uses the Gemini API to generate responses to messages.
Docs: - https://ai.google.dev/tutorials/python_quickstart
"""
def __init__(self):
"""
Initializes the Gemini API with the API key from the environment variables.
"""
google_api_key = os.getenv('GEMINI_KEY')
genai.configure(api_key=google_api_key)
self.__model = genai.GenerativeModel('gemini-pro')
self.__chat = self.__model.start_chat(history=[])
def start_new_chat(self, m_list:list[dict]=None):
"""Start a new chat with the given message list.
Args:
m_list (list[dict], optional): Chat history. Defaults to None.
"""
if m_list is None:
m_list = []
self.__chat = genai.GenerativeModel('gemini-pro').start_chat(
history=map_message_list_to_history(m_list)
)
def send_message(self, message: str, role: Role) -> Message:
"""Sends a message to the Gemini API and returns the response.
Args:
message (str): Message to send.
role (Role): Role of the message sender.
Returns:
Message: Response from the Gemini API mapped to the Message class.
"""
response = self.__chat.send_message({'role': role.name.lower(), 'parts': [message]})
return Message(
role=Role.MODEL,
content=Gemini.to_markdown(response.parts[0].text).data
)
def summaries(self, url:str) -> Markdown:
"""Summarizes the content of the given URL.
Args:
url (str): Url to summarize.
Returns:
Markdown: Markdown formatted summary of the content.
"""
prompt = f'''Summarize the following url in elaborately as possible.
{url}
'''
response = self.__model.generate_content(prompt)
return Gemini.to_markdown(response.text)
@staticmethod
def to_markdown(text:str)->Markdown:
"""Converts the given text to Markdown format.
Args:
text (str): Text to convert.
Returns:
Markdown: Markdown formatted text.
"""
text = text.replace('•', ' *')
return Markdown(textwrap.indent(text, '> ', predicate=lambda _: True))
|
__init__()
Initializes the Gemini API with the API key from the environment variables.
Source code in src/project/gemini/Gemini.py
| def __init__(self):
"""
Initializes the Gemini API with the API key from the environment variables.
"""
google_api_key = os.getenv('GEMINI_KEY')
genai.configure(api_key=google_api_key)
self.__model = genai.GenerativeModel('gemini-pro')
self.__chat = self.__model.start_chat(history=[])
|
send_message(message, role)
Sends a message to the Gemini API and returns the response.
Parameters:
-
message
(str
)
–
-
role
(database.Session.Role
)
–
Role of the message sender.
Returns:
-
Message
( database.Session.Message
) –
Response from the Gemini API mapped to the Message class.
Source code in src/project/gemini/Gemini.py
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53 | def send_message(self, message: str, role: Role) -> Message:
"""Sends a message to the Gemini API and returns the response.
Args:
message (str): Message to send.
role (Role): Role of the message sender.
Returns:
Message: Response from the Gemini API mapped to the Message class.
"""
response = self.__chat.send_message({'role': role.name.lower(), 'parts': [message]})
return Message(
role=Role.MODEL,
content=Gemini.to_markdown(response.parts[0].text).data
)
|
start_new_chat(m_list=None)
Start a new chat with the given message list.
Parameters:
-
m_list
(list[dict]
, default:
None
)
–
Chat history. Defaults to None.
Source code in src/project/gemini/Gemini.py
27
28
29
30
31
32
33
34
35
36
37 | def start_new_chat(self, m_list:list[dict]=None):
"""Start a new chat with the given message list.
Args:
m_list (list[dict], optional): Chat history. Defaults to None.
"""
if m_list is None:
m_list = []
self.__chat = genai.GenerativeModel('gemini-pro').start_chat(
history=map_message_list_to_history(m_list)
)
|
summaries(url)
Summarizes the content of the given URL.
Parameters:
Returns:
-
Markdown
( IPython.display.Markdown
) –
Markdown formatted summary of the content.
Source code in src/project/gemini/Gemini.py
55
56
57
58
59
60
61
62
63
64
65
66
67
68 | def summaries(self, url:str) -> Markdown:
"""Summarizes the content of the given URL.
Args:
url (str): Url to summarize.
Returns:
Markdown: Markdown formatted summary of the content.
"""
prompt = f'''Summarize the following url in elaborately as possible.
{url}
'''
response = self.__model.generate_content(prompt)
return Gemini.to_markdown(response.text)
|
to_markdown(text)
staticmethod
Converts the given text to Markdown format.
Parameters:
Returns:
-
Markdown
( IPython.display.Markdown
) –
Source code in src/project/gemini/Gemini.py
70
71
72
73
74
75
76
77
78
79
80
81 | @staticmethod
def to_markdown(text:str)->Markdown:
"""Converts the given text to Markdown format.
Args:
text (str): Text to convert.
Returns:
Markdown: Markdown formatted text.
"""
text = text.replace('•', ' *')
return Markdown(textwrap.indent(text, '> ', predicate=lambda _: True))
|