TaskGen Function Usage Guide#
This guide details how to define and utilize functions within TaskGen agents, enabling them to execute tasks efficiently.
Define a LLM Function#
To enable your agent to interact with a large language model (LLM), define a function specifying how the agent should communicate with the LLM.
def llm(system_prompt: str, user_prompt: str) -> str:
"""
Interacts with a specified LLM using system and user prompts to generate a response.
:param system_prompt: A string that provides the context or instruction for the LLM.
:param user_prompt: A string that represents the user's input or question.
:return: A string response generated by the LLM.
"""
from openai import ChatCompletion
response = ChatCompletion.create(
model="gpt-4o-mini",
messages=[
{"role": "system", "content": system_prompt},
{"role": "user", "content": user_prompt}
]
)
return response.choices[0].message.content
Using Functions in Agents#
Once functions are defined, they can be assigned to agents to carry out specific tasks.
Creating an Agent:
Initialize an agent and assign it a function for interaction.
from taskgen import Agent my_agent = Agent(name='HelperBot', description='A versatile helper agent.', llm=llm)
Assigning Tasks:
Assign tasks to the agent using the defined functions, and specify the number of subtasks if necessary.
task_output = my_agent.run(task="Schedule a meeting", num_subtasks=3) print(task_output)
Interacting with Tasks:
Use the agent’s capabilities to interact dynamically with ongoing tasks.
response = my_agent.interact(user_query="Reschedule the meeting to a later date.") print(response)
Advanced Function Usage#
Define advanced behaviors and interactions by leveraging the full capabilities of TaskGen functions.
def advanced_interaction(system_prompt: str, user_input: str) -> str:
"""
An advanced function to handle more complex interactions and provide detailed responses based on the context.
:param system_prompt: Contextual prompt for the LLM.
:param user_input: User's specific query or command.
:return: Detailed response from the LLM based on the interaction.
"""
response = llm(system_prompt, user_input)
return response # Modify or process the response as needed for the task
Conclusion#
Utilizing these functions within your TaskGen agents allows for flexible, powerful task management and response generation tailored to specific user needs and contexts.