Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,11 @@ def _get_openai_request_config(self, request: GenerateRequest) -> dict:
}
if request.tools:
openai_config['tools'] = self._get_tools_definition(request.tools)
if any(msg.role == Role.TOOL for msg in request.messages):
# After a tool response, stop forcing additional tool calls.
openai_config['tool_choice'] = 'none'
elif request.tool_choice:
openai_config['tool_choice'] = request.tool_choice
if request.output:
openai_config['response_format'] = self._get_response_format(request.output)
if request.config:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
else: # noqa
from enum import StrEnum # noqa

from pydantic import BaseModel, ConfigDict
from pydantic import BaseModel, ConfigDict, Field


class OpenAIConfig(BaseModel):
Expand All @@ -38,6 +38,10 @@ class OpenAIConfig(BaseModel):
stop: str | list[str] | None = None
max_tokens: int | None = None
stream: bool | None = None
frequency_penalty: float | None = Field(default=None, ge=-2, le=2)
presence_penalty: float | None = Field(default=None, ge=-2, le=2)
logprobs: bool | None = None
top_logprobs: int | None = Field(default=None, ge=0, le=20)


class SupportedOutputFormat(StrEnum):
Expand Down
52 changes: 52 additions & 0 deletions py/plugins/deepseek/pyproject.toml
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 py/plugins/deepseek/src/genkit/plugins/deepseek/__init__.py
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']
40 changes: 40 additions & 0 deletions py/plugins/deepseek/src/genkit/plugins/deepseek/client.py
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 py/plugins/deepseek/src/genkit/plugins/deepseek/model_info.py
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 py/plugins/deepseek/src/genkit/plugins/deepseek/models.py
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,
},
)
Loading
Loading