-
Notifications
You must be signed in to change notification settings - Fork 3
feat(search-tools): LLM-driven search and execute and new API #325
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
14 commits
Select commit
Hold shift + click to select a range
f081c86
add LLM-driven tool_search and tool_execute
shashi-stackone 56ed22c
chore: retrigger CI
shashi-stackone 6e9b7ff
fix lint and tests
shashi-stackone 5678ae0
lint and tests
shashi-stackone 0758bea
Lint formatter CI vs local
shashi-stackone c633fa6
PR Suggestion from bots
shashi-stackone f49d51a
Fix linter error
shashi-stackone 6b9bb8b
Port the search execute changes to the node
shashi-stackone ba9b99f
Fix CI
shashi-stackone 0c5ba5f
Adopt the latest API changes
shashi-stackone 627e28c
update the files
shashi-stackone 7154eae
Fix test
shashi-stackone 94bdeb9
Fix lint issues
shashi-stackone 160eca8
update the doc strings
shashi-stackone 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,167 @@ | ||||||||||||||
| /** | ||||||||||||||
| * This example demonstrates the search and execute tools pattern (tool_search + tool_execute) | ||||||||||||||
| * for LLM-driven tool discovery and execution. | ||||||||||||||
| * | ||||||||||||||
| * Instead of loading all tools upfront, the LLM autonomously searches for | ||||||||||||||
| * relevant tools and executes them — keeping token usage minimal. | ||||||||||||||
| * | ||||||||||||||
| * @example | ||||||||||||||
| * ```bash | ||||||||||||||
| * # Run with required environment variables: | ||||||||||||||
| * STACKONE_API_KEY=your-key OPENAI_API_KEY=your-key STACKONE_ACCOUNT_ID=your-account npx tsx examples/agent-tool-search.ts | ||||||||||||||
| * ``` | ||||||||||||||
| */ | ||||||||||||||
|
|
||||||||||||||
| import process from 'node:process'; | ||||||||||||||
| import { openai } from '@ai-sdk/openai'; | ||||||||||||||
| import { StackOneToolSet } from '@stackone/ai'; | ||||||||||||||
| import { generateText, stepCountIs } from 'ai'; | ||||||||||||||
| import OpenAI from 'openai'; | ||||||||||||||
|
|
||||||||||||||
| const apiKey = process.env.STACKONE_API_KEY; | ||||||||||||||
| if (!apiKey) { | ||||||||||||||
| console.error('STACKONE_API_KEY environment variable is required'); | ||||||||||||||
| process.exit(1); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| if (!process.env.OPENAI_API_KEY) { | ||||||||||||||
| console.error('OPENAI_API_KEY environment variable is required'); | ||||||||||||||
| process.exit(1); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| const accountId = process.env.STACKONE_ACCOUNT_ID; | ||||||||||||||
|
|
||||||||||||||
| /** | ||||||||||||||
| * Example 1: Search and execute with Vercel AI SDK | ||||||||||||||
| * | ||||||||||||||
| * The LLM receives only tool_search and tool_execute — two small tool definitions | ||||||||||||||
| * regardless of how many tools exist. It searches for what it needs and executes. | ||||||||||||||
| */ | ||||||||||||||
| const toolsWithAISDK = async (): Promise<void> => { | ||||||||||||||
| console.log('Example 1: Search and execute with Vercel AI SDK\n'); | ||||||||||||||
|
|
||||||||||||||
| const toolset = new StackOneToolSet({ | ||||||||||||||
| search: { method: 'semantic', topK: 3 }, | ||||||||||||||
| ...(accountId ? { accountId } : {}), | ||||||||||||||
| }); | ||||||||||||||
|
|
||||||||||||||
| // Get search and execute tools — returns a Tools collection with tool_search + tool_execute | ||||||||||||||
| const accountIds = accountId ? [accountId] : []; | ||||||||||||||
| const tools = toolset.getTools({ accountIds }); | ||||||||||||||
|
|
||||||||||||||
| console.log( | ||||||||||||||
| `Search and execute: ${tools | ||||||||||||||
| .toArray() | ||||||||||||||
| .map((t) => t.name) | ||||||||||||||
| .join(', ')}`, | ||||||||||||||
| ); | ||||||||||||||
| console.log(); | ||||||||||||||
|
|
||||||||||||||
| // Pass to the LLM — it will search for calendly tools, then execute | ||||||||||||||
| const { text, steps } = await generateText({ | ||||||||||||||
| model: openai('gpt-5.4'), | ||||||||||||||
| tools: await tools.toAISDK(), | ||||||||||||||
| prompt: 'List my upcoming Calendly events for the next week.', | ||||||||||||||
| stopWhen: stepCountIs(10), | ||||||||||||||
| }); | ||||||||||||||
|
|
||||||||||||||
| console.log('AI Response:', text); | ||||||||||||||
| console.log('\nSteps taken:'); | ||||||||||||||
| for (const step of steps) { | ||||||||||||||
| for (const call of step.toolCalls ?? []) { | ||||||||||||||
| const args = (call as unknown as Record<string, unknown>).args; | ||||||||||||||
| const argsStr = args ? JSON.stringify(args).slice(0, 100) : '{}'; | ||||||||||||||
| console.log(` - ${call.toolName}(${argsStr})`); | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
| }; | ||||||||||||||
|
|
||||||||||||||
| /** | ||||||||||||||
| * Example 2: Search and execute with OpenAI Chat Completions | ||||||||||||||
| * | ||||||||||||||
| * Same pattern, different framework. The search and execute tools convert to any format. | ||||||||||||||
| */ | ||||||||||||||
| const toolsWithOpenAI = async (): Promise<void> => { | ||||||||||||||
| console.log('\nExample 2: Search and execute with OpenAI Chat Completions\n'); | ||||||||||||||
|
|
||||||||||||||
| const toolset = new StackOneToolSet({ | ||||||||||||||
| search: { method: 'semantic', topK: 3 }, | ||||||||||||||
| ...(accountId ? { accountId } : {}), | ||||||||||||||
| }); | ||||||||||||||
|
|
||||||||||||||
| const accountIds = accountId ? [accountId] : []; | ||||||||||||||
| const tools = toolset.getTools({ accountIds }); | ||||||||||||||
| const openaiTools = tools.toOpenAI(); | ||||||||||||||
|
|
||||||||||||||
| const client = new OpenAI(); | ||||||||||||||
| const messages: OpenAI.Chat.Completions.ChatCompletionMessageParam[] = [ | ||||||||||||||
| { | ||||||||||||||
| role: 'system', | ||||||||||||||
| content: | ||||||||||||||
| 'You are a helpful scheduling assistant. Use tool_search to find relevant tools, then tool_execute to run them. Always read the parameter schemas from tool_search results carefully. If a tool needs a user URI, first search for and call a "get current user" tool to obtain it. If a tool execution fails, try different parameters or a different tool.', | ||||||||||||||
| }, | ||||||||||||||
| { | ||||||||||||||
| role: 'user', | ||||||||||||||
| content: 'Check my upcoming Calendly events and list them.', | ||||||||||||||
| }, | ||||||||||||||
| ]; | ||||||||||||||
|
|
||||||||||||||
| // Agent loop — let the LLM drive search and execution | ||||||||||||||
| const maxIterations = 10; | ||||||||||||||
| for (let i = 0; i < maxIterations; i++) { | ||||||||||||||
| const response = await client.chat.completions.create({ | ||||||||||||||
| model: 'gpt-5.4', | ||||||||||||||
| messages, | ||||||||||||||
| tools: openaiTools, | ||||||||||||||
| tool_choice: 'auto', | ||||||||||||||
| }); | ||||||||||||||
|
|
||||||||||||||
| const choice = response.choices[0]; | ||||||||||||||
|
|
||||||||||||||
| if (!choice.message.tool_calls?.length) { | ||||||||||||||
| console.log('Final response:', choice.message.content); | ||||||||||||||
| break; | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| // Add assistant message with tool calls | ||||||||||||||
| messages.push(choice.message); | ||||||||||||||
|
|
||||||||||||||
| // Execute each tool call | ||||||||||||||
| for (const toolCall of choice.message.tool_calls) { | ||||||||||||||
| if (toolCall.type !== 'function') { | ||||||||||||||
| continue; | ||||||||||||||
| } | ||||||||||||||
|
Comment on lines
+131
to
+133
Contributor
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. P2: Skipping non-function tool calls leaves unmatched Prompt for AI agents
Suggested change
|
||||||||||||||
|
|
||||||||||||||
| console.log(`LLM called: ${toolCall.function.name}(${toolCall.function.arguments})`); | ||||||||||||||
|
|
||||||||||||||
| const tool = tools.getTool(toolCall.function.name); | ||||||||||||||
| if (!tool) { | ||||||||||||||
| messages.push({ | ||||||||||||||
| role: 'tool', | ||||||||||||||
| tool_call_id: toolCall.id, | ||||||||||||||
| content: JSON.stringify({ error: `Unknown tool: ${toolCall.function.name}` }), | ||||||||||||||
| }); | ||||||||||||||
| continue; | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| const result = await tool.execute(toolCall.function.arguments); | ||||||||||||||
| messages.push({ | ||||||||||||||
| role: 'tool', | ||||||||||||||
| tool_call_id: toolCall.id, | ||||||||||||||
| content: JSON.stringify(result), | ||||||||||||||
| }); | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
| }; | ||||||||||||||
|
|
||||||||||||||
| // Main execution | ||||||||||||||
| const main = async (): Promise<void> => { | ||||||||||||||
| try { | ||||||||||||||
| await toolsWithAISDK(); | ||||||||||||||
| await toolsWithOpenAI(); | ||||||||||||||
| } catch (error) { | ||||||||||||||
| console.error('Error running examples:', error); | ||||||||||||||
| } | ||||||||||||||
| }; | ||||||||||||||
|
|
||||||||||||||
| await main(); | ||||||||||||||
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.
The OpenAI agent loop (
while (continueLoop)) has no hard iteration/tool-call limit. If the model keeps emitting tool calls (or gets into a bad loop), this example can run indefinitely and incur unbounded API usage. Add a max-iterations/step counter similar to the AI SDK example’sstopWhen: stepCountIs(...)and break with a clear message when exceeded.