-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat(core): Add tool calls attributes for Anthropic AI #17478
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
5 commits
Select commit
Hold shift + click to select a range
8c194ca
feat(core): Instrument tool calls for Anthropic AI
RulaKhaled c4bf630
Update packages/core/src/utils/anthropic-ai/streaming.ts
RulaKhaled 4603f87
fix lint issue
RulaKhaled 5ce395e
lint yelling
RulaKhaled c8f44a4
Merge branch 'develop' into tool-calls-anthropic
RulaKhaled 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
112 changes: 112 additions & 0 deletions
112
dev-packages/node-integration-tests/suites/tracing/anthropic/scenario-stream-tools.mjs
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,112 @@ | ||
import { instrumentAnthropicAiClient } from '@sentry/core'; | ||
import * as Sentry from '@sentry/node'; | ||
|
||
function createMockStreamEvents(model = 'claude-3-haiku-20240307') { | ||
async function* generator() { | ||
// initial message metadata with id/model and input tokens | ||
yield { | ||
type: 'content_block_start', | ||
message: { | ||
id: 'msg_stream_tool_1', | ||
type: 'message', | ||
role: 'assistant', | ||
model, | ||
content: [], | ||
stop_reason: 'end_turn', | ||
usage: { input_tokens: 11 }, | ||
}, | ||
}; | ||
|
||
// streamed text | ||
yield { type: 'content_block_delta', delta: { text: 'Starting tool...' } }; | ||
|
||
// tool_use streamed via partial json | ||
yield { | ||
type: 'content_block_start', | ||
index: 0, | ||
content_block: { type: 'tool_use', id: 'tool_weather_2', name: 'weather' }, | ||
}; | ||
yield { type: 'content_block_delta', index: 0, delta: { partial_json: '{"city":' } }; | ||
yield { type: 'content_block_delta', index: 0, delta: { partial_json: '"Paris"}' } }; | ||
yield { type: 'content_block_stop', index: 0 }; | ||
|
||
// more text | ||
yield { type: 'content_block_delta', delta: { text: 'Done.' } }; | ||
|
||
// final usage | ||
yield { type: 'message_delta', usage: { output_tokens: 9 } }; | ||
} | ||
return generator(); | ||
} | ||
|
||
class MockAnthropic { | ||
constructor(config) { | ||
this.apiKey = config.apiKey; | ||
this.messages = { | ||
create: this._messagesCreate.bind(this), | ||
stream: this._messagesStream.bind(this), | ||
}; | ||
} | ||
|
||
async _messagesCreate(params) { | ||
await new Promise(resolve => setTimeout(resolve, 5)); | ||
if (params?.stream) { | ||
return createMockStreamEvents(params.model); | ||
} | ||
return { | ||
id: 'msg_mock_no_stream', | ||
type: 'message', | ||
model: params.model, | ||
role: 'assistant', | ||
content: [{ type: 'text', text: 'No stream' }], | ||
usage: { input_tokens: 2, output_tokens: 3 }, | ||
}; | ||
} | ||
|
||
async _messagesStream(params) { | ||
await new Promise(resolve => setTimeout(resolve, 5)); | ||
return createMockStreamEvents(params?.model); | ||
} | ||
} | ||
|
||
async function run() { | ||
await Sentry.startSpan({ op: 'function', name: 'main' }, async () => { | ||
const mockClient = new MockAnthropic({ apiKey: 'mock-api-key' }); | ||
const client = instrumentAnthropicAiClient(mockClient); | ||
|
||
// stream via create(stream:true) | ||
const stream1 = await client.messages.create({ | ||
model: 'claude-3-haiku-20240307', | ||
messages: [{ role: 'user', content: 'Need the weather' }], | ||
tools: [ | ||
{ | ||
name: 'weather', | ||
description: 'Get weather', | ||
input_schema: { type: 'object', properties: { city: { type: 'string' } }, required: ['city'] }, | ||
}, | ||
], | ||
stream: true, | ||
}); | ||
for await (const _ of stream1) { | ||
void _; | ||
} | ||
|
||
// stream via messages.stream | ||
const stream2 = await client.messages.stream({ | ||
model: 'claude-3-haiku-20240307', | ||
messages: [{ role: 'user', content: 'Need the weather' }], | ||
tools: [ | ||
{ | ||
name: 'weather', | ||
description: 'Get weather', | ||
input_schema: { type: 'object', properties: { city: { type: 'string' } }, required: ['city'] }, | ||
}, | ||
], | ||
}); | ||
for await (const _ of stream2) { | ||
void _; | ||
} | ||
}); | ||
} | ||
|
||
run(); |
64 changes: 64 additions & 0 deletions
64
dev-packages/node-integration-tests/suites/tracing/anthropic/scenario-tools.mjs
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,64 @@ | ||
import { instrumentAnthropicAiClient } from '@sentry/core'; | ||
import * as Sentry from '@sentry/node'; | ||
|
||
class MockAnthropic { | ||
constructor(config) { | ||
this.apiKey = config.apiKey; | ||
|
||
this.messages = { | ||
create: this._messagesCreate.bind(this), | ||
}; | ||
} | ||
|
||
async _messagesCreate(params) { | ||
await new Promise(resolve => setTimeout(resolve, 5)); | ||
|
||
return { | ||
id: 'msg_mock_tool_1', | ||
type: 'message', | ||
model: params.model, | ||
role: 'assistant', | ||
content: [ | ||
{ type: 'text', text: 'Let me check the weather.' }, | ||
{ | ||
type: 'tool_use', | ||
id: 'tool_weather_1', | ||
name: 'weather', | ||
input: { city: 'Paris' }, | ||
}, | ||
{ type: 'text', text: 'It is sunny.' }, | ||
], | ||
stop_reason: 'end_turn', | ||
stop_sequence: null, | ||
usage: { | ||
input_tokens: 5, | ||
output_tokens: 7, | ||
}, | ||
}; | ||
} | ||
} | ||
|
||
async function run() { | ||
await Sentry.startSpan({ op: 'function', name: 'main' }, async () => { | ||
const mockClient = new MockAnthropic({ apiKey: 'mock-api-key' }); | ||
const client = instrumentAnthropicAiClient(mockClient); | ||
|
||
await client.messages.create({ | ||
model: 'claude-3-haiku-20240307', | ||
messages: [{ role: 'user', content: 'What is the weather?' }], | ||
tools: [ | ||
{ | ||
name: 'weather', | ||
description: 'Get the weather by city', | ||
input_schema: { | ||
type: 'object', | ||
properties: { city: { type: 'string' } }, | ||
required: ['city'], | ||
}, | ||
}, | ||
], | ||
}); | ||
}); | ||
} | ||
|
||
run(); |
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
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.
Uh oh!
There was an error while loading. Please reload this page.