Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 96 additions & 0 deletions docs/js/typescript.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -264,3 +264,99 @@ agents.start()
```
</Step>
</Steps>

## Tool Calls Examples

<AccordionGroup>
<Accordion title="Single Agent Tool Call" icon="wrench" defaultOpen>
Create an agent that can use tools to get information:

```typescript
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?");
```
</Accordion>

<Accordion title="Direct Function Tools" icon="code" defaultOpen>
Create an agent with directly registered function tools:

```typescript
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 as an array without needing to make them global
tools: [getWeather, 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?");
```
</Accordion>
</AccordionGroup>
193 changes: 193 additions & 0 deletions src/praisonai-ts/examples/README-tool-examples.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
# PraisonAI Tool Registration Examples

This document demonstrates the three different ways to register tool functions in PraisonAI.

## Method 1: Using the `tools` array with function objects directly

```typescript
import { Agent } from 'praisonai';

// 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 as an array
tools: [getWeather, getTime]
});

// Start the agent with a prompt that will trigger tool usage
agent.start("What's the weather and time in Paris, France?");
```

## Method 2: Using the `toolFunctions` object with name-function pairs

```typescript
import { Agent } from 'praisonai';

// 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 with custom names
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?");
```

## Method 3: Using the `tools` array with pre-defined tool definitions

```typescript
import { Agent } from 'praisonai';

// Define the functions
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()}`;
}

// Register functions globally
import { registerFunction } from 'praisonai';
registerFunction('get_weather', getWeather);
registerFunction('get_time', getTime);

// Define tool definitions
const weatherTool = {
type: "function",
function: {
name: "get_weather",
description: "Get the current weather for a location",
parameters: {
type: "object",
properties: {
location: {
type: "string",
description: "The location to get weather for"
}
},
required: ["location"]
}
}
};

const timeTool = {
type: "function",
function: {
name: "get_time",
description: "Get the current time for a location",
parameters: {
type: "object",
properties: {
location: {
type: "string",
description: "The location to get time for"
}
},
required: ["location"]
}
}
};

// Create an agent with pre-defined tool definitions
const agent = new Agent({
instructions: `You provide the current weather and time for requested locations.`,
name: "ToolDefinitionAgent",
// Register pre-defined tool definitions
tools: [weatherTool, timeTool]
});

// Start the agent with a prompt that will trigger tool usage
agent.start("What's the weather and time in Paris, France?");
```

## Combined Approach

You can also combine these approaches as needed:

```typescript
import { Agent } from 'praisonai';

// Define the functions
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()}`;
}

// Define a custom tool definition
const calculatorTool = {
type: "function",
function: {
name: "calculate",
description: "Perform a calculation",
parameters: {
type: "object",
properties: {
expression: {
type: "string",
description: "The mathematical expression to calculate"
}
},
required: ["expression"]
}
}
};

// Register the calculator function globally
import { registerFunction } from 'praisonai';
registerFunction('calculate', async (expression: string) => {
console.log(`Calculating ${expression}...`);
// Simple eval for demonstration purposes only
return eval(expression).toString();
});

// Create an agent with mixed tool registration approaches
const agent = new Agent({
instructions: `You can provide weather, time, and perform calculations.`,
name: "MixedToolAgent",
// Register functions directly as an array
tools: [getWeather, getTime, calculatorTool]
});

// Start the agent with a prompt that will trigger tool usage
agent.start("What's the weather in Paris, the time in Tokyo, and what is 25 * 4?");
```
7 changes: 2 additions & 5 deletions src/praisonai-ts/examples/simple/direct-function-tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,8 @@ async function getTime(location: string) {
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
}
// Register functions directly as an array without needing to make them global
tools: [getWeather, getTime]
});

// Start the agent with a prompt that will trigger tool usage
Expand Down
4 changes: 2 additions & 2 deletions src/praisonai-ts/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "praisonai",
"version": "1.0.18",
"version": "1.0.19",
"description": "PraisonAI TypeScript AI Agents Framework - Node.js, npm, and Javascript AI Agents Framework",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down Expand Up @@ -62,7 +62,7 @@
"fast-xml-parser": "^4.5.1",
"node-fetch": "^2.6.9",
"openai": "^4.81.0",
"praisonai": "^1.0.17"
"praisonai": "^1.0.18"
},
"optionalDependencies": {
"boxen": "^7.1.1",
Expand Down
Loading
Loading