The Agent class is a core component of the ReXia.AI framework. It is responsible for running workflows, reflecting on tasks, and interacting with language models to achieve desired outcomes. This documentation provides a detailed overview of the Agent class, its attributes, methods, and usage, including the new task complexity routing feature.
- Installation
- Usage
- Class Attributes
- Methods
- Task Complexity Routing
- Examples
- Dependencies
- Contributing
- License
To use the Agent class, ensure you have the necessary dependencies installed. You can install them using pip:
pip install rexia_aiTo create and use an Agent instance, follow the steps below:
from rexia_ai.agent import Agent
from rexia_ai.workflows import ReflectWorkflow
from rexia_ai.llms import RexiaAIOpenAI
# Initialize the language models
base_llm = RexiaAIOpenAI(...)
router_llm = RexiaAIOpenAI(...)
complex_llm = RexiaAIOpenAI(...)
# Create an Agent instance with routing
agent = Agent(
llm=base_llm,
task="Your task description",
workflow=ReflectWorkflow,
verbose=True,
use_router=True,
router_llm=router_llm,
complex_llm=complex_llm,
task_complexity_threshold=50
)
# Invoke the agent to perform the task
result = agent.invoke()
print(result)workflow: The workflow used by the agent.task: The task assigned to the agent.llm: The language model used by the agent.verbose: A flag for enabling verbose mode.max_attempts: The maximum number of attempts to get a valid response from the model.router: The TaskComplexityRouter instance, if routing is enabled.task_complexity: The complexity score of the current task, if routing is enabled.
__init__(self, llm: RexiaAIOpenAI, task: str, workflow: Optional[Type[BaseWorkflow]] = None, verbose: bool = False, max_attempts: int = 3, use_router: bool = False, router_llm: Optional[RexiaAIOpenAI] = None, complex_llm: Optional[RexiaAIOpenAI] = None, task_complexity_threshold: int = 50)
Initializes an Agent instance.
Parameters:
llm: The base language model used by the agent.task: The task assigned to the agent.workflow: The class of the workflow to be used by the agent. Defaults toReflectWorkflow.verbose: A flag for enabling verbose mode. Defaults toFalse.max_attempts: The maximum number of attempts to get a valid response from the model. Defaults to3.use_router: Whether to use the task complexity router. Defaults toFalse.router_llm: The language model used for the router. Required ifuse_routerisTrue.complex_llm: The language model used for complex tasks. Required ifuse_routerisTrue.task_complexity_threshold: The threshold for determining when to use the complex model. Defaults to50.
Runs the workflow and returns the messages.
Extracts the task result from the messages.
Invokes the agent to perform the task. If a new task is provided, it updates the current task and recalculates the complexity if routing is enabled.
Formats the accepted answer by removing any single word before the JSON object.
The Agent class now supports task complexity routing, which allows it to dynamically choose between a base language model and a more complex model based on the assessed complexity of the task.
- When
use_routeris set toTrue, aTaskComplexityRouterinstance is created. - The router uses the
router_llmto assess the complexity of the given task. - If the complexity score exceeds the
task_complexity_threshold, thecomplex_llmis used for the task. Otherwise, thebase_llmis used. - The complexity assessment is performed each time a new task is provided to the
invokemethod.
- Efficient resource allocation: Simple tasks use the faster, less resource-intensive base model.
- Improved performance on complex tasks: Difficult tasks are handled by the more capable complex model.
- Dynamic adaptation: The agent can switch between models as task complexity changes.
Here's an example of how to use the Agent class with task complexity routing:
from rexia_ai.agent import Agent
from rexia_ai.llms import RexiaAIOpenAI
# Initialize the language models
base_llm = RexiaAIOpenAI(base_url="http://localhost:1234/v1", model="yi-1.5", temperature=0)
router_llm = RexiaAIOpenAI(base_url="http://localhost:1234/v1", model="yi-1.5", temperature=0)
complex_llm = RexiaAIOpenAI(base_url="https://api.01.ai/v1", model="yi-large", temperature=0, api_key=YI_LARGE_API_KEY)
# Create an Agent instance with routing
agent = Agent(
llm=base_llm,
task="What is the capital of France?",
verbose=True,
use_router=True,
router_llm=router_llm,
complex_llm=complex_llm,
task_complexity_threshold=50
)
# Invoke the agent with a simple task
response = agent.invoke()
print("Response:", response)
# Invoke the agent with a more complex task
complex_task = """Write a 500-word explanation of how photosynthesis works in plants,
including the light-dependent and light-independent reactions. Describe the role of chlorophyll,
the importance of water and carbon dioxide, and how the process produces glucose and oxygen.
Then, discuss how this process impacts the global carbon cycle and its significance for life on Earth."""
response = agent.invoke(task=complex_task)
print("Response:", response)rexia-airetyping
Ensure all dependencies are installed to avoid any issues.
We welcome contributions to improve the ReXia.AI framework. Please follow these steps to contribute:
- Fork the repository.
- Create a new branch (
git checkout -b feature-branch). - Make your changes.
- Commit your changes (
git commit -m 'Add new feature'). - Push to the branch (
git push origin feature-branch). - Create a new Pull Request.
This project is licensed under the MIT License. See the LICENSE file for details.