Skip to content

Commit 7b67366

Browse files
authored
fix: Return None if template doesn't exist (#160)
1 parent a94ed8c commit 7b67366

File tree

3 files changed

+59
-1
lines changed

3 files changed

+59
-1
lines changed

examples/templates.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# /// script
2+
# requires-python = ">=3.10"
3+
# dependencies = ["galileo"]
4+
# ///
5+
6+
from galileo import Message, MessageRole
7+
from galileo.prompts import create_prompt_template, get_prompt_template
8+
9+
# Get existing template
10+
prompt_template = get_prompt_template(name="good-prompt", project="My first project")
11+
12+
# Create template if it doesn't exist
13+
if prompt_template is None:
14+
prompt_template = create_prompt_template(
15+
name="good-prompt",
16+
project="My first project",
17+
messages=[
18+
Message(role=MessageRole.SYSTEM, content="you are a helpful assistant"),
19+
Message(role=MessageRole.USER, content="why is sky blue?"),
20+
],
21+
)
22+
23+
# Retrieve the template we just created
24+
retrieved_template = get_prompt_template(project="My First Project", name="science-assistant")

src/galileo/prompts.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,8 @@ def get_prompt_template(project: str, name: str) -> Optional[PromptTemplate]:
122122
if prompt_template.name == name:
123123
_logger.info(f"Get template {prompt_template}")
124124
return prompt_template
125-
raise ValueError(f"Prompt {name} does not exist")
125+
_logger.warning(f"Template {name} not found in project {project}")
126+
return None
126127

127128

128129
def list_prompt_templates(project: str) -> list[PromptTemplate]:

tests/test_prompts.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,3 +120,36 @@ def test_list_prompts(list_prompt_templates_mock: Mock, get_projects_projects_ge
120120

121121
list_prompt_templates_mock.sync.assert_called_once()
122122
get_projects_projects_get_mock.sync_detailed.assert_called_once()
123+
124+
125+
@patch("galileo.projects.get_projects_projects_get")
126+
@patch("galileo.prompts.get_project_templates_projects_project_id_templates_get")
127+
def test_get_prompt_template_found(list_prompt_templates_mock: Mock, get_projects_projects_get_mock: Mock):
128+
"""Test get_prompt_template when template exists."""
129+
list_prompt_templates_mock.sync.return_value = [prompt_template()]
130+
get_projects_projects_get_mock.sync_detailed.return_value = projects_response()
131+
132+
from galileo.prompts import get_prompt_template
133+
134+
template = get_prompt_template(name="andrii-good-prompt", project="andrii-new-project")
135+
136+
assert template is not None
137+
assert template.name == "andrii-good-prompt"
138+
list_prompt_templates_mock.sync.assert_called_once()
139+
get_projects_projects_get_mock.sync_detailed.assert_called_once()
140+
141+
142+
@patch("galileo.projects.get_projects_projects_get")
143+
@patch("galileo.prompts.get_project_templates_projects_project_id_templates_get")
144+
def test_get_prompt_template_not_found(list_prompt_templates_mock: Mock, get_projects_projects_get_mock: Mock):
145+
"""Test get_prompt_template when template doesn't exist."""
146+
list_prompt_templates_mock.sync.return_value = [prompt_template()] # Return a different template
147+
get_projects_projects_get_mock.sync_detailed.return_value = projects_response()
148+
149+
from galileo.prompts import get_prompt_template
150+
151+
template = get_prompt_template(name="nonexistent-template", project="andrii-new-project")
152+
153+
assert template is None
154+
list_prompt_templates_mock.sync.assert_called_once()
155+
get_projects_projects_get_mock.sync_detailed.assert_called_once()

0 commit comments

Comments
 (0)