-
Notifications
You must be signed in to change notification settings - Fork 895
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* feat: support gemini close #402 #412 #403 #338 Signed-off-by: yihong0618 <zouzou0208@gmail.com> * fix: versions things Signed-off-by: yihong0618 <zouzou0208@gmail.com> * fix: lint Signed-off-by: yihong0618 <zouzou0208@gmail.com> --------- Signed-off-by: yihong0618 <zouzou0208@gmail.com>
- Loading branch information
1 parent
05a2027
commit e4b30b4
Showing
8 changed files
with
866 additions
and
733 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
"""Google Gemini bot""" | ||
from __future__ import annotations | ||
|
||
from typing import Any | ||
|
||
from rich import print | ||
import google.generativeai as genai | ||
from google.generativeai.types.generation_types import StopCandidateException | ||
|
||
from xiaogpt.bot.base_bot import BaseBot, ChatHistoryMixin | ||
|
||
generation_config = { | ||
"temperature": 0.7, | ||
"top_p": 1, | ||
"top_k": 1, | ||
"max_output_tokens": 2048, | ||
} | ||
|
||
safety_settings = [ | ||
{"category": "HARM_CATEGORY_HARASSMENT", "threshold": "BLOCK_MEDIUM_AND_ABOVE"}, | ||
{"category": "HARM_CATEGORY_HATE_SPEECH", "threshold": "BLOCK_MEDIUM_AND_ABOVE"}, | ||
{ | ||
"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT", | ||
"threshold": "BLOCK_MEDIUM_AND_ABOVE", | ||
}, | ||
{ | ||
"category": "HARM_CATEGORY_DANGEROUS_CONTENT", | ||
"threshold": "BLOCK_MEDIUM_AND_ABOVE", | ||
}, | ||
] | ||
|
||
|
||
class GeminiBot(ChatHistoryMixin, BaseBot): | ||
name = "Gemini" | ||
|
||
def __init__(self, gemini_key: str) -> None: | ||
genai.configure(api_key=gemini_key) | ||
self.history = [] | ||
model = genai.GenerativeModel( | ||
model_name="gemini-pro", | ||
generation_config=generation_config, | ||
safety_settings=safety_settings, | ||
) | ||
self.convo = model.start_chat() | ||
|
||
@classmethod | ||
def from_config(cls, config): | ||
return cls(gemini_key=config.gemini_key) | ||
|
||
async def ask(self, query, **options): | ||
self.convo.send_message(query) | ||
message = self.convo.last.text.strip() | ||
print(message) | ||
if len(self.convo.history) > 10: | ||
self.convo.history = self.convo.history[2:] | ||
return message | ||
|
||
async def ask_stream(self, query: str, **options: Any): | ||
if len(self.convo.history) > 10: | ||
self.convo.history = self.convo.history[2:] | ||
response = self.convo.send_message(query, stream=True) | ||
for chunk in response: | ||
print(chunk.text) | ||
yield chunk.text |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters