Skip to content

Commit b4da586

Browse files
authored
feat(py): add sample test for directory and file prompt loading (#3971)
1 parent 3d9ff21 commit b4da586

File tree

10 files changed

+149
-2
lines changed

10 files changed

+149
-2
lines changed

‎py/packages/genkit/src/genkit/blocks/prompt.py‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1008,7 +1008,10 @@ async def create_prompt_from_file():
10081008
'type': 'prompt',
10091009
'lazy': True,
10101010
'source': 'file',
1011-
'_async_factory': create_prompt_from_file,
1011+
'prompt': {
1012+
'name': name,
1013+
'variant': variant or '',
1014+
},
10121015
}
10131016

10141017
# Create two separate action functions :

‎py/pyproject.toml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ genkit-plugin-ollama = { workspace = true }
113113
genkit-plugin-vertex-ai = { workspace = true }
114114
google-genai-hello = { workspace = true }
115115
google-genai-image = { workspace = true }
116+
prompt-demo = { workspace = true }
117+
116118

117119
[tool.uv.workspace]
118120
members = ["packages/*", "plugins/*", "samples/*"]
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
model: googleai/gemini-1.5-flash
3+
---
4+
This is a PARTIAL that says: {{my_helper "Partial content with helper"}}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hello {{name}}, I am a dot name test!
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hello {{name}}!
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hola {{name}}!
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
model: googleai/gemini-1.5-flash
3+
input:
4+
schema:
5+
name: string
6+
---
7+
8+
This is a nested prompt, hello {{name}}!
9+
{{> shared_partial}}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Copyright 2025 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
#
15+
# SPDX-License-Identifier: Apache-2.0
16+
17+
[project]
18+
authors = [{ name = "Google" }]
19+
classifiers = [
20+
"Development Status :: 3 - Alpha",
21+
"Environment :: Console",
22+
"Environment :: Web Environment",
23+
"Intended Audience :: Developers",
24+
"Operating System :: OS Independent",
25+
"License :: OSI Approved :: Apache Software License",
26+
"Programming Language :: Python",
27+
"Programming Language :: Python :: 3 :: Only",
28+
"Programming Language :: Python :: 3.10",
29+
"Programming Language :: Python :: 3.11",
30+
"Programming Language :: Python :: 3.12",
31+
"Programming Language :: Python :: 3.13",
32+
"Programming Language :: Python :: 3.14",
33+
"Topic :: Scientific/Engineering :: Artificial Intelligence",
34+
"Topic :: Software Development :: Libraries",
35+
]
36+
dependencies = ["genkit", "structlog>=25.2.0", "genkit-plugin-google-genai"]
37+
description = "Genkit prompt demo"
38+
license = { text = "Apache-2.0" }
39+
name = "prompt-demo"
40+
requires-python = ">=3.10"
41+
version = "0.0.1"
42+
43+
[build-system]
44+
build-backend = "hatchling.build"
45+
requires = ["hatchling"]
46+
47+
[tool.hatch.build.targets.wheel]
48+
packages = ["src"]
49+
50+
[tool.hatch.metadata]
51+
allow-direct-references = true
52+
53+
[tool.uv.sources]
54+
genkit = { workspace = true }
55+
genkit-plugin-google-genai = { workspace = true }
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Copyright 2025 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
#
15+
# SPDX-License-Identifier: Apache-2.0
16+
17+
import asyncio
18+
from pathlib import Path
19+
20+
import structlog
21+
22+
from genkit.ai import Genkit
23+
from genkit.blocks.prompt import load_prompt_folder
24+
from genkit.plugins.google_genai import GoogleAI
25+
26+
logger = structlog.get_logger(__name__)
27+
28+
29+
# Initialize with GoogleAI plugin
30+
ai = Genkit(plugins=[GoogleAI()])
31+
32+
33+
async def main():
34+
# Load the prompts from the directory (data)
35+
current_dir = Path(__file__).resolve().parent
36+
prompts_path = current_dir.parent / 'data'
37+
load_prompt_folder(ai.registry, prompts_path)
38+
39+
# List actions to verify loading
40+
actions = ai.registry.list_serializable_actions()
41+
42+
# Filter for prompts to be specific
43+
# Keys start with /prompt
44+
prompts = [key for key in actions.keys() if key.startswith(('/prompt/', '/executable-prompt/'))]
45+
46+
await logger.ainfo('Registry Status', total_actions=len(actions), loaded_prompts=prompts)
47+
48+
if not prompts:
49+
await logger.awarning('No prompts found! Check directory structure.')
50+
51+
52+
if __name__ == '__main__':
53+
ai.run_main(main())

‎py/uv.lock‎

Lines changed: 19 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)