-
Notifications
You must be signed in to change notification settings - Fork 302
feat(py/genkit): added the resolve_method for openai compatible plugin #3055
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
Open
hendrixmar
wants to merge
5
commits into
main
Choose a base branch
from
hendrixmar/feature/add_resolve_action_openai_compat
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+99
−1
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7bf0fcc
feat(py/genkit): added the resolve_method for openai compatible plugin
hendrixmar 75a5ced
feat(py/genkit): added the resolve_method for openai compatible plugin
hendrixmar 9339880
feat(py/genkit): added the resolve_method for openai compatible plugin
hendrixmar e0d8647
feat(py/genkit): solve pr comments
hendrixmar 052b712
chore: added a constant for the metadata definition in the openai plugin
hendrixmar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,16 +16,30 @@ | |
|
||
|
||
"""OpenAI OpenAI API Compatible Plugin for Genkit.""" | ||
from typing import Any | ||
|
||
from openai import Client, OpenAI as OpenAIClient | ||
|
||
from genkit.ai._plugin import Plugin | ||
from genkit.ai._registry import GenkitRegistry | ||
from genkit.core.action.types import ActionKind | ||
from genkit.plugins.compat_oai.models import ( | ||
SUPPORTED_OPENAI_MODELS, | ||
OpenAIModel, | ||
OpenAIModelHandler, | ||
) | ||
from genkit.plugins.compat_oai.typing import OpenAIConfig | ||
from genkit.plugins.ollama.models import ModelDefinition | ||
|
||
OPENAI_PLUGIN_NAME = 'openai' | ||
|
||
def default_openai_metadata(name: str) -> dict[str, Any]: | ||
return { | ||
'model': { | ||
'label': f"OpenAI - {name}", | ||
'supports': {'multiturn': True} | ||
}, | ||
} | ||
|
||
|
||
class OpenAI(Plugin): | ||
|
@@ -69,6 +83,40 @@ def initialize(self, ai: GenkitRegistry) -> None: | |
}, | ||
) | ||
|
||
def resolve_action( # noqa: B027 | ||
hendrixmar marked this conversation as resolved.
Show resolved
Hide resolved
hendrixmar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self, | ||
ai: GenkitRegistry, | ||
kind: ActionKind, | ||
name: str, | ||
) -> None: | ||
|
||
if kind is not ActionKind.MODEL: | ||
return None | ||
|
||
self._define_openai_model(ai, name) | ||
return None | ||
|
||
def _define_openai_model(self, ai: GenkitRegistry, name: str) -> None: | ||
"""Defines and registers an OpenAI model with Genkit. | ||
|
||
Cleans the model name, instantiates an OpenAI, and registers it | ||
with the provided Genkit AI registry, including metadata about its capabilities. | ||
|
||
Args: | ||
ai: The Genkit AI registry instance. | ||
name: The name of the model to be registered. | ||
""" | ||
|
||
handler = OpenAIModelHandler(OpenAIModel(name, self._openai_client, ai)).generate | ||
ai.define_model( | ||
name=f'openai/{name}', | ||
fn=handler, | ||
config_schema=OpenAIConfig, | ||
metadata=default_openai_metadata(name) | ||
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. Can we validate that the model name is not in the supported models? like for model garden here: https://github.com/firebase/genkit/blob/main/py/plugins/vertex-ai/src/genkit/plugins/vertex_ai/model_garden/model_garden.py#L91 |
||
) | ||
|
||
|
||
|
||
|
||
def openai_model(name: str) -> str: | ||
"""Returns a string representing the OpenAI model name to use with Genkit. | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
what is the purpose of this constant?