aixplain.factories.agent_factory
__author__
Copyright 2024 The aiXplain SDK authors
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
Author: Thiago Castro Ferreira and Lucas Pavanelli Date: May 16th 2024 Description: Agent Factory Class
AgentFactory Objects
class AgentFactory()
Factory class for creating and managing agents in the aiXplain system.
This class provides class methods for creating various types of agents and tools, as well as managing existing agents in the platform.
create
@classmethod
def create(
cls,
name: Text,
description: Text,
instructions: Optional[Text] = None,
llm: Optional[Union[LLM, Text]] = None,
llm_id: Optional[Text] = None,
tools: List[Union[Tool, Model]] = [],
api_key: Text = config.TEAM_API_KEY,
supplier: Union[Dict, Text, Supplier, int] = "aiXplain",
version: Optional[Text] = None,
tasks: List[AgentTask] = [],
output_format: Optional[OutputFormat] = None,
expected_output: Optional[Union[BaseModel, Text,
dict]] = None) -> Agent
Create a new agent in the platform.
Warnings:
The 'instructions' parameter was recently added and serves the same purpose as 'description' did previously: set the role of the agent as a system prompt. The 'description' parameter is still required and should be used to set a short summary of the agent's purpose. For the next releases, the 'instructions' parameter will be required.
Arguments:
name
Text - name of the agentdescription
Text - description of the agent role.instructions
Text - instructions of the agent.llm
Optional[Union[LLM, Text]], optional - LLM instance to use as an object or as an ID.llm_id
Optional[Text], optional - ID of LLM to use if no LLM instance provided. Defaults to None.tools
List[Union[Tool, Model]], optional - list of tool for the agent. Defaults to [].api_key
Text, optional - team/user API key. Defaults to config.TEAM_API_KEY.supplier
Union[Dict, Text, Supplier, int], optional - owner of the agent. Defaults to "aiXplain".version
Optional[Text], optional - version of the agent. Defaults to None.tasks
List[AgentTask], optional - list of tasks for the agent. Defaults to [].description
0 OutputFormat, optional - default output format for agent responses. Defaults to OutputFormat.TEXT.description
1 Union[BaseModel, Text, dict], optional - expected output. Defaults to None.
Returns:
description
2 - created Agent
create_from_dict
@classmethod
def create_from_dict(cls, dict: Dict) -> Agent
Create an agent instance from a dictionary representation.
Arguments:
dict
Dict - Dictionary containing agent configuration and properties.
Returns:
Agent
- Instantiated agent object with properties from the dictionary.
Raises:
Exception
- If agent validation fails or required properties are missing.
create_task
@classmethod
def create_task(cls,
name: Text,
description: Text,
expected_output: Text,
dependencies: Optional[List[Text]] = None) -> AgentTask
Create a new task for an agent.
Arguments:
name
Text - Name of the task.description
Text - Description of what the task should accomplish.expected_output
Text - Description of the expected output format.dependencies
Optional[List[Text]], optional - List of task names that must complete before this task can start. Defaults to None.
Returns:
AgentTask
- Created task object.
create_model_tool
@classmethod
def create_model_tool(cls,
model: Optional[Union[Model, Text]] = None,
function: Optional[Union[Function, Text]] = None,
supplier: Optional[Union[Supplier, Text]] = None,
description: Text = "",
parameters: Optional[Dict] = None,
name: Optional[Text] = None) -> ModelTool
Create a new model tool for use with an agent.
Arguments:
model
Optional[Union[Model, Text]], optional - Model instance or ID. Defaults to None.function
Optional[Union[Function, Text]], optional - Function enum or ID. Defaults to None.supplier
Optional[Union[Supplier, Text]], optional - Supplier enum or name. Defaults to None.description
Text, optional - Description of the tool. Defaults to "".parameters
Optional[Dict], optional - Tool parameters. Defaults to None.name
Optional[Text], optional - Name of the tool. Defaults to None.
Returns:
ModelTool
- Created model tool object.
Raises:
AssertionError
- If the supplier is not valid.
create_pipeline_tool
@classmethod
def create_pipeline_tool(cls,
description: Text,
pipeline: Union[Pipeline, Text],
name: Optional[Text] = None) -> PipelineTool
Create a new pipeline tool for use with an agent.
Arguments:
description
Text - Description of what the pipeline tool does.pipeline
Union[Pipeline, Text] - Pipeline instance or pipeline ID.name
Optional[Text], optional - Name of the tool. Defaults to None.
Returns:
PipelineTool
- Created pipeline tool object.
create_python_interpreter_tool
@classmethod
def create_python_interpreter_tool(cls) -> PythonInterpreterTool
Create a new Python interpreter tool for use with an agent.
This tool allows the agent to execute Python code in a controlled environment.
Returns:
PythonInterpreterTool
- Created Python interpreter tool object.
create_custom_python_code_tool
@classmethod
def create_custom_python_code_tool(
cls,
code: Union[Text, Callable],
name: Text,
description: Text = "") -> CustomPythonCodeTool
Create a new custom Python code tool for use with an agent.
Arguments:
code
Union[Text, Callable] - Python code as string or callable function.name
Text - Name of the tool.description
Text, optional - Description of what the tool does. Defaults to "".
Returns:
CustomPythonCodeTool
- Created custom Python code tool object.
create_sql_tool
@classmethod
def create_sql_tool(cls,
name: Text,
description: Text,
source: str,
source_type: Union[str, DatabaseSourceType],
schema: Optional[Text] = None,
tables: Optional[List[Text]] = None,
enable_commit: bool = False) -> SQLTool
Create a new SQL tool
Arguments:
name
Text - name of the tooldescription
Text - description of the database toolsource
Union[Text, Dict] - database source - can be a connection string or dictionary with connection detailssource_type
Union[str, DatabaseSourceType] - type of source (postgresql, sqlite, csv) or DatabaseSourceType enumschema
Optional[Text], optional - database schema descriptiontables
Optional[List[Text]], optional - table names to work with (optional)enable_commit
bool, optional - enable to modify the database (optional)
Returns:
SQLTool
- created SQLTool
Examples:
CSV - Simple
sql_tool = AgentFactory.create_sql_tool( description="My CSV Tool", source="/path/to/data.csv", source_type="csv", tables=["data"] )
SQLite - Simple
sql_tool = AgentFactory.create_sql_tool( description="My SQLite Tool", source="/path/to/database.sqlite", source_type="sqlite", tables=["users", "products"] )
list
@classmethod
def list(cls) -> Dict
List all agents available in the platform.
Returns:
Dict
- Dictionary containing:- results (List[Agent]): List of available agents.
- page_total (int): Number of agents in current page.
- page_number (int): Current page number.
- total (int): Total number of agents.
Raises:
Exception
- If there is an error listing the agents.
get
@classmethod
def get(cls, agent_id: Text, api_key: Optional[Text] = None) -> Agent
Retrieve an agent by its ID.
Arguments:
agent_id
Text - ID of the agent to retrieve.api_key
Optional[Text], optional - API key for authentication. Defaults to None, using the configured TEAM_API_KEY.
Returns:
Agent
- Retrieved agent object.
Raises:
Exception
- If the agent cannot be retrieved or doesn't exist.