Taskgen Module#
- class taskgen.Agent(*args, **kwargs)#
Bases:
BaseAgent- assign_agent(agent_list)#
Assigns a list of Agents to the main agent, passing in the meta agent as well
- assign_agents(agent_list)#
Assigns a list of Agents to the main agent, passing in the meta agent as well
- assign_function(function_list)#
Assigns a list of functions to be used in function_map
- assign_functions(function_list)#
Assigns a list of functions to be used in function_map
- assign_tool(function_list)#
Assigns a list of functions to be used in function_map
- assign_tools(function_list)#
Assigns a list of functions to be used in function_map
- get_next_subtask(task='')#
Based on what the task is and the subtasks completed, we get the next subtask, function and input parameters. Supports user-given task as well if user wants to use this function directly
- classmethod load_community_agent(agent_name)#
- query(query, output_format, provide_function_list=False, task='')#
Queries the agent with a query and outputs in output_format. If task is provided, we will filter the functions according to the task If you want to provide the agent with the context of functions available to it, set provide_function_list to True (default: False) If task is given, then we will use it to do RAG over functions
- reply_user(query='', stateful=True, verbose=True)#
Generate a reply to the user based on the query / agent task and subtasks completed If stateful, also store this interaction into the subtasks_completed If verbose is given, can also override the verbosity of this function
- run(task='', overall_task='', num_subtasks=0)#
Attempts to do the task using LLM and available functions Loops through and performs either a function call or LLM call up to num_subtasks number of times If overall_task is filled, then we store it to pass to the inner agents for more context
- Return type:
- select_function(task='')#
Based on the task (without any context), output the next function name and input parameters
- select_tool(task='')#
Based on the task (without any context), output the next function name and input parameters
- summarise_subtasks_completed(task='')#
Summarise the subtasks_completed list according to task
- to_function(meta_agent)#
Converts the agent to a function so that it can be called by another agent The agent will take in an instruction, and output the result after processing
- use_agent(agent_name, agent_task)#
Uses an inner agent to do a task for the meta agent. Task outcome goes directly to subtasks_completed of meta agent
- use_function(function_name, function_params, subtask='', stateful=True)#
Uses the function. stateful means we store the outcome of the function
- use_tool(function_name, function_params, subtask='', stateful=True)#
Uses the function. stateful means we store the outcome of the function
- class taskgen.AsyncAgent(*args, **kwargs)#
Bases:
BaseAgent- assign_agent(agent_list)#
Assigns a list of Agents to the main agent, passing in the meta agent as well
- assign_agents(agent_list)#
Assigns a list of Agents to the main agent, passing in the meta agent as well
- assign_function(function_list)#
Assigns a list of functions to be used in function_map
- assign_functions(function_list)#
Assigns a list of functions to be used in function_map
- assign_tool(function_list)#
Assigns a list of functions to be used in function_map
- assign_tools(function_list)#
Assigns a list of functions to be used in function_map
- async get_next_subtask(task='')#
Based on what the task is and the subtasks completed, we get the next subtask, function and input parameters. Supports user-given task as well if user wants to use this function directly
- async query(query, output_format, provide_function_list=False, task='')#
Queries the agent with a query and outputs in output_format. If task is provided, we will filter the functions according to the task If you want to provide the agent with the context of functions available to it, set provide_function_list to True (default: False) If task is given, then we will use it to do RAG over functions
- async reply_user(query='', stateful=True, verbose=True)#
Generate a reply to the user based on the query / agent task and subtasks completed If stateful, also store this interaction into the subtasks_completed If verbose is given, can also override the verbosity of this function
- async run(task='', overall_task='', num_subtasks=0)#
Attempts to do the task using LLM and available functions Loops through and performs either a function call or LLM call up to num_subtasks number of times If overall_task is filled, then we store it to pass to the inner agents for more context
- Return type:
- async select_function(task='')#
Based on the task (without any context), output the next function name and input parameters
- async select_tool(task='')#
Based on the task (without any context), output the next function name and input parameters
- async summarise_subtasks_completed(task='')#
Summarise the subtasks_completed list according to task
- to_function(meta_agent)#
Converts the agent to a function so that it can be called by another agent The agent will take in an instruction, and output the result after processing
- async use_agent(agent_name, agent_task)#
Uses an inner agent to do a task for the meta agent. Task outcome goes directly to subtasks_completed of meta agent
- async use_function(function_name, function_params, subtask='', stateful=True)#
Uses the function. stateful means we store the outcome of the function
- async use_tool(function_name, function_params, subtask='', stateful=True)#
Uses the function. stateful means we store the outcome of the function
- class taskgen.AsyncChromaDbMemory(*args, **kwargs)#
Bases:
BaseChromaDbMemory- async add_file(filepath, text_splitter=None)#
- async append(new_memories=[], mapper=None)#
Appends multiple new memories
- async append_memory_list(new_memories, metadatas=None)#
- async create_embedding(text)#
- get_openai_client()#
- async get_or_create_collection()#
- async remove(memories)#
Removes an existing_memory. existing_memory can be str, or triplet if it is a Knowledge Graph
- async remove_by_id(ids=[])#
- async reset()#
Clears all memories
- async retrieve(task, filter=[])#
Retrieves some memories according to task
- class taskgen.AsyncFunction(*args, **kwargs)#
Bases:
BaseFunction- async async_init()#
This generates the name for the function using strict_json_async
- class taskgen.AsyncMemory(*args, **kwargs)#
Bases:
BaseMemoryRetrieves top k memory items based on task - Inputs:
memory: List. Default: Empty List. The list containing the memory items
top_k: Int. Default: 3. The number of memory list items to retrieve
- mapper: Function. Maps the memory item to another form for comparison by ranker or LLM. Default: lambda x: x
Example mapping: lambda x: x.fn_description (If x is a Class and the string you want to compare for similarity is the fn_description attribute of that class)
- approach: str. Either retrieve_by_ranker or retrieve_by_llm to retrieve memory items
Ranker is faster and cheaper as it compares via embeddings, but are inferior to LLM-based methods for contextual information
llm: Function. The llm to use for strict_json llm retriever
retrieve_fn: Default: None. Takes in task and outputs top_k similar memories in a list
- ranker: Ranker. The Ranker which defines a similarity score between a query and a key. Default: OpenAI text-embedding-3-small model.
Can be replaced with a function which returns similarity score from 0 to 1 when given a query and key
- async retrieve(task)#
Performs retrieval of top_k similar memories according to approach stated
- Return type:
- async retrieve_by_llm(task)#
Performs retrieval via LLMs Returns the key list as well as the value list
- Return type:
- class taskgen.AsyncRanker(model='text-embedding-3-small', ranking_fn=None, database=None)#
Bases:
BaseRanker- async get_or_create_embedding_async(text, client)#
- class taskgen.ChromaDbMemory(*args, **kwargs)#
Bases:
BaseChromaDbMemory- add_file(filepath, text_splitter=None)#
- append(new_memories, mapper=None)#
Appends multiple new memories
- append_memory_list(new_memories, metadatas=None)#
- create_embedding(text)#
- get_openai_client()#
- get_or_create_collection()#
- class taskgen.ConversableAgent(agent, persistent_memory=None, person='User', conversation=None, num_past_conversation=5, verbose=True)#
Bases:
objectThis class takes an Agent and allows for conversational-based interactions with User / another Agent / Environment. Also updates persistent memory with latest information in conversation
- Inputs:
agent (compulsory): Agent. The agent we want to interact with
persistent_memory: dict. What kinds of memory the agent should have that persist over the entire conversation and their descriptions. Uses the same format as output_format of strict_json.
person: str. The name of the person you are talking to
conversation: List. The current existing conversation. Default: None
num_past_conversation: int. The number of past conversations to use for the agent
verbose: bool. Default: True. Whether to print the Agent’s inner states
- ConversableAgent will automatically implement 3 new variables in agent.shared_variables:
Persistent Memory: The memory that will be updated as the conversation goes along, defined in persistent_dict
Conversation: The entire history of the conversationn
Summary of Conversation: A summary of the current conversation
ConversableAgent uses chat() which chats with the Agent and the Agent will perform actions and reply the chat message
- chat(cur_msg)#
This does one chat with the person, firstly performing actions then replying the person, while updating the important memory
- verbose#
Define some external variables for the Agent
- class taskgen.ConversationWrapper(agent, persistent_memory=None, person='User', conversation=None, num_past_conversation=5, verbose=True)#
Bases:
AgentThis class takes an Agent and allows for conversational-based interactions with User / another Agent / Environment. Also updates persistent memory with latest information in conversation
- Inputs:
agent (compulsory): Agent. The agent we want to interact with
persistent_memory: dict. What kinds of memory the agent should have that persist over the entire conversation and their descriptions. Uses the same format as output_format of strict_json.
person: str. The name of the person you are talking to
conversation: List. The current existing conversation. Default: None
num_past_conversation: int. The number of past conversations to use for the agent
verbose: bool. Default: True. Whether to print the Agent’s inner states
- ConversationWrapper will automatically implement 3 new variables in agent.shared_variables:
Persistent Memory: The memory that will be updated as the conversation goes along, defined in persistent_dict
Conversation: The entire history of the conversation
Summary of Conversation: A summary of the current conversation
ConversationWrapper uses chat() which chats with the Agent and the Agent will perform actions and reply the chat message
- chat(cur_msg)#
This does one chat with the person, firstly performing actions then replying the person, while updating the important memory
- verbose#
Define some external variables for the Agent
- class taskgen.Function(*args, **kwargs)#
Bases:
BaseFunction
- class taskgen.Memory(*args, **kwargs)#
Bases:
BaseMemoryRetrieves top k memory items based on task - Inputs:
memory: List. Default: Empty List. The list containing the memory items
top_k: Int. Default: 3. The number of memory list items to retrieve
- mapper: Function. Maps the memory item to another form for comparison by ranker or LLM. Default: lambda x: x
Example mapping: lambda x: x.fn_description (If x is a Class and the string you want to compare for similarity is the fn_description attribute of that class)
- approach: str. Either retrieve_by_ranker or retrieve_by_llm to retrieve memory items
Ranker is faster and cheaper as it compares via embeddings, but are inferior to LLM-based methods for contextual information
llm: Function. The llm to use for strict_json llm retriever
retrieve_fn: Default: None. Takes in task and outputs top_k similar memories in a list
- ranker: Ranker. The Ranker which defines a similarity score between a query and a key. Default: OpenAI text-embedding-3-small model.
Can be replaced with a function which returns similarity score from 0 to 1 when given a query and key
- retrieve(task)#
Performs retrieval of top_k similar memories according to approach stated
- Return type:
- retrieve_by_llm(task)#
Performs retrieval via LLMs Returns the key list as well as the value list
- Return type:
- class taskgen.MemoryTemplate#
Bases:
ABCA generic template provided for all memories
- abstract append(memory_list, mapper=None)#
Appends multiple new memories
- read_docx(filepath)#
- read_file(filepath, text_splitter=None)#
- read_pdf(filepath)#
- abstract remove(existing_memory)#
Removes an existing_memory. existing_memory can be str, or triplet if it is a Knowledge Graph
- abstract reset()#
Clears all memories
- abstract retrieve(task)#
Retrieves some memories according to task
- class taskgen.Ranker(model='text-embedding-3-small', ranking_fn=None, database=None)#
Bases:
BaseRanker- get_or_create_embedding(text, client)#
- taskgen.chat(system_prompt, user_prompt, model='gpt-4o-mini', temperature=0, verbose=False, host='openai', llm=None, **kwargs)#
Performs a chat with the host’s LLM model with system prompt, user prompt, model, verbose and kwargs Returns the output string res - system_prompt: String. Write in whatever you want the LLM to become. e.g. “You are a <purpose in life>” - user_prompt: String. The user input. Later, when we use it as a function, this is the function input - model: String. The LLM model to use for json generation - verbose: Boolean (default: False). Whether or not to print out the system prompt, user prompt, GPT response - host: String. The provider of the LLM - llm: User-made llm function.
- Inputs:
system_prompt: String. Write in whatever you want the LLM to become. e.g. “You are a <purpose in life>”
user_prompt: String. The user input. Later, when we use it as a function, this is the function input
- Output:
res: String. The response of the LLM call
**kwargs: Dict. Additional arguments for LLM chat
- async taskgen.chat_async(system_prompt, user_prompt, model='gpt-4o-mini', temperature=0, verbose=False, host='openai', llm=None, **kwargs)#
Performs a chat with the host’s LLM model with system prompt, user prompt, model, verbose and kwargs Returns the output string res - system_prompt: String. Write in whatever you want the LLM to become. e.g. “You are a <purpose in life>” - user_prompt: String. The user input. Later, when we use it as a function, this is the function input - model: String. The LLM model to use for json generation - verbose: Boolean (default: False). Whether or not to print out the system prompt, user prompt, GPT response - host: String. The provider of the LLM - llm: User-made llm function.
- Inputs:
system_prompt: String. Write in whatever you want the LLM to become. e.g. “You are a <purpose in life>”
user_prompt: String. The user input. Later, when we use it as a function, this is the function input
- Output:
res: String. The response of the LLM call
**kwargs: Dict. Additional arguments for LLM chat
- taskgen.strict_json(system_prompt, user_prompt, output_format, return_as_json=False, custom_checks=None, check_data=None, delimiter='###', num_tries=3, openai_json_mode=False, **kwargs)#
Ensures that OpenAI will always adhere to the desired output JSON format defined in output_format. Uses rule-based iterative feedback to ask GPT to self-correct. Keeps trying up to num_tries it it does not. Returns empty JSON if unable to after num_tries iterations.
Inputs (compulsory): - system_prompt: String. Write in whatever you want GPT to become. e.g. “You are a <purpose in life>” - user_prompt: String. The user input. Later, when we use it as a function, this is the function input - output_format: Dict. JSON format with the key as the output key, and the value as the output description
Inputs (optional): - return_as_json: Bool. Default: False. Whether to return the output as a json. If False, returns as Python dict. If True, returns as json string - custom_checks: Dict. Key is output key, value is function which does checking of content for output field - check_data: Any data type. The additional data for custom_checks to use if required - delimiter: String (Default: ‘###’). This is the delimiter to surround the keys. With delimiter ###, key becomes ###key### - num_tries: Integer (default: 3). The number of tries to iteratively prompt GPT to generate correct json format - openai_json_mode: Boolean (default: False). Whether or not to use OpenAI JSON Mode - **kwargs: Dict. Additional arguments for LLM chat
Output: - res: Dict. The JSON output of the model. Returns {} if JSON parsing failed.
- async taskgen.strict_json_async(system_prompt, user_prompt, output_format, return_as_json=False, custom_checks=None, check_data=None, delimiter='###', num_tries=3, openai_json_mode=False, **kwargs)#
Ensures that OpenAI will always adhere to the desired output JSON format defined in output_format. Uses rule-based iterative feedback to ask GPT to self-correct. Keeps trying up to num_tries it it does not. Returns empty JSON if unable to after num_tries iterations.
Inputs (compulsory): - system_prompt: String. Write in whatever you want GPT to become. e.g. “You are a <purpose in life>” - user_prompt: String. The user input. Later, when we use it as a function, this is the function input - output_format: Dict. JSON format with the key as the output key, and the value as the output description
Inputs (optional): - return_as_json: Bool. Default: False. Whether to return the output as a json. If False, returns as Python dict. If True, returns as json string - custom_checks: Dict. Key is output key, value is function which does checking of content for output field - check_data: Any data type. The additional data for custom_checks to use if required - delimiter: String (Default: ‘###’). This is the delimiter to surround the keys. With delimiter ###, key becomes ###key### - num_tries: Integer (default: 3). The number of tries to iteratively prompt GPT to generate correct json format - openai_json_mode: Boolean (default: False). Whether or not to use OpenAI JSON Mode - **kwargs: Dict. Additional arguments for LLM chat
Output: - res: Dict. The JSON output of the model. Returns {} if JSON parsing failed.
- taskgen.strict_output(system_prompt, user_prompt, output_format, return_as_json=False, custom_checks=None, check_data=None, delimiter='###', num_tries=3, openai_json_mode=False, **kwargs)#
Ensures that OpenAI will always adhere to the desired output JSON format defined in output_format. Uses rule-based iterative feedback to ask GPT to self-correct. Keeps trying up to num_tries it it does not. Returns empty JSON if unable to after num_tries iterations.
Inputs (compulsory): - system_prompt: String. Write in whatever you want GPT to become. e.g. “You are a <purpose in life>” - user_prompt: String. The user input. Later, when we use it as a function, this is the function input - output_format: Dict. JSON format with the key as the output key, and the value as the output description
Inputs (optional): - return_as_json: Bool. Default: False. Whether to return the output as a json. If False, returns as Python dict. If True, returns as json string - custom_checks: Dict. Key is output key, value is function which does checking of content for output field - check_data: Any data type. The additional data for custom_checks to use if required - delimiter: String (Default: ‘###’). This is the delimiter to surround the keys. With delimiter ###, key becomes ###key### - num_tries: Integer (default: 3). The number of tries to iteratively prompt GPT to generate correct json format - openai_json_mode: Boolean (default: False). Whether or not to use OpenAI JSON Mode - **kwargs: Dict. Additional arguments for LLM chat
Output: - res: Dict. The JSON output of the model. Returns {} if JSON parsing failed.
- taskgen.strict_text(system_prompt, user_prompt, output_format, return_as_json=False, custom_checks=None, check_data=None, delimiter='###', num_tries=3, openai_json_mode=False, **kwargs)#
Ensures that OpenAI will always adhere to the desired output JSON format defined in output_format. Uses rule-based iterative feedback to ask GPT to self-correct. Keeps trying up to num_tries it it does not. Returns empty JSON if unable to after num_tries iterations.
Inputs (compulsory): - system_prompt: String. Write in whatever you want GPT to become. e.g. “You are a <purpose in life>” - user_prompt: String. The user input. Later, when we use it as a function, this is the function input - output_format: Dict. JSON format with the key as the output key, and the value as the output description
Inputs (optional): - return_as_json: Bool. Default: False. Whether to return the output as a json. If False, returns as Python dict. If True, returns as json string - custom_checks: Dict. Key is output key, value is function which does checking of content for output field - check_data: Any data type. The additional data for custom_checks to use if required - delimiter: String (Default: ‘###’). This is the delimiter to surround the keys. With delimiter ###, key becomes ###key### - num_tries: Integer (default: 3). The number of tries to iteratively prompt GPT to generate correct json format - openai_json_mode: Boolean (default: False). Whether or not to use OpenAI JSON Mode - **kwargs: Dict. Additional arguments for LLM chat
Output: - res: Dict. The JSON output of the model. Returns {} if JSON parsing failed.