Skip to content

Commit c8c86ec

Browse files
AbeJLazaroAbraham Lazaro Martinez
andauthored
feat(py): Resolve action and list actions methods for model garden plugin (#3040)
Co-authored-by: Abraham Lazaro Martinez <[email protected]>
1 parent 9971b35 commit c8c86ec

File tree

4 files changed

+93
-5
lines changed

4 files changed

+93
-5
lines changed

py/plugins/compat-oai/src/genkit/plugins/compat_oai/models/model.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,11 @@ def normalize_config(config: Any) -> OpenAIConfig:
241241
return config
242242

243243
if isinstance(config, dict):
244+
if config.get('topK'):
245+
del config['topK']
246+
if config.get('topP'):
247+
config['top_p'] = config['topP']
248+
del config['topP']
244249
return OpenAIConfig(**config)
245250

246251
raise ValueError(f'Expected request.config to be a dict or OpenAIConfig, got {type(config).__name__}.')

py/plugins/compat-oai/src/genkit/plugins/compat_oai/models/model_info.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class PluginSource(StrEnum):
4646
GPT_4O_MINI = 'gpt-4o-mini'
4747
O1_MINI = 'o1-mini'
4848

49-
LLAMA_3_1 = 'meta/llama3-405b-instruct-maas'
49+
LLAMA_3_1 = 'meta/llama-3.1-405b-instruct-maas'
5050
LLAMA_3_2 = 'meta/llama-3.2-90b-vision-instruct-maas'
5151

5252
SUPPORTED_OPENAI_MODELS: dict[str, ModelInfo] = {

py/plugins/vertex-ai/src/genkit/plugins/vertex_ai/model_garden/modelgarden_plugin.py

Lines changed: 87 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,16 @@
1717
"""ModelGarden API Compatible Plugin for Genkit."""
1818

1919
import os
20+
from functools import cached_property
2021

2122
from genkit.ai import GenkitRegistry, Plugin
23+
from genkit.blocks.model import model_action_metadata
24+
from genkit.core.action.types import ActionKind
25+
from genkit.plugins.compat_oai.models import SUPPORTED_OPENAI_COMPAT_MODELS
26+
from genkit.plugins.compat_oai.typing import OpenAIConfig
2227
from genkit.plugins.vertex_ai import constants as const
2328

24-
from .model_garden import MODELGARDEN_PLUGIN_NAME, ModelGarden
29+
from .model_garden import MODELGARDEN_PLUGIN_NAME, ModelGarden, model_garden_name
2530

2631

2732
class VertexAIModelGarden(Plugin):
@@ -35,8 +40,24 @@ class VertexAIModelGarden(Plugin):
3540

3641
name = MODELGARDEN_PLUGIN_NAME
3742

38-
def __init__(self, project_id: str | None = None, location: str | None = None, models: list[str] | None = None):
39-
"""Initialize the plugin by registering actions with the registry."""
43+
def __init__(
44+
self,
45+
project_id: str | None = None,
46+
location: str | None = None,
47+
models: list[str] | None = None,
48+
) -> None:
49+
"""Initializes the plugin and sets up its configuration.
50+
51+
This constructor prepares the plugin by assigning the Google Cloud project ID,
52+
location, and a list of models to be used.
53+
54+
Args:
55+
project_id: The Google Cloud project ID to use. If not provided, it attempts
56+
to load from the `GCLOUD_PROJECT` environment variable.
57+
location: The Google Cloud region to use for services. If not provided,
58+
it defaults to `DEFAULT_REGION`.
59+
models: An optional list of model names to register with the plugin.
60+
"""
4061
self.project_id = project_id if project_id is not None else os.getenv(const.GCLOUD_PROJECT)
4162
self.location = location if location is not None else const.DEFAULT_REGION
4263
self.models = models
@@ -55,3 +76,66 @@ def initialize(self, ai: GenkitRegistry) -> None:
5576
registry=ai,
5677
)
5778
model_proxy.define_model()
79+
80+
def resolve_action(
81+
self,
82+
ai: GenkitRegistry,
83+
kind: ActionKind,
84+
name: str,
85+
) -> None:
86+
"""Resolves and action.
87+
88+
Args:
89+
ai: The Genkit registry.
90+
kind: The kind of action to resolve.
91+
name: The name of the action to resolve.
92+
"""
93+
if kind == ActionKind.MODEL:
94+
self._resolve_model(ai=ai, name=name)
95+
96+
def _resolve_model(self, ai: GenkitRegistry, name: str) -> None:
97+
"""Resolves and defines a Model Garden Vertex AI model within the Genkit registry.
98+
99+
This internal method handles the logic for registering new models
100+
of Vertex AI Model Garden that are compatible with OpenaI
101+
based on the provided name.
102+
It extracts a clean name, determines the model type, instantiates the
103+
appropriate model class, and registers it with the Genkit AI registry.
104+
105+
Args:
106+
ai: The Genkit AI registry instance to define the model in.
107+
name: The name of the model to resolve. This name might include a
108+
prefix indicating it's from a specific plugin.
109+
"""
110+
clean_name = (
111+
name.replace(MODELGARDEN_PLUGIN_NAME + '/', '') if name.startswith(MODELGARDEN_PLUGIN_NAME) else name
112+
)
113+
114+
model_proxy = ModelGarden(
115+
model=clean_name,
116+
location=self.location,
117+
project_id=self.project_id,
118+
registry=ai,
119+
)
120+
model_proxy.define_model()
121+
122+
@cached_property
123+
def list_actions(self) -> list[dict[str, str]]:
124+
"""Generate a list of available actions or models.
125+
126+
Returns:
127+
list of actions dicts with the following shape:
128+
{
129+
'name': str,
130+
'kind': ActionKind,
131+
}
132+
"""
133+
actions_list = []
134+
for model, model_info in SUPPORTED_OPENAI_COMPAT_MODELS.items():
135+
actions_list.append(
136+
model_action_metadata(
137+
name=model_garden_name(model), info=model_info.model_dump(), config_schema=OpenAIConfig
138+
)
139+
)
140+
141+
return actions_list

py/samples/model-garden/src/main.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
plugins=[
2626
VertexAIModelGarden(
2727
location='us-central1',
28-
models=['meta/llama-3.2-90b-vision-instruct-maas'],
2928
),
3029
],
3130
)

0 commit comments

Comments
 (0)