trustgraph-vertexai 1.4.10__tar.gz → 1.4.12__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of trustgraph-vertexai might be problematic. Click here for more details.

@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: trustgraph-vertexai
3
- Version: 1.4.10
3
+ Version: 1.4.12
4
4
  Summary: TrustGraph provides a means to run a pipeline of flexible AI processing components in a flexible means to achieve a processing pipeline.
5
5
  Author-email: "trustgraph.ai" <security@trustgraph.ai>
6
6
  Project-URL: Homepage, https://github.com/trustgraph-ai/trustgraph
@@ -18,6 +18,7 @@ Supports both Google's Gemini models and Anthropic's Claude models.
18
18
 
19
19
  from google.oauth2 import service_account
20
20
  import google.auth
21
+ import google.api_core.exceptions
21
22
  import vertexai
22
23
  import logging
23
24
 
@@ -59,8 +60,17 @@ class Processor(LlmService):
59
60
 
60
61
  super(Processor, self).__init__(**params)
61
62
 
62
- self.model = model
63
- self.is_anthropic = 'claude' in self.model.lower()
63
+ # Store default model and configuration parameters
64
+ self.default_model = model
65
+ self.region = region
66
+ self.temperature = temperature
67
+ self.max_output = max_output
68
+ self.private_key = private_key
69
+
70
+ # Model client caches
71
+ self.model_clients = {} # Cache for model instances
72
+ self.generation_configs = {} # Cache for generation configs (Gemini only)
73
+ self.anthropic_client = None # Single Anthropic client (handles multiple models)
64
74
 
65
75
  # Shared parameters for both model types
66
76
  self.api_params = {
@@ -89,71 +99,91 @@ class Processor(LlmService):
89
99
  "Ensure it's set in your environment or service account."
90
100
  )
91
101
 
92
- # Initialize the appropriate client based on the model type
93
- if self.is_anthropic:
94
- logger.info(f"Initializing Anthropic model '{model}' via AnthropicVertex SDK")
95
- # Initialize AnthropicVertex with credentials if provided, otherwise use ADC
96
- anthropic_kwargs = {'region': region, 'project_id': project_id}
97
- if credentials and private_key: # Pass credentials only if from a file
98
- anthropic_kwargs['credentials'] = credentials
99
- logger.debug(f"Using service account credentials for Anthropic model")
102
+ # Store credentials and project info for later use
103
+ self.credentials = credentials
104
+ self.project_id = project_id
105
+
106
+ # Initialize Vertex AI SDK for Gemini models
107
+ init_kwargs = {'location': region, 'project': project_id}
108
+ if credentials and private_key: # Pass credentials only if from a file
109
+ init_kwargs['credentials'] = credentials
110
+
111
+ vertexai.init(**init_kwargs)
112
+
113
+ # Pre-initialize Anthropic client if needed (single client handles all Claude models)
114
+ if 'claude' in self.default_model.lower():
115
+ self._get_anthropic_client()
116
+
117
+ # Safety settings for Gemini models
118
+ block_level = HarmBlockThreshold.BLOCK_ONLY_HIGH
119
+ self.safety_settings = [
120
+ SafetySetting(
121
+ category = HarmCategory.HARM_CATEGORY_HARASSMENT,
122
+ threshold = block_level,
123
+ ),
124
+ SafetySetting(
125
+ category = HarmCategory.HARM_CATEGORY_HATE_SPEECH,
126
+ threshold = block_level,
127
+ ),
128
+ SafetySetting(
129
+ category = HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT,
130
+ threshold = block_level,
131
+ ),
132
+ SafetySetting(
133
+ category = HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT,
134
+ threshold = block_level,
135
+ ),
136
+ ]
137
+
138
+ logger.info("VertexAI initialization complete")
139
+
140
+ def _get_anthropic_client(self):
141
+ """Get or create the Anthropic client (single client for all Claude models)"""
142
+ if self.anthropic_client is None:
143
+ logger.info(f"Initializing AnthropicVertex client")
144
+ anthropic_kwargs = {'region': self.region, 'project_id': self.project_id}
145
+ if self.credentials and self.private_key: # Pass credentials only if from a file
146
+ anthropic_kwargs['credentials'] = self.credentials
147
+ logger.debug(f"Using service account credentials for Anthropic models")
100
148
  else:
101
- logger.debug(f"Using Application Default Credentials for Anthropic model")
102
-
103
- self.llm = AnthropicVertex(**anthropic_kwargs)
104
- else:
105
- # For Gemini models, initialize the Vertex AI SDK
106
- logger.info(f"Initializing Google model '{model}' via Vertex AI SDK")
107
- init_kwargs = {'location': region, 'project': project_id}
108
- if credentials and private_key: # Pass credentials only if from a file
109
- init_kwargs['credentials'] = credentials
110
-
111
- vertexai.init(**init_kwargs)
112
-
113
- self.llm = GenerativeModel(model)
114
-
115
- self.generation_config = GenerationConfig(
116
- temperature=temperature,
149
+ logger.debug(f"Using Application Default Credentials for Anthropic models")
150
+
151
+ self.anthropic_client = AnthropicVertex(**anthropic_kwargs)
152
+
153
+ return self.anthropic_client
154
+
155
+ def _get_gemini_model(self, model_name):
156
+ """Get or create a Gemini model instance"""
157
+ if model_name not in self.model_clients:
158
+ logger.info(f"Creating GenerativeModel instance for '{model_name}'")
159
+ self.model_clients[model_name] = GenerativeModel(model_name)
160
+
161
+ # Create generation config for this model
162
+ self.generation_configs[model_name] = GenerationConfig(
163
+ temperature=self.temperature,
117
164
  top_p=1.0,
118
165
  top_k=10,
119
166
  candidate_count=1,
120
- max_output_tokens=max_output,
167
+ max_output_tokens=self.max_output,
121
168
  )
122
169
 
123
- # Block none doesn't seem to work
124
- block_level = HarmBlockThreshold.BLOCK_ONLY_HIGH
125
- # block_level = HarmBlockThreshold.BLOCK_NONE
126
-
127
- self.safety_settings = [
128
- SafetySetting(
129
- category = HarmCategory.HARM_CATEGORY_HARASSMENT,
130
- threshold = block_level,
131
- ),
132
- SafetySetting(
133
- category = HarmCategory.HARM_CATEGORY_HATE_SPEECH,
134
- threshold = block_level,
135
- ),
136
- SafetySetting(
137
- category = HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT,
138
- threshold = block_level,
139
- ),
140
- SafetySetting(
141
- category = HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT,
142
- threshold = block_level,
143
- ),
144
- ]
170
+ return self.model_clients[model_name], self.generation_configs[model_name]
145
171
 
172
+ async def generate_content(self, system, prompt, model=None):
146
173
 
147
- logger.info("VertexAI initialization complete")
174
+ # Use provided model or fall back to default
175
+ model_name = model or self.default_model
148
176
 
149
- async def generate_content(self, system, prompt):
177
+ logger.debug(f"Using model: {model_name}")
150
178
 
151
179
  try:
152
- if self.is_anthropic:
180
+ if 'claude' in model_name.lower():
153
181
  # Anthropic API uses a dedicated system prompt
154
- logger.debug("Sending request to Anthropic model...")
155
- response = self.llm.messages.create(
156
- model=self.model,
182
+ logger.debug(f"Sending request to Anthropic model '{model_name}'...")
183
+ client = self._get_anthropic_client()
184
+
185
+ response = client.messages.create(
186
+ model=model_name,
157
187
  system=system,
158
188
  messages=[{"role": "user", "content": prompt}],
159
189
  max_tokens=self.api_params['max_output_tokens'],
@@ -166,15 +196,17 @@ class Processor(LlmService):
166
196
  text=response.content[0].text,
167
197
  in_token=response.usage.input_tokens,
168
198
  out_token=response.usage.output_tokens,
169
- model=self.model
199
+ model=model_name
170
200
  )
171
201
  else:
172
202
  # Gemini API combines system and user prompts
173
- logger.debug("Sending request to Gemini model...")
203
+ logger.debug(f"Sending request to Gemini model '{model_name}'...")
174
204
  full_prompt = system + "\n\n" + prompt
175
205
 
176
- response = self.llm.generate_content(
177
- full_prompt, generation_config = self.generation_config,
206
+ llm, generation_config = self._get_gemini_model(model_name)
207
+
208
+ response = llm.generate_content(
209
+ full_prompt, generation_config = generation_config,
178
210
  safety_settings = self.safety_settings,
179
211
  )
180
212
 
@@ -182,7 +214,7 @@ class Processor(LlmService):
182
214
  text = response.text,
183
215
  in_token = response.usage_metadata.prompt_token_count,
184
216
  out_token = response.usage_metadata.candidates_token_count,
185
- model = self.model
217
+ model = model_name
186
218
  )
187
219
 
188
220
  logger.info(f"Input Tokens: {resp.in_token}")
@@ -0,0 +1 @@
1
+ __version__ = "1.4.12"
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: trustgraph-vertexai
3
- Version: 1.4.10
3
+ Version: 1.4.12
4
4
  Summary: TrustGraph provides a means to run a pipeline of flexible AI processing components in a flexible means to achieve a processing pipeline.
5
5
  Author-email: "trustgraph.ai" <security@trustgraph.ai>
6
6
  Project-URL: Homepage, https://github.com/trustgraph-ai/trustgraph
@@ -1 +0,0 @@
1
- __version__ = "1.4.10"