-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgoogle_vertex_ai.py
62 lines (46 loc) · 2.09 KB
/
google_vertex_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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import asyncio
import vertexai
from vertexai.generative_models import GenerativeModel, HarmCategory, HarmBlockThreshold
from deepeval.models.base_model import DeepEvalBaseLLM
class GoogleVertexAI(DeepEvalBaseLLM):
"""Class that implements Vertex AI for DeepEval"""
def __init__(self, model_name, *args, **kwargs):
super().__init__(model_name, *args, **kwargs)
def load_model(self, *args, **kwargs):
# Initialize safety filters for Vertex AI model
# This is important to ensure no evaluation responses are blocked
safety_settings = {
HarmCategory.HARM_CATEGORY_UNSPECIFIED: HarmBlockThreshold.BLOCK_NONE,
HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT: HarmBlockThreshold.BLOCK_NONE,
HarmCategory.HARM_CATEGORY_HATE_SPEECH: HarmBlockThreshold.BLOCK_NONE,
HarmCategory.HARM_CATEGORY_HARASSMENT: HarmBlockThreshold.BLOCK_NONE,
HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT: HarmBlockThreshold.BLOCK_NONE
}
vertexai.init(project=kwargs['project'], location=kwargs['location'])
return GenerativeModel(
model_name=self.model_name,
safety_settings=safety_settings)
def generate(self, prompt: str) -> str:
return self.model.generate_content(prompt).text
async def a_generate(self, prompt: str) -> str:
response = await self.model.generate_content_async(prompt)
return response.text
def get_model_name(self) -> str:
return self.model_name
def main():
model = GoogleVertexAI(model_name="gemini-1.0-pro-002",
project="genai-atamel",
location="us-central1")
prompt = "Write me a joke"
print(f"Prompt: {prompt}")
response = model.generate(prompt)
print(f"Response: {response}")
async def main_async():
model = GoogleVertexAI(model_name="gemini-1.0-pro-002")
prompt = "Write me a joke"
print(f"Prompt: {prompt}")
response = await model.a_generate(prompt)
print(f"Response: {response}")
if __name__ == '__main__':
main()
# asyncio.run(main_async())