-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
feat(vercel-ai): Add rerank support and fix token attribute mapping #19144
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 all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
85e26d0
feat(core): Add AI SDK rerank support and fix token attribute mapping
sergical dadcb6a
feat(node): Add rerank to Vercel AI instrumented methods
sergical 6055a18
fix(node): Add null check for Vercel AI methods before patching
sergical a55c7fa
chore: fix formatting
sergical 82838a3
fix(e2e): Use V3 mock models for AI SDK v6
sergical af6061a
fix(e2e): Update AI SDK v6 mock models to correct V3 API format
sergical 1432759
test(e2e): Remove nextjs-15-ai-v6 test due to AI SDK mock bug
sergical 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
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
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
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
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
75 changes: 75 additions & 0 deletions
75
packages/core/test/lib/tracing/vercel-ai-parent-tokens.test.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,75 @@ | ||
| import { describe, expect, it } from 'vitest'; | ||
| import { addVercelAiProcessors } from '../../../src/tracing/vercel-ai'; | ||
| import type { SpanJSON } from '../../../src/types-hoist/span'; | ||
| import { getDefaultTestClientOptions, TestClient } from '../../mocks/client'; | ||
|
|
||
| describe('vercel-ai parent span token attributes', () => { | ||
| it('should map ai.usage.inputTokens to gen_ai.usage.input_tokens', () => { | ||
| const options = getDefaultTestClientOptions({ tracesSampleRate: 1.0 }); | ||
| const client = new TestClient(options); | ||
| client.init(); | ||
| addVercelAiProcessors(client); | ||
|
|
||
| const mockSpan: SpanJSON = { | ||
| description: 'ai.streamText', | ||
| span_id: 'test-span-id', | ||
| trace_id: 'test-trace-id', | ||
| start_timestamp: 1000, | ||
| timestamp: 2000, | ||
| origin: 'auto.vercelai.otel', | ||
| data: { | ||
| 'ai.usage.inputTokens': 100, | ||
| 'ai.usage.outputTokens': 50, | ||
| }, | ||
| }; | ||
|
|
||
| const event = { | ||
| type: 'transaction' as const, | ||
| spans: [mockSpan], | ||
| }; | ||
|
|
||
| const eventProcessor = client['_eventProcessors'].find(processor => processor.id === 'VercelAiEventProcessor'); | ||
| expect(eventProcessor).toBeDefined(); | ||
|
|
||
| const processedEvent = eventProcessor!(event, {}); | ||
|
|
||
| expect(processedEvent?.spans?.[0]?.data?.['gen_ai.usage.input_tokens']).toBe(100); | ||
| expect(processedEvent?.spans?.[0]?.data?.['gen_ai.usage.output_tokens']).toBe(50); | ||
| // Original attributes should be renamed to vercel.ai.* namespace | ||
| expect(processedEvent?.spans?.[0]?.data?.['ai.usage.inputTokens']).toBeUndefined(); | ||
| expect(processedEvent?.spans?.[0]?.data?.['ai.usage.outputTokens']).toBeUndefined(); | ||
| }); | ||
|
|
||
| it('should map ai.response.avgOutputTokensPerSecond to ai.response.avgCompletionTokensPerSecond', () => { | ||
| const options = getDefaultTestClientOptions({ tracesSampleRate: 1.0 }); | ||
| const client = new TestClient(options); | ||
| client.init(); | ||
| addVercelAiProcessors(client); | ||
|
|
||
| const mockSpan: SpanJSON = { | ||
| description: 'ai.streamText.doStream', | ||
| span_id: 'test-span-id', | ||
| trace_id: 'test-trace-id', | ||
| start_timestamp: 1000, | ||
| timestamp: 2000, | ||
| origin: 'auto.vercelai.otel', | ||
| data: { | ||
| 'ai.response.avgOutputTokensPerSecond': 25.5, | ||
| }, | ||
| }; | ||
|
|
||
| const event = { | ||
| type: 'transaction' as const, | ||
| spans: [mockSpan], | ||
| }; | ||
|
|
||
| const eventProcessor = client['_eventProcessors'].find(processor => processor.id === 'VercelAiEventProcessor'); | ||
| expect(eventProcessor).toBeDefined(); | ||
|
|
||
| const processedEvent = eventProcessor!(event, {}); | ||
|
|
||
| // Should be renamed to match the expected attribute name | ||
| expect(processedEvent?.spans?.[0]?.data?.['vercel.ai.response.avgCompletionTokensPerSecond']).toBe(25.5); | ||
| expect(processedEvent?.spans?.[0]?.data?.['ai.response.avgOutputTokensPerSecond']).toBeUndefined(); | ||
| }); | ||
| }); |
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,14 @@ | ||
| import { describe, expect, it } from 'vitest'; | ||
| import { getSpanOpFromName } from '../../../src/tracing/vercel-ai/utils'; | ||
|
|
||
| describe('vercel-ai rerank support', () => { | ||
| describe('getSpanOpFromName', () => { | ||
| it('should map ai.rerank to gen_ai.invoke_agent', () => { | ||
| expect(getSpanOpFromName('ai.rerank')).toBe('gen_ai.invoke_agent'); | ||
| }); | ||
|
|
||
| it('should map ai.rerank.doRerank to gen_ai.rerank', () => { | ||
| expect(getSpanOpFromName('ai.rerank.doRerank')).toBe('gen_ai.rerank'); | ||
| }); | ||
| }); | ||
| }); | ||
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.
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.
Uh oh!
There was an error while loading. Please reload this page.