1717"""ModelGarden API Compatible Plugin for Genkit."""
1818
1919import os
20+ from functools import cached_property
2021
2122from 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
2227from 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
2732class 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
0 commit comments