-
Notifications
You must be signed in to change notification settings - Fork 67
fix(ai-sdk): model provider attribute transformation for openai #609
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
104 changes: 104 additions & 0 deletions
104
packages/traceloop-sdk/src/lib/tracing/ai-sdk-transformations.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| import { ReadableSpan } from "@opentelemetry/sdk-trace-node"; | ||
| import { SpanAttributes } from "@traceloop/ai-semantic-conventions"; | ||
|
|
||
| const AI_GENERATE_TEXT_DO_GENERATE = "ai.generateText.doGenerate"; | ||
| const AI_STREAM_TEXT_DO_STREAM = "ai.streamText.doStream"; | ||
| const AI_RESPONSE_TEXT = "ai.response.text"; | ||
| const AI_PROMPT_MESSAGES = "ai.prompt.messages"; | ||
| const AI_USAGE_PROMPT_TOKENS = "ai.usage.promptTokens"; | ||
| const AI_USAGE_COMPLETION_TOKENS = "ai.usage.completionTokens"; | ||
| const AI_MODEL_PROVIDER = "ai.model.provider"; | ||
|
|
||
| export const transformAiSdkSpanName = (span: ReadableSpan): void => { | ||
| const nameMap: Record<string, string> = { | ||
| [AI_GENERATE_TEXT_DO_GENERATE]: "ai.generateText.generate", | ||
| [AI_STREAM_TEXT_DO_STREAM]: "ai.streamText.stream", | ||
| }; | ||
|
|
||
| if (span.name in nameMap) { | ||
| // Unfortunately, the span name is not writable as this is not the intended behavior | ||
| // but it is a workaround to set the correct span name | ||
| (span as any).name = nameMap[span.name]; | ||
| } | ||
| }; | ||
|
|
||
| export const transformResponseText = (attributes: Record<string, any>): void => { | ||
| if (AI_RESPONSE_TEXT in attributes) { | ||
| attributes[`${SpanAttributes.LLM_COMPLETIONS}.0.content`] = | ||
| attributes[AI_RESPONSE_TEXT]; | ||
| attributes[`${SpanAttributes.LLM_COMPLETIONS}.0.role`] = "assistant"; | ||
| delete attributes[AI_RESPONSE_TEXT]; | ||
| } | ||
| }; | ||
|
|
||
| export const transformPromptMessages = (attributes: Record<string, any>): void => { | ||
| if (AI_PROMPT_MESSAGES in attributes) { | ||
| try { | ||
| const messages = JSON.parse(attributes[AI_PROMPT_MESSAGES] as string); | ||
| messages.forEach( | ||
| (msg: { role: string; content: any }, index: number) => { | ||
| attributes[`${SpanAttributes.LLM_PROMPTS}.${index}.content`] = | ||
| typeof msg.content === "string" | ||
| ? msg.content | ||
| : JSON.stringify(msg.content); | ||
| attributes[`${SpanAttributes.LLM_PROMPTS}.${index}.role`] = msg.role; | ||
| }, | ||
| ); | ||
| delete attributes[AI_PROMPT_MESSAGES]; | ||
| } catch { | ||
| // Skip if JSON parsing fails | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| export const transformPromptTokens = (attributes: Record<string, any>): void => { | ||
| if (AI_USAGE_PROMPT_TOKENS in attributes) { | ||
| attributes[`${SpanAttributes.LLM_USAGE_PROMPT_TOKENS}`] = | ||
| attributes[AI_USAGE_PROMPT_TOKENS]; | ||
| delete attributes[AI_USAGE_PROMPT_TOKENS]; | ||
| } | ||
| }; | ||
|
|
||
| export const transformCompletionTokens = (attributes: Record<string, any>): void => { | ||
| if (AI_USAGE_COMPLETION_TOKENS in attributes) { | ||
| attributes[`${SpanAttributes.LLM_USAGE_COMPLETION_TOKENS}`] = | ||
| attributes[AI_USAGE_COMPLETION_TOKENS]; | ||
| delete attributes[AI_USAGE_COMPLETION_TOKENS]; | ||
| } | ||
| }; | ||
|
|
||
| export const calculateTotalTokens = (attributes: Record<string, any>): void => { | ||
| const promptTokens = attributes[`${SpanAttributes.LLM_USAGE_PROMPT_TOKENS}`]; | ||
| const completionTokens = attributes[`${SpanAttributes.LLM_USAGE_COMPLETION_TOKENS}`]; | ||
|
|
||
| if (promptTokens && completionTokens) { | ||
| attributes[`${SpanAttributes.LLM_USAGE_TOTAL_TOKENS}`] = | ||
| Number(promptTokens) + Number(completionTokens); | ||
| } | ||
| }; | ||
|
|
||
| export const transformVendor = (attributes: Record<string, any>): void => { | ||
| if (AI_MODEL_PROVIDER in attributes) { | ||
| const vendor = attributes[AI_MODEL_PROVIDER]; | ||
| if (vendor && vendor.startsWith("openai")) { | ||
| attributes[SpanAttributes.LLM_SYSTEM] = "OpenAI"; | ||
| } else { | ||
| attributes[SpanAttributes.LLM_SYSTEM] = vendor; | ||
| } | ||
| delete attributes[AI_MODEL_PROVIDER]; | ||
| } | ||
| }; | ||
|
|
||
| export const transformAiSdkAttributes = (attributes: Record<string, any>): void => { | ||
| transformResponseText(attributes); | ||
| transformPromptMessages(attributes); | ||
| transformPromptTokens(attributes); | ||
| transformCompletionTokens(attributes); | ||
| calculateTotalTokens(attributes); | ||
| transformVendor(attributes); | ||
| }; | ||
|
|
||
| export const transformAiSdkSpan = (span: ReadableSpan): void => { | ||
| transformAiSdkSpanName(span); | ||
| transformAiSdkAttributes(span.attributes); | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Bug: The condition in calculateTotalTokens uses 'if (promptTokens && completionTokens)' which fails when token values are 0 (a valid case). Consider checking for undefined (e.g., promptTokens != null && completionTokens != null) instead.