Skip to content

Commit

Permalink
context var keys
Browse files Browse the repository at this point in the history
  • Loading branch information
lievan committed Oct 30, 2024
1 parent 60cde3d commit a2f05ca
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
2 changes: 2 additions & 0 deletions ddtrace/llmobs/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ def validate_prompt(prompt: dict) -> Dict[str, Union[str, dict, List[str]]]:
if not all(isinstance(k, str) for k in ctx_variable_keys):
raise TypeError("Prompt `context_variable_keys` must be a list of strings.")
validated_prompt[INTERNAL_CONTEXT_VARIABLE_KEYS] = ctx_variable_keys
else:
validated_prompt[INTERNAL_CONTEXT_VARIABLE_KEYS] = ["context"]
return validated_prompt


Expand Down
34 changes: 32 additions & 2 deletions tests/llmobs/test_llmobs_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -777,6 +777,28 @@ def test_annotate_prompt_dict(LLMObs):
"variables": {"var1": "var1", "var2": "var3"},
"version": "1.0.0",
"id": "test_prompt",
"_dd_context_variable_keys": ["context"],
}


def test_annotate_prompt_dict_with_context_var_keys(LLMObs):
with LLMObs.llm(model_name="test_model") as span:
LLMObs.annotate(
span=span,
prompt={
"template": "{var1} {var3}",
"variables": {"var1": "var1", "var2": "var3"},
"version": "1.0.0",
"id": "test_prompt",
"context_variable_keys": ["var1", "var2"],
},
)
assert json.loads(span.get_tag(INPUT_PROMPT)) == {
"template": "{var1} {var3}",
"variables": {"var1": "var1", "var2": "var3"},
"version": "1.0.0",
"id": "test_prompt",
"_dd_context_variable_keys": ["var1", "var2"],
}


Expand All @@ -789,13 +811,15 @@ def test_annotate_prompt_typed_dict(LLMObs):
variables={"var1": "var1", "var2": "var3"},
version="1.0.0",
id="test_prompt",
context_variable_keys=["var1", "var2"],
),
)
assert json.loads(span.get_tag(INPUT_PROMPT)) == {
"template": "{var1} {var3}",
"variables": {"var1": "var1", "var2": "var3"},
"version": "1.0.0",
"id": "test_prompt",
"_dd_context_variable_keys": ["var1", "var2"],
}


Expand Down Expand Up @@ -1769,7 +1793,10 @@ def test_annotation_context_modifies_span_tags(LLMObs):
def test_annotation_context_modifies_prompt(LLMObs):
with LLMObs.annotation_context(prompt={"template": "test_template"}):
with LLMObs.llm(name="test_agent", model_name="test") as span:
assert json.loads(span.get_tag(INPUT_PROMPT)) == {"template": "test_template"}
assert json.loads(span.get_tag(INPUT_PROMPT)) == {
"template": "test_template",
"_dd_context_variable_keys": ["context"],
}


def test_annotation_context_modifies_name(LLMObs):
Expand Down Expand Up @@ -1908,7 +1935,10 @@ async def test_annotation_context_async_modifies_span_tags(LLMObs):
async def test_annotation_context_async_modifies_prompt(LLMObs):
async with LLMObs.annotation_context(prompt={"template": "test_template"}):
with LLMObs.llm(name="test_agent", model_name="test") as span:
assert json.loads(span.get_tag(INPUT_PROMPT)) == {"template": "test_template"}
assert json.loads(span.get_tag(INPUT_PROMPT)) == {
"template": "test_template",
"_dd_context_variable_keys": ["context"],
}


async def test_annotation_context_async_modifies_name(LLMObs):
Expand Down

0 comments on commit a2f05ca

Please sign in to comment.