-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathgemini_model.py
More file actions
70 lines (61 loc) · 2.22 KB
/
Copy pathgemini_model.py
File metadata and controls
70 lines (61 loc) · 2.22 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import google.generativeai as genai
import os
from typing import Optional
class GeminiModel:
def __init__(
self,
api_key=os.environ["GOOGLE_API_KEY"],
model_name="gemini-pro",
temperature: Optional[int] = 0,
top_p: Optional[int] = 1,
top_k: Optional[int] = 1,
max_output_tokens: Optional[int] = 30720,
safety_settings: Optional[dict] = None,
):
# Configure the API with the provided key
genai.configure(api_key=api_key)
# Default configuration settings; can be customized further if needed
generation_config = {
"temperature": temperature,
"top_p": top_p,
"top_k": top_k,
"max_output_tokens": max_output_tokens,
}
safety_settings = safety_settings
# safety_settings = [
# {"category": "HARM_CATEGORY_HARASSMENT", "threshold": "BLOCK_ONLY_HIGH"},
# {"category": "HARM_CATEGORY_HATE_SPEECH", "threshold": "BLOCK_ONLY_HIGH"},
# {
# "category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
# "threshold": "BLOCK_ONLY_HIGH",
# },
# {
# "category": "HARM_CATEGORY_DANGEROUS_CONTENT",
# "threshold": "BLOCK_ONLY_HIGH",
# },
# ]
# Set up the model with the provided model name
self.model = genai.GenerativeModel(
model_name=model_name,
generation_config=generation_config,
safety_settings=safety_settings,
)
def generate_content(self, prompts):
# Generate content based on the provided prompts
response = self.model.generate_content([prompts])
return response.text
def embedding(self, text):
embeddings = genai.embed_content(
model="models/embedding-001",
content=text,
task_type="retrieval_document",
)
return embeddings
# # Example usage:
# if __name__ == "__main__":
# api_key = "YOUR_API_KEY"
# model_name = "gemini-1.0-pro"
# gen_ai_model = GeminiModel(api_key, model_name)
# prompts = ["hey Hi"]
# response_text = gen_ai_model.generate_content(prompts)
# print(response_text)