diff --git a/content/en/llm_observability/instrumentation/sdk.md b/content/en/llm_observability/instrumentation/sdk.md index 8eacb204a7466..8993274014ceb 100644 --- a/content/en/llm_observability/instrumentation/sdk.md +++ b/content/en/llm_observability/instrumentation/sdk.md @@ -1230,7 +1230,7 @@ public class MyJavaClass { LLMObsSpan taskSpan = LLMObs.startTaskSpan("preprocessDocument", null, "session-141"); ... // preprocess document for data extraction taskSpan.annotateIO(...); // record the input and output - taskSpan.finish(); + taskSpan.finish(); } public String extractData(String document) { @@ -1740,7 +1740,7 @@ public class MyJavaClass { {{< tabs >}} {{% tab "Python" %}} -The SDK's `LLMObs.annotate_context()` method returns a context manager that can be used to modify all auto-instrumented spans started while the annotation context is active. +The SDK's `LLMObs.annotation_context()` method returns a context manager that can be used to modify all auto-instrumented spans started while the annotation context is active. The `LLMObs.annotation_context()` method accepts the following arguments: @@ -1756,7 +1756,7 @@ The `LLMObs.annotation_context()` method accepts the following arguments: `tags` : optional - _dictionary_ -
A dictionary of JSON serializable key-value pairs that users can add as tags on the span. Example keys: `session`, `env`, `system`, and `version`. For more information about tags, see [Getting Started with Tags][1]. +
A dictionary of JSON serializable key-value pairs that users can add as tags on the span. Example keys: `session`, `env`, `system`, and `version`. For more information about tags, see [Getting Started with Tags](/getting_started/tagging/). {{% /collapse-content %}} @@ -1788,7 +1788,46 @@ def rag_workflow(user_question): {{< /code-block >}} -[1]: /getting_started/tagging/ +{{% /tab %}} + +{{% tab "Node.js" %}} + +The SDK's `llmobs.annotationContext()` accepts a callback function that can be used to modify all auto-instrumented spans started while inside the scope of the callback function. + +The `llmobs.annotationContext()` method accepts the following options on the first argument: + +{{% collapse-content title="Options" level="h4" expanded=false id="annotating-autoinstrumented-span-arguments" %}} + +`name` +: optional - _str_ +
Name that overrides the span name for any auto-instrumented spans that are started within the annotation context. + +`tags` +: optional - _object_ +
An object of JSON serializable key-value pairs that users can add as tags on the span. Example keys: `session`, `env`, `system`, and `version`. For more information about tags, see [Getting Started with Tags](/getting_started/tagging/). + +{{% /collapse-content %}} + +#### Example + +{{< code-block lang="javascript" >}} +const { llmobs } = require('dd-trace'); + +function ragWorkflow(userQuestion) { + const contextStr = retrieveDocuments(userQuestion).join(" "); + + const completion = await llmobs.annotationContext({ + tags: { + retrieval_strategy: "semantic_similarity" + }, + name: "augmented_generation" + }, async () => { + const completion = await openai_client.chat.completions.create(...); + return completion.choices[0].message.content; + }); +} + +{{< /code-block >}} {{% /tab %}} {{< /tabs >}} @@ -2161,6 +2200,32 @@ function redactProcessor(span) { llmobs.registerProcessor(redactProcessor) {{< /code-block >}} +### Example: conditional modification with auto-instrumentation + +When using auto instrumentation, the span is not always contextually accessible. To conditionally modify the inputs and outputs on auto-instrumented spans, `llmobs.annotationContext()` can be used in addition to a span processor. + +{{< code-block lang="javascript" >}} +const { llmobs } = require('dd-trace'); + +function redactProcessor(span) { + if (span.getTag("no_input") == "true") { + for (const message of span.input) { + message.content = ""; + } + } + + return span; +} + +llmobs.registerProcessor(redactProcessor); + +async function callOpenai() { + await llmobs.annotationContext({ tags: { no_input: "true" } }, async () => { + // make call to openai + }); +} +{{< /code-block >}} + ### Example: preventing spans from being emitted {{< code-block lang="javascript" >}}