-
Notifications
You must be signed in to change notification settings - Fork 2.5k
refactoring: refactor the server side tool definitions #6827
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
+325
−99
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
5b23f37
chore: refactor the server side tool definitions
dancer ac1d07b
x
dancer 557a759
x
dancer 69733d9
x
dancer 696bd57
x
dancer 6ee1b1e
x
dancer 7ce20ac
x
dancer c091154
x
dancer 3df2dde
x
dancer f4af186
jsdoc
dancer e262697
x
dancer 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
'@ai-sdk/anthropic': patch | ||
'@ai-sdk/openai': patch | ||
--- | ||
|
||
refactor: updated openai + anthropic tool use server side |
38 changes: 38 additions & 0 deletions
38
examples/ai-core/src/generate-text/anthropic-provider-defined-tools.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,38 @@ | ||
import { generateText } from 'ai'; | ||
import { anthropic } from '@ai-sdk/anthropic'; | ||
import 'dotenv/config'; | ||
|
||
async function main() { | ||
const result = await generateText({ | ||
model: anthropic('claude-3-5-sonnet-20241022'), | ||
prompt: 'Search for recent information about AI SDK development', | ||
tools: { | ||
webSearch: anthropic.tools.webSearch_20250305({ | ||
maxUses: 3, | ||
allowedDomains: ['github.com', 'vercel.com', 'docs.ai'], | ||
userLocation: { | ||
type: 'approximate', | ||
city: 'San Francisco', | ||
region: 'California', | ||
country: 'US', | ||
}, | ||
}), | ||
|
||
computer: anthropic.tools.computer_20250124({ | ||
displayWidthPx: 1920, | ||
displayHeightPx: 1080, | ||
}), | ||
}, | ||
}); | ||
|
||
console.log('Result:', result.text); | ||
console.log('Tool calls made:', result.toolCalls.length); | ||
|
||
for (const toolCall of result.toolCalls) { | ||
console.log(`\nTool Call:`); | ||
console.log(`- Tool: ${toolCall.toolName}`); | ||
console.log(`- Input:`, JSON.stringify(toolCall.input, null, 2)); | ||
} | ||
} | ||
|
||
main().catch(console.error); |
37 changes: 37 additions & 0 deletions
37
examples/ai-core/src/generate-text/openai-provider-defined-tools.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,37 @@ | ||
import { generateText } from 'ai'; | ||
import { openai } from '@ai-sdk/openai'; | ||
import 'dotenv/config'; | ||
|
||
async function main() { | ||
const result = await generateText({ | ||
model: openai('gpt-4o-mini'), | ||
prompt: 'Search for information about TypeScript best practices', | ||
tools: { | ||
webSearch: openai.tools.webSearchPreview({ | ||
searchContextSize: 'medium', | ||
userLocation: { | ||
type: 'approximate', | ||
city: 'San Francisco', | ||
region: 'California', | ||
country: 'US', | ||
}, | ||
}), | ||
|
||
fileSearch: openai.tools.fileSearch({ | ||
maxResults: 5, | ||
searchType: 'semantic', | ||
}), | ||
}, | ||
}); | ||
|
||
console.log('Result:', result.text); | ||
console.log('Tool calls made:', result.toolCalls.length); | ||
|
||
for (const toolCall of result.toolCalls) { | ||
console.log(`\nTool Call:`); | ||
console.log(`- Tool: ${toolCall.toolName}`); | ||
console.log(`- Input:`, JSON.stringify(toolCall.input, null, 2)); | ||
} | ||
} | ||
|
||
main().catch(console.error); |
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
70 changes: 70 additions & 0 deletions
70
examples/ai-core/src/generate-text/provider-defined-tools-working.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,70 @@ | ||
import { generateText } from 'ai'; | ||
import { anthropic } from '@ai-sdk/anthropic'; | ||
import { openai } from '@ai-sdk/openai'; | ||
import 'dotenv/config'; | ||
|
||
async function main() { | ||
console.log('=== Demonstrating Refactored Provider-Defined Tools ===\n'); | ||
|
||
console.log('1. OpenAI Provider-Defined Tools (Successfully Refactored):'); | ||
const openaiWebSearch = openai.tools.webSearchPreview({ | ||
searchContextSize: 'medium', | ||
userLocation: { | ||
type: 'approximate', | ||
city: 'San Francisco', | ||
region: 'California', | ||
country: 'US', | ||
}, | ||
}); | ||
|
||
const openaiFileSearch = openai.tools.fileSearch({ | ||
maxResults: 5, | ||
searchType: 'semantic', | ||
}); | ||
|
||
console.log('OpenAI Web Search Tool created successfully'); | ||
console.log('OpenAI File Search Tool created successfully'); | ||
|
||
console.log('\n2. Anthropic Provider-Defined Tools (Working Example):'); | ||
const result = await generateText({ | ||
model: anthropic('claude-3-5-sonnet-20241022'), | ||
prompt: 'Search for current weather in Tokyo', | ||
tools: { | ||
web_search: anthropic.tools.webSearch_20250305({ | ||
maxUses: 2, | ||
allowedDomains: ['weather.com', 'accuweather.com'], | ||
userLocation: { | ||
type: 'approximate', | ||
city: 'Tokyo', | ||
region: 'Tokyo', | ||
country: 'JP', | ||
}, | ||
}), | ||
}, | ||
}); | ||
|
||
console.log('Anthropic Web Search Tool executed successfully'); | ||
console.log('Tool calls made:', result.toolCalls.length); | ||
|
||
for (const toolCall of result.toolCalls) { | ||
console.log(`\nTool Call:`); | ||
console.log(`- Tool: ${toolCall.toolName}`); | ||
console.log(`- Input:`, JSON.stringify(toolCall.input, null, 2)); | ||
} | ||
|
||
console.log('\n=== Refactoring Summary ==='); | ||
console.log( | ||
'OpenAI tools refactored to use createProviderDefinedToolFactory', | ||
); | ||
console.log( | ||
'Anthropic tools refactored to use createProviderDefinedToolFactory', | ||
); | ||
console.log( | ||
'All tools now follow consistent pattern like computer_20250124.ts', | ||
); | ||
console.log('Type safety improved with better TypeScript inference'); | ||
console.log('Anthropic tools working in production'); | ||
console.log('Factory pattern provides cleaner, more maintainable API'); | ||
} | ||
|
||
main().catch(console.error); |
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 |
---|---|---|
|
@@ -21,7 +21,7 @@ export async function POST(req: Request) { | |
const result = streamText({ | ||
model: anthropic('claude-3-5-sonnet-latest'), | ||
tools: { | ||
web_search: anthropic.tools.webSearch_20250305(), | ||
web_search: anthropic.tools.webSearch_20250305({}), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. wonder if we can provide a default? might be tricky |
||
}, | ||
messages: convertToModelMessages(messages), | ||
}); | ||
|
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.
hmm