Skip to content

Commit fc7a363

Browse files
committed
feedback
1 parent 39c0ae7 commit fc7a363

2 files changed

Lines changed: 39 additions & 16 deletions

File tree

langfuse/model.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -370,8 +370,8 @@ def __eq__(self, other):
370370

371371
def compile_with_placeholders(
372372
self,
373-
variables: Dict[str, str],
374373
placeholders: Dict[str, List[ChatMessageDict]],
374+
variables: Optional[Dict[str, str]] = None,
375375
) -> List[ChatMessageDict]:
376376
"""Compile chat prompt by first replacing placeholders, then expanding variables.
377377
@@ -382,22 +382,26 @@ def compile_with_placeholders(
382382
Returns:
383383
List[ChatMessageDict]: Compiled chat messages
384384
"""
385+
if variables is None:
386+
variables = {}
387+
385388
messages_with_placeholders_replaced: List[ChatMessageDict] = []
386389

387390
# Subsitute the placeholders for their supplied ChatMessages
388391
for item in self.prompt:
389392
if item["type"] == "placeholder" and item["name"] in placeholders:
390393
if (
391-
isinstance(placeholders[item["name"]], Iterable)
394+
isinstance(placeholders[item["name"]], List)
392395
and len(placeholders[item["name"]]) > 0
393396
):
394397
messages_with_placeholders_replaced.extend(
395398
placeholders[item["name"]]
396399
)
397400
else:
398-
raise ValueError(
401+
empty_placeholder_error = (
399402
f"The provided placeholder: {item['name']} is empty"
400403
)
404+
raise ValueError(empty_placeholder_error)
401405
elif item["type"] == "message":
402406
messages_with_placeholders_replaced.append(
403407
ChatMessageDict(
@@ -437,7 +441,7 @@ def get_langchain_prompt(self, **kwargs):
437441
self._get_langchain_prompt_string(
438442
TemplateParser.compile_template(msg["content"], kwargs)
439443
if kwargs
440-
else msg["content"]
444+
else msg["content"],
441445
),
442446
)
443447
for msg in self.prompt

tests/test_prompt.py

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -110,13 +110,13 @@ def test_create_chat_prompt_with_placeholders():
110110
second_prompt_client = langfuse.get_prompt(prompt_name, type="chat")
111111

112112
messages = second_prompt_client.compile_with_placeholders(
113-
variables={"role": "helpful", "task": "coding"},
114113
placeholders={
115114
"history": [
116115
{"role": "user", "content": "Example: {{task}}"},
117116
{"role": "assistant", "content": "Example response"},
118117
],
119118
},
119+
variables={"role": "helpful", "task": "coding"},
120120
)
121121

122122
# Create a test generation using compiled messages
@@ -205,7 +205,7 @@ def test_get_prompt_with_placeholders():
205205

206206

207207
@pytest.mark.parametrize(
208-
"variables,placeholders,expected_len,expected_contents",
208+
("variables", "placeholders", "expected_len", "expected_contents"),
209209
[
210210
# Variables only, no placeholders
211211
(
@@ -223,7 +223,24 @@ def test_get_prompt_with_placeholders():
223223
"examples": [
224224
{"role": "user", "content": "Example question"},
225225
{"role": "assistant", "content": "Example answer"},
226-
]
226+
],
227+
},
228+
4,
229+
[
230+
"You are a {{role}} assistant",
231+
"Example question",
232+
"Example answer",
233+
"Help me with {{task}}",
234+
],
235+
),
236+
# Placeholders only, variables None
237+
(
238+
None,
239+
{
240+
"examples": [
241+
{"role": "user", "content": "Example question"},
242+
{"role": "assistant", "content": "Example answer"},
243+
],
227244
},
228245
4,
229246
[
@@ -240,7 +257,7 @@ def test_get_prompt_with_placeholders():
240257
"examples": [
241258
{"role": "user", "content": "Show me {{task}}"},
242259
{"role": "assistant", "content": "Here's {{task}}"},
243-
]
260+
],
244261
},
245262
4,
246263
[
@@ -250,13 +267,14 @@ def test_get_prompt_with_placeholders():
250267
"Help me with coding",
251268
],
252269
),
253-
# Empty placeholder array
254-
(
255-
{"role": "helpful", "task": "coding"},
256-
{"examples": []},
257-
2,
258-
["You are a helpful assistant", "Help me with coding"],
259-
),
270+
# # Empty placeholder array
271+
# This is expected to fail! If the user provides a placeholder, it should contain an array
272+
# (
273+
# {"role": "helpful", "task": "coding"},
274+
# {"examples": []},
275+
# 2,
276+
# ["You are a helpful assistant", "Help me with coding"],
277+
# ),
260278
# Unused placeholders
261279
(
262280
{"role": "helpful", "task": "coding"},
@@ -288,7 +306,8 @@ def test_compile_with_placeholders(
288306
)
289307

290308
result = ChatPromptClient(mock_prompt).compile_with_placeholders(
291-
variables, placeholders
309+
placeholders,
310+
variables,
292311
)
293312

294313
assert len(result) == expected_len

0 commit comments

Comments
 (0)