agent-from-scratch is a Python-based repository for developers and researchers to understand the fundamentals of single and multi-agent systems without going through more dense and sophisticated Agent frameworks such as Langgraph and Autogen. In fact, agent-from-sctach is not even a framework, it is single script repository which you can simply download and meddle with it to understand how agents work.
It is a fork of OpenAI's Swarm, which is already straightforward. However, agent-from-scratch is even simpler, making it easier to quickly start and understand single and multi-agent systems.
It is for software developers and ML Engineers who would like to understand what are agents and how are they built. This is not a Agent framework. This is simply a python script
There is a single script called agent.py
in this. It is a very easy read. One can read it and understand what Agents are and how they are built. Once you understand you can either fork this repository or just copy paste the agent.py
script and start building your own agents.
- Clone or fork the repository:
git clone https://github.com/hexo-ai/agent-from-scratch.git
- To set up the conda environment, run the following command:
conda env create -f environment.yml
. Alternatively, you can use a virtual environment. - Create a
.env
file by copying the structure from.env.template
. - Add your environment variables to the
.env
file. - Activate the conda environment using
conda activate agent-from-scratch
or your virtual environment. - Install the requirements using
pip install -r requirements.txt
. - To run the single agent example, execute
python single_agent_example.py
. This script implements a weather agent with capabilities to send emails. - To run the multi-agent example, execute
python multi_agent_example.py
. This script implements sales and refund agents with capabilities to apply discounts and process refunds.
This is the singular script which constructs an Agent. An agent is nothing more nothing lesser than an LLM which can call tools/functions and go in loops to accomplish a goal. Here is an architecture of an Agent.
A User can assign a goal to an agent. An Agent consists of an LLM and the LLM is capable to calling a bunch of tools. The LLM takes up the goal and uses whichever tool, whenever necessary in order to accomplish the goal.
- In
agent.py
, you will see a class calledAgent
which can take up a goal (variable named instructions), an LLM (variable named model) and a bunch of tools (variable named functions). - It has another class called
Swarm
which contains the logic on how the LLM can calls the tools and accomplish the given goal.
class Swarm:
def __init__():
# Implements logic of how llm and tool calling works together
def get_chat_completion():
# Implements LLM completion
def handle_tool_calls():
# Implements tool calling
def handle_function_result():
# processes the tool outptu
def run():
# runs the whole logic of goal -> llm -> tools -> output -> feedback from environment
We have included two examples in this repository: namely, single_agent_example.py
and multi_agent_example.py
.
In single_agent_example.py
, we have a simple implementation of a weather agent. Here are the features of the agent:
- Goal: To retrieve the weather information for the user
- llm: gpt-4o
- tools: get_weather and send_email
In the multi_agent_example.py
, we have a simple implementation of a customer bot multi-agent. There are three agents implemented in it, namely: triage_agent, sales_agent and refund agent
- Goal: Route the user request to the correct agent
- llm: gpt-4o
- tools: sales_agent, refund_agent
- Goal: To sell a product
- llm: gpt-4o
- tools: triage_agent
- Goal: To process a refund
- llm: gpt-4o
- tools: process_refund, apply_discount
Please feel free to fork this repository and create PRs. We are eager to get PRs on the following:
- Additional working examples of Agents
- Tracing and Logging of the Agents
- Feedback and learning from experiences