A Task-based agentic framework building on StrictJSON outputs by LLM agents
Project description
TaskGen v3.2.1
A Task-based agentic framework building on StrictJSON outputs by LLM agents
- Related Repositories: StrictJSON (https://github.com/tanchongmin/strictjson)
- Video (Part 1): https://www.youtube.com/watch?v=O_XyTT7QGH4
- Video (Part 2): https://www.youtube.com/watch?v=OWk7moRfTPE
- TaskGen Ask Me Anything: https://www.youtube.com/watch?v=mheIWKugqF4
Creator's Preamble
Happy to share that the task-based agentic framework I have been working on - TaskGen - is largely complete!
Noteable features include:
- Splitting of Tasks into subtasks for bite-sized solutions for each subtask
- Single Agent with LLM Functions
- Single Agent with External Functions
- Meta Agent with Inner Agents as Functions
- Shared Variables for multi-modality support
- Retrieval Augmented Generation (RAG) over Function space
- Memory to provide additional task-based prompts for task
- Global Context for configuring your own prompts + add persistent variables
- Async mode for Agent, Function and
strict_json
added - Community Uploading and Downloading of Agent and Functions
I am quite sure that this is the best open-source agentic framework for task-based execution out there! Existing frameworks like AutoGen rely too much on conversational text which is lengthy and not targeted. TaskGen uses StrictJSON (JSON parser with type checking and more!) as the core, and agents are efficient and are able to do Chain of Thought natively using JSON keys and descriptions as a guide.
What can you do to help:
- Star the github so more people can use it (It's open source and free to use, even commercially!)
- Contribute your favourite external function integrations so that it can be much more boilerplate for others to use :)
- Contribute template Jupyter Notebooks for your favourite use cases :)
I can't wait to see what this new framework can do for you!
Benefits of JSON messaging over agentic frameworks using conversational free-text like AutoGen
- JSON format helps do Chain-of-Thought prompting naturally and is less verbose than free text
- JSON format allows natural parsing of multiple output fields by agents
- StrictJSON helps to ensure all output fields are there and of the right format required for downstream processing
Tutorials and Community Support
- Created: 17 Feb 2024 by John Tan Chong Min
- Co-developer / Lead Contributor: Prince Saroj
- Collaborators welcome
- Discussion Channel (my discord - John's AI Group): https://discord.gg/bzp87AHJy5
How do I use this?
- Download package via command line
pip install taskgen-ai
- Set up your LLM and provide any API keys if needed
- Import the required functions from
taskgen
and use them!
Differences in LLM for Agentic Framework
- Default model is now gpt-4o-mini if you do not specify any LLM!
- Weaker models like ChatGPT (gpt-3.5-turbo) and Llama 3 8B are consistent only if you specify very clearly what you want the Agent to do and give examples of what you want
- gpt-4o-mini, gpt-4o, Llama 3 70B and more advanced models can perform better zero-shot without much examples
- TaskGen is compatible with ChatGPT and similar models, but for more robust use, consider using gpt-4o-mini and better models
1. Agent Basics
-
Create an agent by entering your agent's name and description
-
Agents are task-based, so they will help generate subtasks to fulfil your main task
-
Agents are made to be non-verbose, so they will just focus only on task instruction (Much more efficient compared to conversational-based agentic frameworks like AutoGen)
-
Agent's interactions will be stored into
subtasks_completed
by default, which will serve as a memory buffer for future interactions -
Inputs for Agent:
- agent_name: String. Name of agent, hinting at what the agent does
- agent_description: String. Short description of what the agent does
- max_subtasks: Int. Default: 5. The maximum number of subtasks the agent can have
- verbose: Bool. Default: True. Whether to print out agent's intermediate thoughts
- llm: Function. The LLM to be used by the Agent
-
Agent Internal Parameters:
- Task: String. The task the agent has been assigned to - Defaults to "No task assigned"
- Subtasks Completed: Dict. The keys are the subtask names and the values are the result of the respective subtask
- Is Task Completed: Bool. Whether the current Task is completed
-
Task Running
- reset(): Resets the Agent Internal Parameters and Subtasks Completed. You should do this at the start of every new task assigned to the Agent to minimise potential confusion of what has been done for this task versus previous tasks
- run(task: str, num_subtasks: int = max_subtasks): Performs the task. Do note that agent's state will not be reset, so if you want to reset it, call reset() prior to running this. Runs the task for num_subtasks steps. If not specified, we will take the max_subtasks.
-
Give User Output
- reply_user(query: str = '', stateful: bool = True): Using all information from subtasks, give a reply about the
query
to the user. Ifquery
is not given, then it replies based on the current task the agent is doing. Ifstateful
is True, saves this query and reply intosubtasks_completed
- reply_user(query: str = '', stateful: bool = True): Using all information from subtasks, give a reply about the
-
Check status of Agent:
- status(): Lists out Agent Name, Agent Description, Available Functions (default function is to use the LLM), Task, Subtasks Completed and Is Task Completed
Example Agent Creation
my_agent = Agent('Helpful assistant', 'You are a generalist agent', llm = llm)
Example Agent Task Running - Split the assigned task into subtasks and execute each of them
output = my_agent.run('Give me 5 words rhyming with cool, and make a 4-sentence poem using them')
Subtask identified: Find 5 words that rhyme with 'cool'
Getting LLM to perform the following task: Find 5 words that rhyme with 'cool'
pool, rule, fool, tool, school
Subtask identified: Compose a 4-sentence poem using the words 'pool', 'rule', 'fool', 'tool', and 'school'
Getting LLM to perform the following task: Compose a 4-sentence poem using the words 'pool', 'rule', 'fool', 'tool', and 'school'
In the school, the golden rule is to never be a fool. Use your mind as a tool, and always follow the pool.
Task completed successfully!
Check Agent's Status
my_agent.status()
Agent Name: Helpful assistant
Agent Description: You are a generalist agent
Available Functions: ['use_llm', 'end_task']
Task: Give me 5 words rhyming with cool, and make a 4-sentence poem using them
Subtasks Completed:
Subtask: Find 5 words that rhyme with 'cool'
pool, rule, fool, tool, school
Subtask: Compose a 4-sentence poem using the words 'pool', 'rule', 'fool', 'tool', and 'school'
In the school, the golden rule is to never be a fool. Use your mind as a tool, and always follow the pool.
Is Task Completed: True
Example Agent Reply to User - Reference the subtasks' output to answer the user's query
output = my_agent.reply_user()
Here are 5 words that rhyme with "cool": pool, rule, fool, tool, school. Here is a 4-sentence poem using these words: "In the school, the golden rule is to never be a fool. Use your mind as a tool, and always follow the pool."
2. Power Up your Agents - Bring in Functions (aka Tools)
- First define the functions, either using class
Function
(see Tutorial 0), or just any Python function with input and output types defined in the signature and with a docstring - After creating your agent, use
assign_functions
to assign a list of functions of classFunction
, or general Python functions (which will be converted to AsyncFunction) - Function names will be automatically inferred if not specified
- Proceed to run tasks by using
run()
# This is an example of an LLM-based function (see Tutorial 0)
sentence_style = Function(fn_description = 'Output a sentence with words <var1> and <var2> in the style of <var3>',
output_format = {'output': 'sentence'},
fn_name = 'sentence_with_objects_entities_emotion',
llm = llm)
# This is an example of an external user-defined function (see Tutorial 0)
def binary_to_decimal(binary_number: str) -> int:
'''Converts binary_number to integer of base 10'''
return int(str(binary_number), 2)
# Initialise your Agent
my_agent = Agent('Helpful assistant', 'You are a generalist agent')
# Assign the functions
my_agent.assign_functions([sentence_style, binary_to_decimal])
# Run the Agent
output = my_agent.run('First convert binary string 1001 to a number, then generate me a happy sentence with that number and a ball')
Subtask identified: Convert the binary number 1001 to decimal
Calling function binary_to_decimal with parameters {'x': '1001'}
{'output1': 9}
Subtask identified: Generate a happy sentence with the decimal number and a ball
Calling function sentence_with_objects_entities_emotion with parameters {'obj': '9', 'entity': 'ball', 'emotion': 'happy'}
{'output': 'I am so happy with my 9 balls.'}
Task completed successfully!
-
Approach 1: Automatically Run your agent using
run()
-
Approach 2: Manually select and use functions for your task
- select_function(task: str): Based on the task, output the next function name and input parameters
- use_function(function_name: str, function_params: dict, subtask: str = '', stateful: bool = True): Uses the function named
function_name
withfunction_params
.stateful
controls whether the output of this function will be saved tosubtasks_completed
under the key ofsubtask
-
Assign/Remove Functions:
- assign_functions(function_list: list): Assigns a list of functions to the agent
- remove_function(function_name: str): Removes function named function_name from the list of assigned functions
-
Show Functions:
- list_functions(): Returns the list of functions of the agent
- print_functions(): Prints the list of functions of the agent
3. AsyncAgent
AsyncAgent
works the same way asAgent
, only much faster due to parallelisation of tasks- It can only be assigned functions of class
AsyncFunction
, or general Python functions (which will be converted to AsyncFunction) - If you define your own
AsyncFunction
, you should define the fn_name as well if it is not an External Function - As a rule of thumb, just add the
await
keyword to any function that you run with theAsyncAgent
Example LLM in Async Mode
async def llm_async(system_prompt: str, user_prompt: str):
''' Here, we use OpenAI for illustration, you can change it to your own LLM '''
# ensure your LLM imports are all within this function
from openai import AsyncOpenAI
# define your own LLM here
client = AsyncOpenAI()
response = await client.chat.completions.create(
model='gpt-4o-mini',
temperature = 0,
messages=[
{"role": "system", "content": system_prompt},
{"role": "user", "content": user_prompt}
]
)
return response.choices[0].message.content
Example Agentic Workflow
# This is an example of an LLM-based function (see Tutorial 0)
sentence_style = AsyncFunction(fn_description = 'Output a sentence with words <var1> and <var2> in the style of <var3>',
output_format = {'output': 'sentence'},
fn_name = 'sentence_with_objects_entities_emotion', # you must define fn_name for LLM-based functions
llm = llm_async) # use an async LLM function
# This is an example of an external user-defined function (see Tutorial 0)
def binary_to_decimal(binary_number: str) -> int:
'''Converts binary_number to integer of base 10'''
return int(str(binary_number), 2)
# Initialise your Agent
my_agent = AsyncAgent('Helpful assistant', 'You are a generalist agent')
# Assign the functions
my_agent.assign_functions([sentence_style, binary_to_decimal])
# Run the Agent
output = await my_agent.run('Generate me a happy sentence with a number and a ball. The number is b1001 converted to decimal')
4. Shared Variables
"Because text is not enough" - Anonymous
shared_variables
is a dictionary, that is initialised in Agent (default empty dictionary), and can be referenced by any function of the agent (including Inner Agents and their functions)- This can be useful for non-text modalitiies (e.g. audio, pdfs, image) and lengthy text modalities, which we do not want to output into
subtasks_completed
directly - To use, simply define an External Function with
shared_variables
as the first input variable, from which you can access and modifyshared_variables
directly - The agent will also be able to be self-referenced in the External Function via
shared_variables['agent']
, so you can change the agent's internal parameters viashared_variables
- If the function has no output because the output is stored in
shared_variables
, the default return value will be{'Status': 'Completed'}
Example External Function using shared_variables
# Use shared_variables as input to your external function to access and modify the shared variables
def generate_quotes(shared_variables, number_of_quotes: int, category: str):
''' Generates number_of_quotes quotes about category '''
# Retrieve from shared variables
my_quote_list = shared_variables['Quote List']
# Generate the quotes
res = strict_json(system_prompt = f'''Generate {number_of_quotes} sentences about {category}.
Do them in the format "<Quote> - <Person>", e.g. "The way to get started is to quit talking and begin doing. - Walt Disney"
Ensure your quotes contain only ' within the quote, and are enclosed by " ''',
user_prompt = '',
output_format = {'Quote List': f'list of {number_of_quotes} quotes, type: List[str]'},
llm = llm)
my_quote_list.extend([f'Category: {category}. '+ x for x in res['Quote List']])
# Store back to shared variables
shared_variables['Quote List'] = my_quote_list
5. Global Context
-
Global Context
is a very powerful feature in TaskGen, as it allows the Agent to be updated with the latest environmental state before every decision it makes -
It also allows for learnings in
shared_variables
to be carried across tasks, making the Agent teachable and learn through experiences -
A recommended practice is to always store the learnings of the Agent during the External Function call, and reset the Agent after each task, so that
subtasks_completed
will be as short as possible to avoid confusion to the Agent -
There are two ways to use
Global Context
, and both can be used concurrently:-
global_context
- If all you need in the global context is
shared_variables
without any modification to it, then you can useglobal_context
global_context
is a string with<shared_variables_name>
enclosed with<>
. These <> will be replaced with the actual variable inshared_variables
-
get_global_context
get_global_context
is a function that takes in the agent's internal parameters (self) and outputs a string to the LLM to append to the prompts of any LLM-based calls internally, e.g.get_next_subtask
,use_llm
,reply_to_user
- You have full flexibility to access anything the agent knows and process the
shared_variables
as required and configure a global prompt to the agent
-
Example for global_context
: Inventory Manager
- We can use
Global Context
to keep track of inventory state - We simply get the functions
add_item_to_inventory
andremove_item_from_inventory
to modify theshared_variable
namedInventory
- Note we can also put rule-based checks like checking if item is in inventory before removing inside the function
- Even after task reset, the Agent still knows the inventory because of
Global Context
def add_item_to_inventory(shared_variables, item: str) -> str:
''' Adds item to inventory, and returns outcome of action '''
shared_variables['Inventory'].append(item)
return f'{item} successfully added to Inventory'
def remove_item_from_inventory(shared_variables, item: str) -> str:
''' Removes item from inventory and returns outcome of action '''
if item in shared_variables['Inventory']:
shared_variables['Inventory'].remove(item)
return f'{item} successfully removed from Inventory'
else:
return f'{item} not found in Inventory, unable to remove'
agent = Agent('Inventory Manager',
'Adds and removes items in Inventory. Only able to remove items if present in Inventory',
shared_variables = {'Inventory': []},
global_context = 'Inventory: <Inventory>', # Add in Global Context here with shared_variables Inventory
llm = llm).assign_functions([add_item_to_inventory, remove_item_from_inventory])
Other Features
- There are other features like Memory (Tutorial 3), Hierarchical Agents (Tutorial 4), CodeGen and External Function Interfacing (Tutorial 5), Conversation Class (Tutorial 6)
- These extend the baseline features of TaskGen and you are encouraged to take a look at the Tutorials for more information.
Known Limitations
gpt-3.5-turbo
is not that great with mathematical functions for Agents. Usegpt-4o-mini
or better for more consistent resultsgpt-3.5-turbo
is not that great with Memory (Tutorial 3). Usegpt-4o-mini
or better for more consistent results
Contributing to the project
Test locally
- Clone the repository
- If using a virtual environment, activate it
cd
into taskgen repository- Install the package via command line
pip install -e .
- Now you can import the package and use it in your code
Submitting a pull request
- Fork the repository
- Create a new branch
- Make your changes
- Push your changes to your fork
- Submit a pull request
What are we looking out for?
- Contributing example Agents and Functions. Agents can now be contributed easily using
agent.contribute_agent()
and the entire Agent code with all the functions and memory will be converted to text-based code and put on the TaskGen GitHub. Do share your use cases actively so that other people can benefit :) These Agents can be downloaded by others viaagent.load_community_agent(agent_name)
- Jupyter Notebooks showcasing what could be done with the framework for something useful. Let your imagination guide you, we look forward to see what you create
- Other Known Limitations - Do test the framework out extensively and note its failure cases. We will see if we can address them, if not we will put them in Known Limitations.
- (For the prompt engineer). If you could find a better way to make the prompts work, let us know directly - we do need to test this out across all Tutorial Jupyter Notebooks to make sure that it really works with existing datasets. Also, if you are using other LLMs beside OpenAI, and find the prompts do not work as well - try to rejig your own prompts and let us know as well!
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for taskgen_ai-3.2.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5f148372eb6e5dafcf4d680753654fdde5bf8e50b71e129480fa2e04651ebf1f |
|
MD5 | 987c138f008871cebf06c0e8802c97c4 |
|
BLAKE2b-256 | b83c4914b7268af4f4fe54d53a15b253e2c925fd7813d0ade62dd083e4cff0b8 |