Introduction
Before delving into AI Agents, it is crucial to understand the lifecycle of a sophisticated language model like GPT. A large language model such as GPT begins with pretraining, where it learns from a vast amount of textual data to establish a basic understanding of the language. This is followed by supervised fine-tuning, where the model is enhanced for specific tasks using designated datasets. Reward modeling further improves the model’s behavior through positive reinforcement, while reinforcement learning allows the model to dynamically learn and adapt through interactions. In this article, we will explore how AI Agents can be built using “Tool Use.”

Overview
- Language models like GPT are developed through pretraining, supervised fine-tuning, reward modeling, and reinforcement learning.
- Each phase involves specific datasets, algorithms, model adjustments, and evaluations to enhance the model’s capabilities.
- Static models struggle with providing real-time information, requiring regular fine-tuning, which is resource-intensive and often impractical.
- Build AI Agents Using “Tool Use” in Agentic Workflow.
- AI agents with access to external tools can gather real-time data, execute tasks, and maintain context, enhancing accuracy and responsiveness.
GPT Assistant Training Pipeline
Each phase of the model’s development—pretraining, supervised fine-tuning, reward modeling, and reinforcement learning—progresses through four critical components: Dataset, Algorithm, Model, and Evaluation.
Pretraining Phase
The pretraining phase involves the model ingesting vast amounts of raw internet data to establish a foundation. This phase demands significant hardware resources and months of intensive training to initialize and update weights as learning progresses.

Supervised Fine-Tuning Phase
Supervised fine-tuning focuses on task-specific labeled datasets to refine the model’s parameters for accurate predictions. This phase requires less time and resources compared to pretraining due to enhanced dataset quality.
Reward Modeling Phase
Reward modeling enhances model performance through positive reinforcement signals, leading to further improvements through human feedback or evaluation.
Reinforcement Learning Phase
Reinforcement learning optimizes the model’s responses through interactions with its environment, ensuring adaptability to new information.
The Challenge of Real-Time Data
Addressing the challenge of real-time data involves continuously updating the model with new information to ensure accurate responses to queries.
However, a critical question arises: How do we equip the model to access and respond to real-world information, especially for the latest queries and prompts?
For example, the model may struggle to provide responses grounded in real-world data when tested with specific questions.

Fine-tune the Model
One approach is to fine-tune the model regularly to address the latest queries and prompts. However, this method comes with challenges such as insufficient data, high computation requirements, and time intensiveness.
So here comes AI Agents
AI agents, equipped with external tools, can collect and process information, execute tasks, and maintain context from past interactions. These agents go beyond basic LLMs by interfacing with external tools, gathering and manipulating data, planning tasks, and enhancing feature capabilities.
Using AI Agents for Real-Time Information Retrieval
AI agents can access real-time information by utilizing external tools to perform tasks such as web searches and data processing. By integrating external resources, AI agents can handle complex queries effectively.
Handling Complex Queries with Computational Tools
For instance, an LLM equipped with a code execution tool can handle complex queries by executing commands to compute accurate results, enhancing its capabilities in handling various tasks.
For example, it could create a command like {tool: python-interpreter, code: “cost_price * (1 – 0.20)”}, where “cost_price” represents the initial cost of the item. This method ensures that the LLM effectively utilizes computational tools to accurately calculate profit or loss instead of trying to generate the answer directly through language processing, which may not produce precise results. Additionally, users can also utilize external tools to perform tasks such as booking tickets, known as Task Planning – Agentic Workflow.
AI agents can assist ChatGPT in addressing the challenge of lacking real-world data. By enabling access to the Internet, the AI can conduct Google searches to retrieve relevant information. In this scenario, the Internet search tool is utilized.
When the AI recognizes the need for current weather information, it includes a list of available tools in its API request. By utilizing the get_current_weather function with a specified location parameter, such as “London,” the system fetches the latest weather details for that location. This data is then seamlessly integrated into the AI’s response to enhance accuracy and relevance.
Now, let’s implement and incorporate Tool Use to understand the Agentic workflow. We will use AI agents as a tool to obtain current weather information, addressing the limitation of not being able to respond to real-world questions with up-to-date data.
To begin the implementation process, we first need to install necessary dependencies and libraries:
langchain
langchain-community>=0.0.36
langchainhub>=0.1.15
llama_cpp_python
pandas
loguru
googlesearch-python
transformers
Openai
Next, we import the required libraries:
from openai import OpenAI
import json
from rich import print
import dotenv
dotenv.load_dotenv()
Ensure your OpenAI API key is stored in an env file or variable. Then, interact with the GPT model using code to inquire about the weather in London. This code snippet sets up a basic interaction with an AI model, prompting it to generate a response based on its training data.
It’s important to note that this code does not fetch real-time weather data but rather relies on the AI model’s training data for generating responses.
By defining a function to retrieve weather information and structuring it as a tool in an AI conversation, we can enhance the AI’s capabilities. This function can be used to provide accurate weather information based on the specified location, such as London, San Francisco, or Paris. Additionally, by incorporating tools like this into AI conversations, we can improve the relevance and accuracy of the information shared with users. Let’s break down the code into its components:
– The `get_current_weather` function:
– Takes a location parameter.
– Returns simulated weather data for London, San Francisco, and Paris.
– Returns “unknown” for any other location.
– The weather data is returned as a JSON string.
– The `messages` list:
– Contains a single message from the user asking about the weather in London.
– The `tools` list:
– Defines a single tool (function) that the AI can use.
– The tool is of type “function” and describes the `get_current_weather` function:
– Name: The name of the function to be called.
– Description: A brief description of what the function does.
– Parameters: Describes the expected input for the function:
– Expects an object with a location property.
– Location should be a string describing a city.
– The location parameter is required.
The code snippet then shows how to create a response using the `client.chat.completions.create` method with the specified model, messages, and tools. Additionally, three external scripts named LLMs, tools, and tool_executor are imported as helper functions.
The `llms.py` script manages interactions with OpenAI’s chat completion API and enables the use of external tools within the chat context. It defines a class `OpenAIChatCompletion` that handles interactions with the API and tool management.
The `tools.py` script defines individual tools or functions that can be utilized by the AI system, such as fetching real-time weather data.
Overall, these components work together to facilitate chat interactions and tool usage within the AI system. Let’s analyze each tool individually:
get_current_weather:
– Retrieves real-time weather information for a specified city using the wttr.in API.
– Returns the weather data as a JSON string.
– Includes error handling and logging.
Tool_executor.py:
– Manages the execution and coordination of tools to ensure they are correctly integrated into the AI’s response workflow.
The code snippet provided defines a ToolRegistry class and related helper functions for overseeing and running tools in an AI system. Here’s a breakdown:
ToolRegistry class:
– Maintains a collection of tools, storing them in their original form and an OpenAI-compatible format.
– Offers methods for registering, fetching, and executing tools.
Key methods:
– register_tool: Adds a new tool to the registry.
– openai_tools: Property that returns tools in OpenAI’s function format.
– call_tool: Executes a single tool.
– call_tools: Executes multiple tools from a ChatCompletion output.
Helper functions:
– need_tool_use: Determines if a ChatCompletion output necessitates tool utilization.
– check_function_signature: Validates function calls against the available tools.
The ToolRegistry class plays a pivotal role in managing and executing tools within an AI system, facilitating:
– Simple registration of new tools
– Conversion of tools to OpenAI’s function calling format
– Execution of tools based on AI model outputs
– Validation of tool calls and signatures
This design enables smooth integration with AI models that support function calling, such as those from OpenAI. The structured approach facilitates the expansion of an AI system’s capabilities by enabling interaction with external tools and data sources. Functions like need_tool_use and check_function_signature enhance the utility of ChatCompletion outputs and validate tool usage.
This code is integral to a comprehensive system for developing AI agents that can leverage external tools and APIs to go beyond basic text generation. It includes external scripts and helper functions necessary for integrating external tools and functionalities into AI processes.
An instance of OpenAIChatCompletion is instantiated, and the get_current_weather tool is linked to this instance. A message list is created with a user query about London’s weather, and a chat completion is initiated using this setup.
The AI intelligently identifies the need to utilize the get_current_weather function to respond to the query about London’s weather. Instead of directly answering, it requests the function to be called with “London” as the argument.
The AI’s ability to decide to use available tools to gather information before responding enhances the accuracy and relevance of its answers. The code checks for the need to use tools based on the AI’s output, executes the weather tool, updates the context of the conversation, and prompts the AI to respond with the new information.
This implementation is a significant advancement in creating context-aware AI systems that can interact meaningfully with the real world by integrating external tools and data sources with large language models.
The FAQ section addresses common queries about AI agents with dynamic tool use, highlighting the advantages and potential applications of such systems in various fields like personal assistance, customer service, finance, healthcare, and project management.