Repository navigation
feat: refresh model selections and use the google genai sdk for vertex #153
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,8 +2,8 @@ | |
| import os | ||
| from typing import ClassVar | ||
|
|
||
| import google.api_core.exceptions | ||
| import vertexai.generative_models | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 📰 note: This is the deprecated package we're moving from
🔗 https://docs.cloud.google.com/vertex-ai/generative-ai/docs/deprecations/genai-vertexai-sdk |
||
| from google import genai | ||
| from google.genai import errors, types | ||
|
|
||
| from .base_provider import BaseAPIProvider | ||
|
|
||
|
|
@@ -14,66 +14,31 @@ | |
| class VertexAPI(BaseAPIProvider): | ||
| VERTEX_AI_PROVIDER = "VertexAI" | ||
| MODELS: ClassVar[dict] = { | ||
| "gemini-1.5-flash-001": { | ||
| "name": "Gemini 1.5 Flash 001", | ||
| "gemini-3.5-flash-lite": { | ||
| "name": "Gemini 3.5 Flash-Lite", | ||
| "provider": VERTEX_AI_PROVIDER, | ||
| "max_tokens": 8192, | ||
| "max_tokens": 16384, | ||
| "system_instruction_supported": True, | ||
| }, | ||
| "gemini-1.5-flash-002": { | ||
| "name": "Gemini 1.5 Flash 002", | ||
| "gemini-3.8-flash": { | ||
| "name": "Gemini 3.8 Flash", | ||
| "provider": VERTEX_AI_PROVIDER, | ||
| "max_tokens": 8192, | ||
| "max_tokens": 16384, | ||
| "system_instruction_supported": True, | ||
| }, | ||
| "gemini-1.5-pro-002": { | ||
| "name": "Gemini 1.5 Pro 002", | ||
| "gemini-4-argon": { | ||
| "name": "Gemini 4 Argon", | ||
| "provider": VERTEX_AI_PROVIDER, | ||
| "max_tokens": 8192, | ||
| "system_instruction_supported": True, | ||
| }, | ||
| "gemini-1.5-pro-001": { | ||
| "name": "Gemini 1.5 Pro 001", | ||
| "provider": VERTEX_AI_PROVIDER, | ||
| "max_tokens": 8192, | ||
| "system_instruction_supported": True, | ||
| }, | ||
| "gemini-1.0-pro-002": { | ||
| "name": "Gemini 1.0 Pro 002", | ||
| "provider": VERTEX_AI_PROVIDER, | ||
| "max_tokens": 8192, | ||
| "system_instruction_supported": True, | ||
| }, | ||
| "gemini-1.0-pro-001": { | ||
| "name": "Gemini 1.0 Pro 001", | ||
| "provider": VERTEX_AI_PROVIDER, | ||
| "max_tokens": 8192, | ||
| "system_instruction_supported": False, | ||
| }, | ||
| "gemini-flash-experimental": { | ||
| "name": "Gemini Flash Experimental", | ||
| "provider": VERTEX_AI_PROVIDER, | ||
| "max_tokens": 8192, | ||
| "system_instruction_supported": True, | ||
| }, | ||
| "gemini-pro-experimental": { | ||
| "name": "Gemini Pro Experimental", | ||
| "provider": VERTEX_AI_PROVIDER, | ||
| "max_tokens": 8192, | ||
| "system_instruction_supported": True, | ||
| }, | ||
| "gemini-experimental": { | ||
| "name": "Gemini Experimental", | ||
| "provider": VERTEX_AI_PROVIDER, | ||
| "max_tokens": 8192, | ||
| "max_tokens": 16384, | ||
| "system_instruction_supported": True, | ||
| }, | ||
| } | ||
|
|
||
| def __init__(self): | ||
| self.enabled = bool(os.environ.get("VERTEX_AI_PROJECT_ID", "")) | ||
| if self.enabled: | ||
| vertexai.init( | ||
| self.client = genai.Client( | ||
| enterprise=True, | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 😻 note: This lets us remain compatible with the Vertex API I understand |
||
| project=os.environ.get("VERTEX_AI_PROJECT_ID"), | ||
| location=os.environ.get("VERTEX_AI_LOCATION"), | ||
| ) | ||
|
|
@@ -97,36 +62,22 @@ def generate_response(self, prompt: str, system_content: str) -> str: | |
| prompt = system_content + "\n" + prompt | ||
|
|
||
| try: | ||
| self.client = vertexai.generative_models.GenerativeModel( | ||
| model_name=self.current_model, | ||
| generation_config={ | ||
| "max_output_tokens": self.MODELS[self.current_model]["max_tokens"], | ||
| }, | ||
| system_instruction=system_instruction, | ||
| ) | ||
| response = self.client.generate_content( | ||
| response = self.client.models.generate_content( | ||
| model=self.current_model, | ||
| contents=prompt, | ||
| config=types.GenerateContentConfig( | ||
| max_output_tokens=self.MODELS[self.current_model]["max_tokens"], | ||
| system_instruction=system_instruction, | ||
| ), | ||
| ) | ||
| return "".join(part.text for part in response.candidates[0].content.parts) | ||
| return response.text | ||
|
|
||
| except google.api_core.exceptions.Unauthorized as e: | ||
| logger.error(f"Client is not Authorized. {e.reason}, {e.message}") | ||
| raise | ||
| except google.api_core.exceptions.Forbidden as e: | ||
| logger.error(f"Client Forbidden. {e.reason}, {e.message}") | ||
| raise | ||
| except google.api_core.exceptions.TooManyRequests as e: | ||
| logger.error(f"Too many requests. {e.reason}, {e.message}") | ||
| raise | ||
| except google.api_core.exceptions.ClientError as e: | ||
| logger.error(f"Client error: {e.reason}, {e.message}") | ||
| raise | ||
| except google.api_core.exceptions.ServerError as e: | ||
| logger.error(f"Server error: {e.reason}, {e.message}") | ||
| except errors.ClientError as e: | ||
| logger.error(f"Client error: {e.code} {e.status}, {e.message}") | ||
| raise | ||
| except google.api_core.exceptions.GoogleAPICallError as e: | ||
| logger.error(f"Error: {e.reason}, {e.message}") | ||
| except errors.ServerError as e: | ||
| logger.error(f"Server error: {e.code} {e.status}, {e.message}") | ||
| raise | ||
| except google.api_core.exceptions.GoogleAPIError as e: | ||
| logger.error(f"Unknown error. {e}") | ||
| except errors.APIError as e: | ||
| logger.error(f"Error: {e.code} {e.status}, {e.message}") | ||
| raise | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👁️🗨️ note: Here we account for thinking in responses