Call tools with an AI agent Pipe locally using OpenAI
In this guide, you’ll learn what tool calls are and how you can use them with an AI agent pipe using GPT-4o-mini to create a self-healing agent.
What are Tool Calls?
Tool calling lets an LLM (like GPT) use functions from your codebase to handle tasks it can't manage on its own. Instead of just producing text, the model can generate a tool call (a function name with parameters) that triggers a specific function in your code.
With tool calling, you can have the model do things like fetch real-time info, run complex calculations, retrieve data from a database, or interact with other systems.
Key Benefits
- More functionality: It extends the model’s abilities beyond text generation, allowing it to perform practical tasks.
- Improved accuracy: For tasks like calculations or getting real-time data, it ensures accurate and reliable results.
- Broader use cases: It makes the model more useful in real-world applications, easily connecting it with other systems.
How to Create a Tool and Integrate with an AI Agent Pipe Locally?
Let's build a tool locally using BaseAI that will return the current weather for a given location.
Step #1: Create a weather tool
Create a directory in your local machine and navigate to it. Run the following command in the terminal:
1# create a project directory
2mkdir my-ai-project
3
4# intialize npm
5npm init -y
6
7# install dotenv package
8npm install dotenvThis command will create a package.json file in your project directory with default values. It will also install the dotenv package to read environment variables from the .env file.
Let’s initialize BaseAI in our project. To do this, run the following command in your project terminal:
1npx baseai@latest initLet’s create a tool now. To do it, run the following command in your project terminal:
1npx baseai@latest toolThe CLI will ask you to provide the name and description of the tool. Let's call it getCurrentWeather and provide a description like Get the current weather for a given location.
Your tool will be created at /baseai/tools/get-current-weather.ts.
Step #2: Update the Tool
Navigate to your project directory and open the tool you created. You can find it at /baseai/tools/get-current-weather.ts. This is what it looks like:
1import { ToolI } from '@baseai/core';
2
3export async function getCurrentWeather() {
4 // Your tool logic here
5}
6
7const getCurrentWeatherTool = (): ToolI => ({
8 run: getCurrentWeather,
9 type: 'function' as const,
10 function: {
11 name: 'getCurrentWeather',
12 description: 'Get the current weather for a given location',
13 parameters: {},
14 },
15});
16
17export default getCurrentWeatherTool;Let's add parameters to the getCurrentWeather function. The LLM will give values to these parameters when it calls the tool.
Let’s add a static return from the getCurrentWeather function. You can replace it with your logic to get the current weather.
1import {ToolI} from '@baseai/core';
2
3export async function getCurrentWeather(location: string, unit: string) {
4 return `Weather in ${location} is 72 degrees ${unit === 'celsius' ? 'Celsius' : 'Fahrenheit'}`;
5}
6
7const getCurrentWeatherTool = (): ToolI => ({
8 run: getCurrentWeather,
9 type: 'function' as const,
10 function: {
11 name: 'getCurrentWeather',
12 description: 'Get the current weather for a given location',
13 parameters: {
14 type: 'object',
15 properties: {
16 location: {
17 type: 'string',
18 description: 'The city and state, e.g. San Francisco, CA',
19 },
20 unit: {
21 type: 'string',
22 enum: ['celsius', 'fahrenheit'],
23 },
24 },
25 required: ['location'],
26 },
27 },
28});
29
30export default getCurrentWeatherTool;
31Step #3: Integrate the Tool in an AI Agent Pipe
To integrate the tool in an AI agent pipe, let’s create a basic summarizer pipe using this command:
1npx baseai@latest pipeIt will ask you for the name, description, and other details of the pipe step-by-step.
Once you are done, your pipe will be created successfully at /baseai/pipes/summarizer.ts.
Let's update the pipe. We will do the following:
- Add system prompt to the pipe
- Update pipe function name from
pipeNametopipeSummarizer - Import the
getCurrentWeathertool we created in step 2 inside pipe - Call the
getCurrentWeather()tool inside tools array
This is what it will look like after adding these details:
1import { PipeI } from '@baseai/core';
2import getCurrentWeatherTool from '../tools/get-current-weather';
3
4const pipeSummarizer = (): PipeI => ({
5 apiKey: process.env.LANGBASE_API_KEY!, // Replace with your API key https://langbase.com/docs/api-reference/api-keys
6 name: 'summarizer',
7 description: 'A pipe that summarizes content and make it less wordy',
8 status: 'public',
9 model: 'openai:gpt-4o-mini',
10 stream: true,
11 json: false,
12 store: true,
13 moderate: true,
14 top_p: 1,
15 max_tokens: 1000,
16 temperature: 0.7,
17 presence_penalty: 1,
18 frequency_penalty: 1,
19 stop: [],
20 tool_choice: 'auto',
21 parallel_tool_calls: false,
22 messages: [
23 { role: 'system', content: `You are a content summarizer. You will summarize content without loosing context into less wordy to the point version.` },
24 ],
25 variables: [],
26 memory: [],
27 tools: [getCurrentWeatherTool()]
28});
29
30export default pipeSummarizer;To integrate the summarizer pipe with your Node.js project, create an index.ts file in your project by running this command:
1touch index.tsIn this index.ts file, import the summarizer pipe and the tool you created. We will use the pipe primitive from @baseai/core to run the pipe. Let's update the user message to the following in our summarizer pipe. And instead of streaming, let's generate text from LLM.
What's the weather in San Francisco?
This is how the index.ts file will look like:
1import 'dotenv/config';
2import {Pipe} from '@baseai/core';
3import pipeSummarizer from './baseai/pipes/summarizer';
4
5const pipe = new Pipe(pipeSummarizer());
6
7const userMsg = `What's the weather in San Francisco?`;
8
9async function main() {
10 const response = await pipe.run({
11 messages: [{role: 'user', content: userMsg}],
12 stream: false,
13 });
14
15 console.log(response.completion);
16}
17
18main();Since we are using OpenAI’s model GPT-4o-mini, your OpenAI key is required. Create an .env file in the root directory and add the key.
OPENAI_API_KEY="<REPLACE-OPENAI-KEY>" # Add your OpenAI API key in .env file
Step #4: Start the BaseAI server
To run the pipe locally, you need to start the BaseAI server. Run the following command in your terminal:
1npx baseai@latest devStep #5: Run the Pipe
Let’s call our pipe now. To do this, run the following command in your terminal:
1npx tsx index.tsIt will prompt the LLM model to get answers to your weather query.
The current weather in San Francisco is 72 degrees Fahrenheit.
When we configured the weather tool, we added 72 degrees Fahrenheit as a static return of the getCurrentWeather function. That's why we are getting this response.
This all happens locally on your machine and the response should be streamed in your terminal.
In the same way you can create more tools and ship AI features. Since tool is a function in your code, you can call any pipe from it as well.