Let’s create a Task Management Assistant with AIIDE. This assistant will help you manage your tasks, by creating, updating, and deleting them. Each task also has a custom schedule associated with it.
Prerequisites
Before we start, make sure you have pandas and aiide installed:
Imports
Let’s start by importing the necessary modules:
AIIDE is used to define and manage the chat instance.
Tool is used to define the tools.
TOOL_DEF, DICT and STR are some of the helpful abstractions for defining the JSON API for tool definition.
Pandas is used to maintian the tasks in a DataFrame, and aiide is used to create the assistant.
Define the Tool
Since the heart of the assistant is task management, let’s define the tool first:
The tool class inherits TOOL and has to have the following methods:
tool_def: This method returns the tool’s JSON API. It is called multiple times during generation. You can return different JSON API based on the context. ex: change the enums of the fields etc
main: This method is called when the tool is invoked. It takes the inputs defined in the JSON API and returns the output.
Let’s define the Tool
Here we are taking the parent instance(chat instance which will be defined below) as an argument and storing it in the self.parent attribute.
It’s better to provide all the tools with the chat instance as the parent, so that they can access the environment and functions of other tools if and when needed.
Environment
Technically, the environment ENV is a list of strings that are associated with each tool.
We can use the environment to provide the LLM with the last couple of snapshots of any data the tool is acting upon.
This way, we won’t overload the llm with the entire history of the data, which might only have slight modifications.
It is also good for cost savings.
In this tutorial, we are only going to provide the current task list and the current timestamp as the single(latest) element in the environment.
Tool Definition
tool_def expects OpenAI’s JSON API for the tool definition. We have created a helper function TOOL_DEF to make it easier to define the JSON API.
Here we are defining the tool with the name task_management and providing a description of what the tool does.
We are defining the properties of the tool:
task_id: The name of the task. It must be unique.
operation_type: The operation to perform on the task. It can be either add, edit, or delete.
task_description: The description of the task. It must be in markdown.
cron_schedule_expression: The cron schedule expression for the task. The format is ‘minute hour day month day_of_week’.
We are also defining the required properties of the tool: task_id and operation_type.
Main Function
The main function is the function that is called when the tool is invoked. It takes the inputs defined in the JSON API, performs the operation on the dataframe, updates the ENV and returns the output.
Here we are checking the operation_type and performing the operation on the dataframe accordingly.
Define the Chat Instance
Now that we have defined the tool, let’s define the chat instance:
Here we are defining the chat instance Agent which inherits AIIDE.
We are taking the task dataframe as an argument and storing it in the self.task_df attribute.
We are creating an instance of the TaskTool and storing it in the self.task_tool attribute.
We are setting up the chat instance with a system message and the model type.
Main Loop
Now that we have defined the chat instance, let’s run the main loop:
Here we are creating a task dataframe with some initial tasks.
We are creating an instance of the Agent and passing the task dataframe as an argument.
Let’s create a loop to take user input and chat with the assistant:
We call the chat method of the agent instance with the user message and the tools we wish to enable for that interaction.
We are iterating over the response and printing the response accordingly.
AIIDE only supports streaming currently and we iterate over it to get the responses and parse them based on type of response.
For response type of text: The LLM is talking to the user, which has delta and content as attributes.
For response type of tool_call: The LLM is calling a tool, which has name and arguments as attributes.
For response type of tool_response: Response from the tool, which has response as an attribute along with name and arguments.
Complete code for reference
Conclusion
In this tutorial, we created a Task Management Assistant with AIIDE. We defined a tool to add, edit, or delete tasks to the list and created a chat instance to manage the tasks. We also created a loop to take user input and chat with the assistant.