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: Optional[List[Union[Tool, Model]]] = None,
api_key: Text = config.TEAM_API_KEY,
supplier: Union[Dict, Text, Supplier, int] = "aiXplain",
version: Optional[Text] = None,
tasks: List[WorkflowTask] = None,
workflow_tasks: Optional[List[WorkflowTask]] = None,
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:
nameText - name of the agentdescriptionText - description of the agent instructions.instructionsText - instructions of the agent.llmOptional[Union[LLM, Text]], optional - LLM instance to use as an object or as an ID.llm_idOptional[Text], optional - ID of LLM to use if no LLM instance provided. Defaults to None.toolsList[Union[Tool, Model]], optional - list of tool for the agent. Defaults to [].api_keyText, optional - team/user API key. Defaults to config.TEAM_API_KEY.supplierUnion[Dict, Text, Supplier, int], optional - owner of the agent. Defaults to "aiXplain".versionOptional[Text], optional - version of the agent. Defaults to None.workflow_tasksList[WorkflowTask], optional - list of tasks for the agent. Defaults to [].description0 OutputFormat, optional - default output format for agent responses. Defaults to OutputFormat.TEXT.description1 Union[BaseModel, Text, dict], optional - expected output. Defaults to None.
Returns:
description2 - created Agent
create_from_dict
@classmethod
def create_from_dict(cls, dict: Dict) -> Agent
Create an agent instance from a dictionary representation.
Arguments:
dictDict - 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_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:
modelOptional[Union[Model, Text]], optional - Model instance or ID. Defaults to None.functionOptional[Union[Function, Text]], optional - Function enum or ID. Defaults to None.supplierOptional[Union[Supplier, Text]], optional - Supplier enum or name. Defaults to None.descriptionText, optional - Description of the tool. Defaults to "".parametersOptional[Dict], optional - Tool parameters. Defaults to None.nameOptional[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:
descriptionText - Description of what the pipeline tool does.pipelineUnion[Pipeline, Text] - Pipeline instance or pipeline ID.nameOptional[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:
codeUnion[Text, Callable] - Python code as string or callable function.nameText - Name of the tool.descriptionText, 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:
nameText - name of the tooldescriptionText - description of the database toolsourceUnion[Text, Dict] - database source - can be a connection string or dictionary with connection detailssource_typeUnion[str, DatabaseSourceType] - type of source (postgresql, sqlite, csv) or DatabaseSourceType enumschemaOptional[Text], optional - database schema descriptiontablesOptional[List[Text]], optional - table names to work with (optional)enable_commitbool, 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: Optional[Text] = None,
name: Optional[Text] = None,
api_key: Optional[Text] = None) -> Agent
Retrieve an agent by its ID or name.
Arguments:
agent_idOptional[Text], optional - ID of the agent to retrieve.nameOptional[Text], optional - Name of the agent to retrieve.api_keyOptional[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.ValueError- If neither agent_id nor name is provided, or if both are provided.