Create a Custom Tool¶
The true power of an agent comes from the tools it can use. CogniCoreAi is designed to make adding new tools as simple as possible. This guide will show you how to create a custom tool to get the current date and time.
The Core Concept¶
Any custom tool is a Python class that inherits from cognicoreai.Tool and correctly implements three things:
A
nameproperty: A unique, single-word identifier for the tool.A
descriptionproperty: A clear, natural language explanation of what the tool does. The LLM uses this to decide when to use the tool.A
runmethod: The actual Python code that gets executed.
Step 1: Define the Tool Class¶
Let’s create a tool that returns the current date and time. Create a new class, DateTimeTool, that inherits from Tool.
1import datetime
2from cognicoreai import Tool
3
4class DateTimeTool(Tool):
5 """A tool to get the current date and time."""
6
7 @property
8 def name(self) -> str:
9 return "get_current_datetime"
10
11 @property
12 def description(self) -> str:
13 return "Returns the current date and time. It takes no input."
14
15 def run(self, tool_input: str) -> str:
16 # This tool ignores the input, but it's good practice to include
17 # the parameter in the method signature to match the base class.
18 return datetime.datetime.now().isoformat()
Step 2: Use the Tool in an Agent¶
Now, you can import your new tool and add it to the agent’s tool list during initialization.
1from cognicoreai import Agent, OpenAI_LLM, VolatileMemory, CalculatorTool
2# Import your new custom tool
3from your_project_file import DateTimeTool
4
5# 1. Initialize the LLM and Memory
6llm = OpenAI_LLM()
7memory = VolatileMemory()
8
9# 2. Create a list of all tools the agent can use
10tools = [CalculatorTool(), DateTimeTool()]
11
12# 3. Create the agent
13agent = Agent(llm, memory, tools)
14
15# 4. Chat with the agent and ask it to use the new tool
16# The LLM will see "get_current_datetime" in its list of available tools
17# and will know to call it when asked about the time.
18response = agent.chat("What time is it right now?")
19print(response)
And that’s it! The agent will now have the ability to report the current date and time by executing your custom Python code.