🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.
TypeScript analyticsautogenevaluationlangchainlarge-language-modelsllama-indexllmllm-evaluationllm-observabilityllmopsmonitoringobservabilityopen-sourceopenaiplaygroundprompt-engineeringprompt-managementself-hostedycombinator
feat: map additional properties for otel llm spans (#5422)
Steffen Schmitzbf68a7edd522
diff --git a/web/src/__tests__/server/api/otel/otelMapping.servertest.ts b/web/src/__tests__/server/api/otel/otelMapping.servertest.tsindex 6e6efdae66..c29f14548e 100644--- a/web/src/__tests__/server/api/otel/otelMapping.servertest.ts+++ b/web/src/__tests__/server/api/otel/otelMapping.servertest.ts@@ -525,6 +525,60 @@ describe("OTel Resource Span Mapping", () => {entityAttributeValue: "Observing LLMs",},],+ [+ "#5412: should map input.value to input for smolagents",+ {+ entity: "observation",+ otelAttributeKey: "input.value",+ otelAttributeValue: {+ stringValue: JSON.stringify({+ task: "Play some chess",+ stream: false,+ }),+ },+ entityAttributeKey: "input",+ entityAttributeValue: JSON.stringify({+ task: "Play some chess",+ stream: false,+ }),+ },+ ],+ [+ "#5412: should map llm.token_count.completion to provided_usage_details.output",+ {+ entity: "observation",+ otelAttributeKey: "llm.token_count.completion",+ otelAttributeValue: {+ intValue: { low: 100, high: 0, unsigned: false },+ },+ entityAttributeKey: "usageDetails.output",+ entityAttributeValue: 100,+ },+ ],+ [+ "#5412: should map llm.token_count.total to provided_usage_details.total",+ {+ entity: "observation",+ otelAttributeKey: "llm.token_count.total",+ otelAttributeValue: {+ intValue: { low: 100, high: 0, unsigned: false },+ },+ entityAttributeKey: "usageDetails.total",+ entityAttributeValue: 100,+ },+ ],+ [+ "#5412: should map llm.invocation_parameters to modelParameters",+ {+ entity: "observation",+ otelAttributeKey: "llm.invocation_parameters",+ otelAttributeValue: {+ stringValue: '{"max_tokens": 4096}',+ },+ entityAttributeKey: "modelParameters.max_tokens",+ entityAttributeValue: 4096,+ },+ ],])("Attributes: %s",(diff --git a/web/src/features/otel/server/index.ts b/web/src/features/otel/server/index.tsindex 795f3fa04f..a7d17bebe8 100644--- a/web/src/features/otel/server/index.ts+++ b/web/src/features/otel/server/index.ts@@ -127,6 +127,13 @@ const extractInputAndOutput = (return { input, output };}+ // SmolAgents sets input.value and output.value+ input = attributes["input.value"];+ output = attributes["output.value"];+ if (input || output) {+ return { input, output };+ }+// TraceLoop uses attributes propertyconst inputAttributes = Object.keys(attributes).filter((key) =>key.startsWith("gen_ai.prompt"),@@ -181,6 +188,15 @@ const extractSessionId = (const extractModelParameters = (attributes: Record<string, unknown>,): Record<string, unknown> => {+ // If we get invocation parameters, we use them as they are+ if (attributes["llm.invocation_parameters"]) {+ try {+ return JSON.parse(attributes["llm.invocation_parameters"] as string);+ } catch (e) {+ // fallthrough+ }+ }+const modelParameters = Object.keys(attributes).filter((key) =>key.startsWith("gen_ai.request."),);@@ -194,7 +210,11 @@ const extractModelParameters = (const extractModelName = (attributes: Record<string, unknown>,): string | undefined => {- const modelNameKeys = ["gen_ai.request.model", "gen_ai.response.model"];+ const modelNameKeys = [+ "gen_ai.request.model",+ "gen_ai.response.model",+ "llm.model_name",+ ];for (const key of modelNameKeys) {if (attributes[key]) {return typeof attributes[key] === "string"@@ -208,7 +228,9 @@ const extractUsageDetails = (attributes: Record<string, unknown>,): Record<string, unknown> => {const usageDetails = Object.keys(attributes).filter(- (key) => key.startsWith("gen_ai.usage.") && key !== "gen_ai.usage.cost",+ (key) =>+ (key.startsWith("gen_ai.usage.") && key !== "gen_ai.usage.cost") ||+ key.startsWith("llm.token_count"),);const usageDetailKeyMapping: Record<string, string> = {prompt_tokens: "input",@@ -216,9 +238,13 @@ const extractUsageDetails = (total_tokens: "total",input_tokens: "input",output_tokens: "output",+ prompt: "input",+ completion: "output",};return usageDetails.reduce((acc: any, key) => {- const usageDetailKey = key.replace("gen_ai.usage.", "");+ const usageDetailKey = key+ .replace("gen_ai.usage.", "")+ .replace("llm.token_count.", "");const mappedUsageDetailKey =usageDetailKeyMapping[usageDetailKey] ?? usageDetailKey;acc[mappedUsageDetailKey] = attributes[key];@@ -317,9 +343,9 @@ export const convertOtelSpanToIngestionEvent = (...extractInputAndOutput(span?.events ?? [], attributes),};- // If the span has any gen_ai attributes, we consider it a generation- const isGeneration = Object.keys(attributes).some((key) =>- key.startsWith("gen_ai"),+ // If the span has any gen_ai or llm attributes, we consider it a generation+ const isGeneration = Object.keys(attributes).some(+ (key) => key.startsWith("gen_ai") || key.startsWith("llm"),);events.push({