Skip to content

Commit e9c7d65

Browse files
committed
feat(py): add support for ai.define_resource
1 parent e2a1508 commit e9c7d65

File tree

9 files changed

+755
-2
lines changed

9 files changed

+755
-2
lines changed

py/packages/genkit/pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ dependencies = [
5050
"dotpromptz>=0.1.4",
5151
"uvicorn>=0.34.0",
5252
"anyio>=4.9.0",
53+
"uritemplate>=4.1.1",
5354
]
5455
description = "Genkit AI Framework"
5556
license = { text = "Apache-2.0" }

py/packages/genkit/src/genkit/ai/_registry.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import structlog
4848
from pydantic import BaseModel
4949

50+
from genkit.ai.resource import ResourceFn, ResourceOptions, define_resource
5051
from genkit.blocks.embedding import EmbedderFn, EmbedderOptions
5152
from genkit.blocks.evaluator import BatchEvaluatorFn, EvaluatorFn
5253
from genkit.blocks.formats.types import FormatDef
@@ -675,6 +676,21 @@ async def prompt(
675676
variant=variant,
676677
)
677678

679+
def define_resource(self, opts: ResourceOptions, fn: ResourceFn) -> Action:
680+
"""Defines a resource and registers it with the registry.
681+
682+
This creates a resource action that can handle requests for a specific URI
683+
or URI template.
684+
685+
Args:
686+
opts: Options defining the resource (name, uri, template, etc.).
687+
fn: The function that implements resource content retrieval.
688+
689+
Returns:
690+
The registered `Action` for the resource.
691+
"""
692+
return define_resource(self.registry, opts, fn)
693+
678694

679695
class FlowWrapper:
680696
"""A wapper for flow functions to add `stream` method."""

0 commit comments

Comments
 (0)