-
-
Notifications
You must be signed in to change notification settings - Fork 505
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enhance agent tool function registration with direct function support
- Added support for directly registering tool functions without global declaration - Implemented auto-generation of tool definitions for registered functions - Created new example scripts demonstrating direct function tool registration - Updated package version to 1.0.18 - Expanded Agent configuration to support `toolFunctions` property - Added type exports for improved type safety
- Loading branch information
1 parent
b966df4
commit 14049e4
Showing
8 changed files
with
279 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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,35 @@ | ||
import { Agent } from 'praisonai'; | ||
|
||
/** | ||
* Example of a simple agent with direct function registration | ||
* | ||
* This example demonstrates how to create a simple agent that uses directly | ||
* registered functions as tools without having to define tool schemas manually | ||
* or make functions globally available. | ||
*/ | ||
|
||
// Define the functions directly | ||
async function getWeather(location: string) { | ||
console.log(`Getting weather for ${location}...`); | ||
return `${Math.floor(Math.random() * 30)}°C`; | ||
} | ||
|
||
async function getTime(location: string) { | ||
console.log(`Getting time for ${location}...`); | ||
const now = new Date(); | ||
return `${now.getHours()}:${now.getMinutes()}`; | ||
} | ||
|
||
// Create an agent with directly registered functions | ||
const agent = new Agent({ | ||
instructions: `You provide the current weather and time for requested locations.`, | ||
name: "DirectFunctionAgent", | ||
// Register functions directly without needing to make them global | ||
toolFunctions: { | ||
get_weather: getWeather, | ||
get_time: getTime | ||
} | ||
}); | ||
|
||
// Start the agent with a prompt that will trigger tool usage | ||
agent.start("What's the weather and time in Paris, France and Tokyo, Japan?"); |
This file contains 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,73 @@ | ||
import { Agent } from 'praisonai'; | ||
|
||
/** | ||
* Example of a simple agent with multiple tool calling capability | ||
* | ||
* This example demonstrates how to create a simple agent that can use multiple tools | ||
* to get weather and time information for different locations. | ||
*/ | ||
|
||
// Define a weather tool | ||
const getWeather = { | ||
type: "function", | ||
function: { | ||
name: "get_weather", | ||
description: "Get current temperature for a given location.", | ||
parameters: { | ||
type: "object", | ||
properties: { | ||
location: { | ||
type: "string", | ||
description: "City and country e.g. Bogotá, Colombia" | ||
} | ||
}, | ||
required: ["location"], | ||
additionalProperties: false | ||
}, | ||
strict: true | ||
} | ||
}; | ||
|
||
// Define a time tool | ||
const getTime = { | ||
type: "function", | ||
function: { | ||
name: "get_time", | ||
description: "Get current time for a given location.", | ||
parameters: { | ||
type: "object", | ||
properties: { | ||
location: { | ||
type: "string", | ||
description: "City and country e.g. Bogotá, Colombia" | ||
} | ||
}, | ||
required: ["location"], | ||
additionalProperties: false | ||
}, | ||
strict: true | ||
} | ||
}; | ||
|
||
// Make the functions globally available | ||
// The agent will automatically find and use these functions | ||
(global as any).get_weather = async function(location: string) { | ||
console.log(`Getting weather for ${location}...`); | ||
return `${Math.floor(Math.random() * 30)}°C`; | ||
}; | ||
|
||
(global as any).get_time = async function(location: string) { | ||
console.log(`Getting time for ${location}...`); | ||
const now = new Date(); | ||
return `${now.getHours()}:${now.getMinutes()}`; | ||
}; | ||
|
||
// Create an agent with both weather and time tools | ||
const agent = new Agent({ | ||
instructions: `You provide the current weather and time for requested locations.`, | ||
name: "WeatherTimeAgent", | ||
tools: [getWeather, getTime] | ||
}); | ||
|
||
// Start the agent with a prompt that will trigger multiple tool calls | ||
agent.start("What's the weather and time in Paris, France and Tokyo, Japan?"); |
46 changes: 46 additions & 0 deletions
46
src/praisonai-ts/examples/simple/single-agent-tool-call.ts
This file contains 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,46 @@ | ||
import { Agent } from 'praisonai'; | ||
|
||
/** | ||
* Example of a simple agent with tool calling capability | ||
* | ||
* This example demonstrates how to create a simple agent that can use tools | ||
* to get weather information for a location. | ||
*/ | ||
|
||
// Define a weather tool | ||
const getWeather = { | ||
type: "function", | ||
function: { | ||
name: "get_weather", | ||
description: "Get current temperature for a given location.", | ||
parameters: { | ||
type: "object", | ||
properties: { | ||
location: { | ||
type: "string", | ||
description: "City and country e.g. Bogotá, Colombia" | ||
} | ||
}, | ||
required: ["location"], | ||
additionalProperties: false | ||
}, | ||
strict: true | ||
} | ||
}; | ||
|
||
// Make the function globally available | ||
// The agent will automatically find and use this function | ||
(global as any).get_weather = async function(location: string) { | ||
console.log(`Getting weather for ${location}...`); | ||
return `20°C`; | ||
}; | ||
|
||
// Create an agent with the weather tool | ||
const agent = new Agent({ | ||
instructions: `You provide the current weather for requested locations.`, | ||
name: "WeatherAgent", | ||
tools: [getWeather] | ||
}); | ||
|
||
// Start the agent with a prompt that will trigger tool usage | ||
agent.start("What's the weather in Paris, France?"); |
This file contains 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 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 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 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
35 changes: 35 additions & 0 deletions
35
src/praisonai-ts/tests/development/simple/direct-function-tools.ts
This file contains 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,35 @@ | ||
import { Agent } from '../../../src/agent/proxy'; | ||
|
||
/** | ||
* Example of a simple agent with direct function registration | ||
* | ||
* This example demonstrates how to create a simple agent that uses directly | ||
* registered functions as tools without having to define tool schemas manually | ||
* or make functions globally available. | ||
*/ | ||
|
||
// Define the functions directly | ||
async function getWeather(location: string) { | ||
console.log(`Getting weather for ${location}...`); | ||
return `${Math.floor(Math.random() * 30)}°C`; | ||
} | ||
|
||
async function getTime(location: string) { | ||
console.log(`Getting time for ${location}...`); | ||
const now = new Date(); | ||
return `${now.getHours()}:${now.getMinutes()}`; | ||
} | ||
|
||
// Create an agent with directly registered functions | ||
const agent = new Agent({ | ||
instructions: `You provide the current weather and time for requested locations.`, | ||
name: "DirectFunctionAgent", | ||
// Register functions directly without needing to make them global | ||
toolFunctions: { | ||
get_weather: getWeather, | ||
get_time: getTime | ||
} | ||
}); | ||
|
||
// Start the agent with a prompt that will trigger tool usage | ||
agent.start("What's the weather and time in Paris, France and Tokyo, Japan?"); |