-
Notifications
You must be signed in to change notification settings - Fork 408
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(llmobs): add prompt and name arguments to annotation context #10711
base: main
Are you sure you want to change the base?
Changes from all commits
d3c9c59
e936ea0
3fe76e7
75f8a67
a1ef17a
7f75e2f
a5018b7
36fe0e1
066cf7d
ac5ab80
f699787
929ae61
0f7525d
a6ae5a9
dead1c6
9f0d345
2fd304a
9a74231
dd1023c
c6212d2
d89c3f5
ed7e205
a7f160a
235f3fd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -230,16 +230,25 @@ def disable(cls) -> None: | |
log.debug("%s disabled", cls.__name__) | ||
|
||
@classmethod | ||
def annotation_context(cls, tags: Optional[Dict[str, Any]] = None) -> AnnotationContext: | ||
def annotation_context( | ||
cls, tags: Optional[Dict[str, Any]] = None, prompt: Optional[dict] = None, name: Optional[str] = None | ||
) -> AnnotationContext: | ||
""" | ||
Sets specified attributes on all LLMObs spans created while the returned AnnotationContext is active. | ||
Do not use nested annotation contexts to override the same tags since the order in which annotations | ||
Do not use nested annotation contexts to override the same attributes since the order in which annotations | ||
are applied is non-deterministic. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is it non-deterministic? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We register the annotation function as a hook on span start. Because these hooks are stored as a set, the order in which these hooks are called is non-deterministic |
||
|
||
:param tags: Dictionary of JSON serializable key-value tag pairs to set or update on the LLMObs span | ||
regarding the span's context. | ||
:param prompt: A dictionary that represents the prompt used for an LLM call in the following form: | ||
`{"template": "...", "id": "...", "version": "...", "variables": {"variable_1": "...", ...}}`. | ||
Can also be set using the `ddtrace.llmobs.utils.Prompt` constructor class. | ||
This argument is only applicable to LLM spans. | ||
:param name: Set to override the span name for any spans annotated within the returned context. | ||
""" | ||
return AnnotationContext(cls._instance.tracer, lambda span: cls.annotate(span, tags=tags)) | ||
return AnnotationContext( | ||
cls._instance.tracer, lambda span: cls.annotate(span, tags=tags, prompt=prompt, _name=name) | ||
) | ||
|
||
@classmethod | ||
def flush(cls) -> None: | ||
|
@@ -483,6 +492,7 @@ def annotate( | |
metadata: Optional[Dict[str, Any]] = None, | ||
metrics: Optional[Dict[str, Any]] = None, | ||
tags: Optional[Dict[str, Any]] = None, | ||
_name: Optional[str] = None, | ||
) -> None: | ||
""" | ||
Sets parameters, inputs, outputs, tags, and metrics as provided for a given LLMObs span. | ||
|
@@ -491,7 +501,9 @@ def annotate( | |
:param Span span: Span to annotate. If no span is provided, the current active span will be used. | ||
Must be an LLMObs-type span, i.e. generated by the LLMObs SDK. | ||
:param prompt: A dictionary that represents the prompt used for an LLM call in the following form: | ||
{"template": "...", "id": "...", "version": "...", "variables": {"variable_1": "value_1", ...}}. | ||
`{"template": "...", "id": "...", "version": "...", "variables": {"variable_1": "...", ...}}`. | ||
Can also be set using the `ddtrace.llmobs.utils.Prompt` constructor class. | ||
This argument is only applicable to LLM spans. | ||
:param input_data: A single input string, dictionary, or a list of dictionaries based on the span kind: | ||
- llm spans: accepts a string, or a dictionary of form {"content": "...", "role": "..."}, | ||
or a list of dictionaries with the same signature. | ||
|
@@ -534,15 +546,13 @@ def annotate( | |
if parameters is not None: | ||
log.warning("Setting parameters is deprecated, please set parameters and other metadata as tags instead.") | ||
cls._tag_params(span, parameters) | ||
if _name is not None: | ||
span.name = _name | ||
if prompt is not None: | ||
cls._tag_prompt(span, prompt) | ||
if not span_kind: | ||
log.debug("Span kind not specified, skipping annotation for input/output data") | ||
return | ||
if prompt is not None: | ||
if span_kind == "llm": | ||
cls._tag_prompt(span, prompt) | ||
else: | ||
log.warning("Annotating prompts are only supported for LLM span kinds.") | ||
|
||
if input_data or output_data: | ||
if span_kind == "llm": | ||
cls._tag_llm_io(span, input_messages=input_data, output_messages=output_data) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
--- | ||
features: | ||
- | | ||
LLM Observability: Introduces `prompt` and `name` arguments to ``LLMObs.annotation_context`` to support setting an integration generated span's name and `prompt` field. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's make documentation for this in our public docs, probably next to
Annotate a span
.