-
Notifications
You must be signed in to change notification settings - Fork 631
feat(py): Implemented deepseek plugin #4051
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
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
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 |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| # Copyright 2026 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| # | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| [project] | ||
| name = "genkit-plugin-deepseek" | ||
| version = "0.1.0" | ||
| description = "Genkit DeepSeek Plugin" | ||
| authors = [{ name = "Google" }] | ||
| license = { text = "Apache-2.0" } | ||
| requires-python = ">=3.10" | ||
| dependencies = [ | ||
| "genkit", | ||
| "genkit-plugin-compat-oai", | ||
| "openai>=1.0.0", | ||
| ] | ||
| classifiers = [ | ||
| "Development Status :: 3 - Alpha", | ||
| "Environment :: Console", | ||
| "Environment :: Web Environment", | ||
| "Intended Audience :: Developers", | ||
| "Operating System :: OS Independent", | ||
| "License :: OSI Approved :: Apache Software License", | ||
| "Programming Language :: Python", | ||
| "Programming Language :: Python :: 3 :: Only", | ||
| "Programming Language :: Python :: 3.10", | ||
| "Programming Language :: Python :: 3.11", | ||
| "Programming Language :: Python :: 3.12", | ||
| "Programming Language :: Python :: 3.13", | ||
| "Programming Language :: Python :: 3.14", | ||
| "Topic :: Scientific/Engineering :: Artificial Intelligence", | ||
| "Topic :: Software Development :: Libraries", | ||
| ] | ||
|
|
||
| [build-system] | ||
| build-backend = "hatchling.build" | ||
| requires = ["hatchling"] | ||
|
|
||
| [tool.hatch.build.targets.wheel] | ||
| packages = ["src/genkit", "src/genkit/plugins"] |
22 changes: 22 additions & 0 deletions
22
py/plugins/deepseek/src/genkit/plugins/deepseek/__init__.py
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 |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| # Copyright 2026 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| # | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| """DeepSeek plugin for Genkit.""" | ||
|
|
||
| from .models import deepseek_name | ||
| from .plugin import DeepSeek | ||
|
|
||
| __all__ = ['DeepSeek', 'deepseek_name'] |
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 |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| # Copyright 2026 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| # | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| """DeepSeek API client.""" | ||
|
|
||
| from openai import OpenAI as _OpenAI | ||
|
|
||
|
|
||
| class DeepSeekClient: | ||
| """DeepSeek API client initialization.""" | ||
|
|
||
| def __new__(cls, **deepseek_params) -> _OpenAI: | ||
| """Initialize the DeepSeek client. | ||
|
|
||
| Args: | ||
| **deepseek_params: Client configuration parameters including: | ||
| - api_key: DeepSeek API key. | ||
| - base_url: API base URL (defaults to https://api.deepseek.com). | ||
| - Additional OpenAI client parameters. | ||
|
|
||
| Returns: | ||
| Configured OpenAI client instance. | ||
| """ | ||
| api_key = deepseek_params.pop('api_key') | ||
| base_url = deepseek_params.pop('base_url', 'https://api.deepseek.com') | ||
|
|
||
| return _OpenAI(api_key=api_key, base_url=base_url, **deepseek_params) |
58 changes: 58 additions & 0 deletions
58
py/plugins/deepseek/src/genkit/plugins/deepseek/model_info.py
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 |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| # Copyright 2026 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| # | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| """DeepSeek model information and metadata.""" | ||
|
|
||
| from genkit.types import ModelInfo, Supports | ||
|
|
||
| __all__ = ['SUPPORTED_DEEPSEEK_MODELS', 'get_default_model_info'] | ||
|
|
||
| # Model capabilities matching JS implementation | ||
| _DEEPSEEK_SUPPORTS = Supports( | ||
| multiturn=True, | ||
| tools=True, | ||
| media=False, | ||
| system_role=True, | ||
| output=['text', 'json'], | ||
| ) | ||
|
|
||
| SUPPORTED_DEEPSEEK_MODELS: dict[str, ModelInfo] = { | ||
| 'deepseek-reasoner': ModelInfo( | ||
| label='DeepSeek - Reasoner', | ||
| versions=['deepseek-reasoner'], | ||
| supports=_DEEPSEEK_SUPPORTS, | ||
| ), | ||
| 'deepseek-chat': ModelInfo( | ||
| label='DeepSeek - Chat', | ||
| versions=['deepseek-chat'], | ||
| supports=_DEEPSEEK_SUPPORTS, | ||
| ), | ||
| } | ||
|
|
||
|
|
||
| def get_default_model_info(name: str) -> ModelInfo: | ||
| """Get default model information for unknown DeepSeek models. | ||
|
|
||
| Args: | ||
| name: Model name. | ||
|
|
||
| Returns: | ||
| Default ModelInfo with standard DeepSeek capabilities. | ||
| """ | ||
| return ModelInfo( | ||
| label=f'DeepSeek - {name}', | ||
| supports=_DEEPSEEK_SUPPORTS, | ||
| ) | ||
124 changes: 124 additions & 0 deletions
124
py/plugins/deepseek/src/genkit/plugins/deepseek/models.py
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 |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| # Copyright 2026 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| # | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| """DeepSeek model integration for Genkit.""" | ||
|
|
||
| from collections.abc import Callable | ||
| from typing import Any | ||
|
|
||
| from genkit.ai import GenkitRegistry | ||
| from genkit.plugins.compat_oai.models.model import OpenAIModel | ||
| from genkit.plugins.compat_oai.typing import OpenAIConfig | ||
| from genkit.plugins.deepseek.client import DeepSeekClient | ||
| from genkit.plugins.deepseek.model_info import ( | ||
| SUPPORTED_DEEPSEEK_MODELS, | ||
| get_default_model_info, | ||
| ) | ||
|
|
||
| DEEPSEEK_PLUGIN_NAME = 'deepseek' | ||
|
|
||
|
|
||
| def deepseek_name(name: str) -> str: | ||
| """Create a DeepSeek action name. | ||
|
|
||
| Args: | ||
| name: Base name for the action. | ||
|
|
||
| Returns: | ||
| The fully qualified DeepSeek action name. | ||
| """ | ||
| return f'{DEEPSEEK_PLUGIN_NAME}/{name}' | ||
|
|
||
|
|
||
| class DeepSeekModel: | ||
| """Manages DeepSeek model integration for Genkit. | ||
|
|
||
| This class provides integration with DeepSeek's OpenAI-compatible API, | ||
| allowing DeepSeek models to be exposed as Genkit models. It handles | ||
| client initialization, model information retrieval, and dynamic model | ||
| definition within the Genkit registry. | ||
|
|
||
| Follows the Model Garden pattern for implementation consistency. | ||
| """ | ||
|
|
||
| def __init__( | ||
| self, | ||
| model: str, | ||
| api_key: str, | ||
| registry: GenkitRegistry, | ||
| **deepseek_params, | ||
| ) -> None: | ||
| """Initialize the DeepSeek instance. | ||
|
|
||
| Args: | ||
| model: The name of the specific DeepSeek model (e.g., 'deepseek-chat'). | ||
| api_key: DeepSeek API key for authentication. | ||
| registry: An instance of GenkitRegistry to register the model. | ||
| **deepseek_params: Additional parameters for the DeepSeek client. | ||
| """ | ||
| self.name = model | ||
| self.ai = registry | ||
| client_params = {'api_key': api_key, **deepseek_params} | ||
| self.client = DeepSeekClient(**client_params) | ||
|
|
||
| def get_model_info(self) -> dict[str, Any] | None: | ||
| """Retrieve metadata and supported features for the specified model. | ||
|
|
||
| This method looks up the model's information from a predefined list | ||
| of supported DeepSeek models or provides default information. | ||
|
|
||
| Returns: | ||
| A dictionary containing the model's 'name' and 'supports' features. | ||
| The 'supports' key contains a dictionary representing the model's | ||
| capabilities (e.g., tools, streaming). | ||
| """ | ||
| model_info = SUPPORTED_DEEPSEEK_MODELS.get(self.name, get_default_model_info(self.name)) | ||
| return { | ||
| 'name': model_info.label, | ||
| 'supports': model_info.supports.model_dump(), | ||
| } | ||
|
|
||
| def to_deepseek_model(self) -> Callable: | ||
| """Convert the DeepSeek model into a Genkit-compatible model function. | ||
|
|
||
| This method wraps the underlying DeepSeek client and its generation | ||
| logic into a callable that adheres to the OpenAI model interface | ||
| expected by Genkit. | ||
|
|
||
| Returns: | ||
| A callable function (the generate method of an OpenAIModel instance) | ||
| that can be used by Genkit. | ||
| """ | ||
| deepseek_model = OpenAIModel(self.name, self.client, self.ai) | ||
| return deepseek_model.generate | ||
|
|
||
| def define_model(self) -> None: | ||
| """Define and register the DeepSeek model with the Genkit registry. | ||
|
|
||
| This method orchestrates the retrieval of model metadata and the | ||
| creation of the generation function, then registers this model | ||
| within the Genkit framework using self.ai.define_model. | ||
| """ | ||
| model_info = self.get_model_info() | ||
| generate_fn = self.to_deepseek_model() | ||
| self.ai.define_model( | ||
| name=deepseek_name(self.name), | ||
| fn=generate_fn, | ||
| config_schema=OpenAIConfig, | ||
| metadata={ | ||
| 'model': model_info, | ||
| }, | ||
| ) |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.