-
Notifications
You must be signed in to change notification settings - Fork 1
/
google_generative_ai.py
38 lines (32 loc) · 1.39 KB
/
google_generative_ai.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
import google.generativeai as genai
class GoogleGenerativeAI:
def __init__(self, api_key):
self.api_key = api_key
self.model = None
self.configure()
def configure(self):
genai.configure(api_key=self.api_key)
generation_config = {
"temperature": 0.9,
"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"}
]
self.model = genai.GenerativeModel(model_name="gemini-pro",
generation_config=generation_config,
safety_settings=safety_settings)
def generate_content(self, prompt_parts):
response = self.model.generate_content(prompt_parts)
return response.text
# Usage Example
# api_key = "YOUR_API_KEY" # Replace with your API key
# google_ai = GoogleGenerativeAI(api_key)
# prompt_parts = ["你好"]
# response_text = google_ai.generate_content(prompt_parts)
# print(response_text)