-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgoogle_vertex_ai_langchain.py
70 lines (53 loc) · 2.25 KB
/
google_vertex_ai_langchain.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
62
63
64
65
66
67
import asyncio
from langchain_google_vertexai import (
ChatVertexAI,
HarmBlockThreshold,
HarmCategory
)
from deepeval.models.base_model import DeepEvalBaseLLM
class GoogleVertexAILangChain(DeepEvalBaseLLM):
"""Class that implements Vertex AI via LangChain 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
}
return ChatVertexAI(
model_name=self.model_name,
safety_settings=safety_settings,
project=kwargs['project'],
location=kwargs['location']
)
def generate(self, prompt: str) -> str:
return self.model.invoke(prompt).content
async def a_generate(self, prompt: str) -> str:
response = await self.model.ainvoke(prompt)
return response.content
def get_model_name(self):
return self.model_name
def main():
model = GoogleVertexAILangChain(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 = GoogleVertexAILangChain(model_name="gemini-1.0-pro-002",
project="genai-atamel",
location="us-central1")
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())