-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
feat(core): Instrument langgraph createReactAgent #20344
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
Open
andreiborza
wants to merge
1
commit into
develop
Choose a base branch
from
ab/langgraph-agents
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
65 changes: 65 additions & 0 deletions
65
dev-packages/node-integration-tests/suites/tracing/langgraph/agent-scenario.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,65 @@ | ||
| import { ChatAnthropic } from '@langchain/anthropic'; | ||
| import { HumanMessage, SystemMessage } from '@langchain/core/messages'; | ||
| import { createReactAgent } from '@langchain/langgraph/prebuilt'; | ||
| import * as Sentry from '@sentry/node'; | ||
| import express from 'express'; | ||
|
|
||
| function startMockAnthropicServer() { | ||
| const app = express(); | ||
| app.use(express.json()); | ||
|
|
||
| app.post('/v1/messages', (req, res) => { | ||
| const model = req.body.model; | ||
|
|
||
| res.json({ | ||
| id: 'msg_react_agent_123', | ||
| type: 'message', | ||
| role: 'assistant', | ||
| content: [ | ||
| { | ||
| type: 'text', | ||
| text: 'Paris is the capital of France.', | ||
| }, | ||
| ], | ||
| model: model, | ||
| stop_reason: 'end_turn', | ||
| stop_sequence: null, | ||
| usage: { | ||
| input_tokens: 20, | ||
| output_tokens: 10, | ||
| }, | ||
| }); | ||
| }); | ||
|
|
||
| return new Promise(resolve => { | ||
| const server = app.listen(0, () => { | ||
| resolve(server); | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| async function run() { | ||
| const server = await startMockAnthropicServer(); | ||
| const baseUrl = `http://localhost:${server.address().port}`; | ||
|
|
||
| await Sentry.startSpan({ op: 'function', name: 'main' }, async () => { | ||
| const llm = new ChatAnthropic({ | ||
| model: 'claude-3-5-sonnet-20241022', | ||
| apiKey: 'mock-api-key', | ||
| clientOptions: { | ||
| baseURL: baseUrl, | ||
| }, | ||
| }); | ||
|
|
||
| const agent = createReactAgent({ llm, tools: [], name: 'helpful_assistant' }); | ||
|
|
||
| await agent.invoke({ | ||
| messages: [new SystemMessage('You are a helpful assistant.'), new HumanMessage('What is the capital of France?')], | ||
| }); | ||
| }); | ||
|
|
||
| await Sentry.flush(2000); | ||
| server.close(); | ||
| } | ||
|
|
||
| run(); |
122 changes: 122 additions & 0 deletions
122
dev-packages/node-integration-tests/suites/tracing/langgraph/agent-tools-scenario.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,122 @@ | ||
| import { tool } from '@langchain/core/tools'; | ||
| import { ChatAnthropic } from '@langchain/anthropic'; | ||
| import { createReactAgent } from '@langchain/langgraph/prebuilt'; | ||
| import { HumanMessage } from '@langchain/core/messages'; | ||
| import * as Sentry from '@sentry/node'; | ||
| import express from 'express'; | ||
| import { z } from 'zod'; | ||
|
|
||
| let callCount = 0; | ||
|
|
||
| function startMockAnthropicServer() { | ||
| const app = express(); | ||
| app.use(express.json()); | ||
|
|
||
| app.post('/v1/messages', (req, res) => { | ||
| callCount++; | ||
| const model = req.body.model; | ||
|
|
||
| if (callCount === 1) { | ||
| // First call: model decides to call the "add" tool | ||
| res.json({ | ||
| id: 'msg_1', | ||
| type: 'message', | ||
| role: 'assistant', | ||
| content: [ | ||
| { | ||
| type: 'tool_use', | ||
| id: 'toolu_add_1', | ||
| name: 'add', | ||
| input: { a: 3, b: 5 }, | ||
| }, | ||
| ], | ||
| model: model, | ||
| stop_reason: 'tool_use', | ||
| usage: { input_tokens: 20, output_tokens: 10 }, | ||
| }); | ||
| } else if (callCount === 2) { | ||
| // Second call: model sees add result=8, calls "multiply" | ||
| res.json({ | ||
| id: 'msg_2', | ||
| type: 'message', | ||
| role: 'assistant', | ||
| content: [ | ||
| { | ||
| type: 'tool_use', | ||
| id: 'toolu_mul_1', | ||
| name: 'multiply', | ||
| input: { a: 8, b: 4 }, | ||
| }, | ||
| ], | ||
| model: model, | ||
| stop_reason: 'tool_use', | ||
| usage: { input_tokens: 30, output_tokens: 10 }, | ||
| }); | ||
| } else { | ||
| // Third call: model returns final answer | ||
| res.json({ | ||
| id: 'msg_3', | ||
| type: 'message', | ||
| role: 'assistant', | ||
| content: [{ type: 'text', text: 'The result is 32.' }], | ||
| model: model, | ||
| stop_reason: 'end_turn', | ||
| usage: { input_tokens: 40, output_tokens: 10 }, | ||
| }); | ||
| } | ||
| }); | ||
|
|
||
| return new Promise(resolve => { | ||
| const server = app.listen(0, () => resolve(server)); | ||
| }); | ||
| } | ||
|
|
||
| async function run() { | ||
| const server = await startMockAnthropicServer(); | ||
| const baseUrl = `http://localhost:${server.address().port}`; | ||
|
|
||
| await Sentry.startSpan({ op: 'function', name: 'main' }, async () => { | ||
| const llm = new ChatAnthropic({ | ||
| model: 'claude-3-5-sonnet-20241022', | ||
| apiKey: 'mock-api-key', | ||
| clientOptions: { baseURL: baseUrl }, | ||
| }); | ||
|
|
||
| const addTool = tool( | ||
| async ({ a, b }) => { | ||
| return String(a + b); | ||
| }, | ||
| { | ||
| name: 'add', | ||
| description: 'Add two numbers', | ||
| schema: z.object({ a: z.number(), b: z.number() }), | ||
| }, | ||
| ); | ||
|
|
||
| const multiplyTool = tool( | ||
| async ({ a, b }) => { | ||
| return String(a * b); | ||
| }, | ||
| { | ||
| name: 'multiply', | ||
| description: 'Multiply two numbers', | ||
| schema: z.object({ a: z.number(), b: z.number() }), | ||
| }, | ||
| ); | ||
|
|
||
| const agent = createReactAgent({ | ||
| llm, | ||
| tools: [addTool, multiplyTool], | ||
| name: 'math_assistant', | ||
| }); | ||
|
|
||
| await agent.invoke({ | ||
| messages: [new HumanMessage('Calculate (3 + 5) * 4')], | ||
| }); | ||
| }); | ||
|
|
||
| await Sentry.flush(2000); | ||
| server.close(); | ||
| } | ||
|
|
||
| run(); |
17 changes: 17 additions & 0 deletions
17
dev-packages/node-integration-tests/suites/tracing/langgraph/instrument-agent.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,17 @@ | ||
| import * as Sentry from '@sentry/node'; | ||
| import { loggingTransport } from '@sentry-internal/node-integration-tests'; | ||
|
|
||
| Sentry.init({ | ||
| dsn: 'https://public@dsn.ingest.sentry.io/1337', | ||
| release: '1.0', | ||
| tracesSampleRate: 1.0, | ||
| sendDefaultPii: true, | ||
| transport: loggingTransport, | ||
| beforeSendTransaction: event => { | ||
| // Filter out mock express server transactions | ||
| if (event.transaction && event.transaction.includes('/v1/messages')) { | ||
| return null; | ||
| } | ||
| return event; | ||
| }, | ||
| }); |
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
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.
l: should we introduce a constant for this?