Tool Manager¶
- class oscopilot.tool_repository.manager.tool_manager.ToolManager(generated_tool_repo_dir=None)[source]¶
Bases:
objectManages tools within a repository, including adding, deleting, and retrieving tool information.
The ToolManager class provides a comprehensive interface for managing a collection of tools, where each tool is associated with its code, description, and other metadata. It supports operations such as adding new tools, checking for the existence of tools, retrieving tool names, descriptions, and codes, and deleting tools from the collection. It leverages a vector database for efficient retrieval of tools based on similarity searches.
- generated_tools¶
Stores the mapping relationship between tool names and their information (code, description).
- Type:
dict
- generated_tool_repo_dir¶
The directory path where the tools’ information is stored, including code files, description files, and a JSON file containing the tools’ metadata.
- Type:
str
- vectordb_path¶
The path to the vector database used for storing and retrieving tool descriptions based on similarity.
- Type:
str
- vectordb¶
An instance of the Chroma class for managing the vector database.
- Type:
Chroma
Note
The class uses OpenAI’s text-embedding-ada-002 model by default for generating embeddings via the OpenAIEmbeddings wrapper. Ensure that the OPENAI_API_KEY and OPENAI_ORGANIZATION are correctly set for OpenAI API access.
This class is designed to facilitate the management of a dynamic collection of tools, providing functionalities for easy addition, retrieval, and deletion of tools. It ensures that the tools’ information is synchronized across a local repository and a vector database for efficient retrieval based on content similarity.
- add_new_tool(info)[source]¶
Adds a new tool to the tool manager, including updating the vector database and tool repository with the provided information.
This method processes the given tool information, which includes the task name, code, and description. It prints out the task name and description, checks if the tool already exists (rewriting it if so), and updates both the vector database and the tool dictionary. Finally, it persists the new tool’s code and description in the repository and ensures the vector database is synchronized with the generated tools.
- Parameters:
info (dict) – A dictionary containing the tool’s information, which must include ‘task_name’, ‘code’, and ‘description’.
- Raises:
AssertionError – If the vector database’s count does not match the length of the generated_tools dictionary after adding the new tool, indicating a synchronization issue.
- delete_tool(tool)[source]¶
Deletes all information related to a specified tool from the tool manager.
This method removes the tool’s information from the vector database, the generated_tools.json file, and also deletes the tool’s code and description files from the repository. It performs the deletion only if the tool exists in the respective storage locations and provides console feedback for each successful deletion action.
- Parameters:
tool (str) – The name of the tool to be deleted.
Note
This method assumes that the tool’s information is stored in a structured manner within the tool manager’s repository, including a separate code file (.py), a description text file (.txt), and an arguments description text file (.txt), all named after the tool.
- property descriptions¶
Retrieve the descriptions of all tools in a dictionary.
This property constructs a dictionary where each key is a tool name and its value is the description of that tool, extracted from the generated_tools dictionary.
- Returns:
A dictionary mapping each tool name to its description.
- Return type:
dict
- exist_tool(tool)[source]¶
Checks if a tool exists in the tool manager based on the tool name.
- Parameters:
tool (str) – The name of the tool to check.
- Returns:
True if the tool exists, False otherwise.
- Return type:
bool
- get_tool_code(tool_name)[source]¶
Retrieve the code of a specific tool by its name.
Given a tool name, this method fetches and returns the code associated with that tool from the generated_tools dictionary. If the tool does not exist, a KeyError will be raised.
- Parameters:
tool_name (str) – The name of the tool for which the code is requested.
- Returns:
The code of the specified tool.
- Return type:
str
- Raises:
KeyError – If the tool_name does not exist in the generated_tools dictionary.
- property programs¶
Retrieve all the code from the code repository as a single string.
This property concatenates the code of all tools stored in the generated_tools dictionary, separating each tool’s code with two newlines.
- Returns:
A string containing the code of all tools, each separated by two newlines.
- Return type:
str
- retrieve_tool_code(tool_name)[source]¶
Returns the code of specified tools based on their names.
Similar to retrieving tool descriptions, this method iterates over a list of tool names and retrieves the code for each tool from the generated_tools dictionary. It then compiles and returns a list of these codes.
- Parameters:
tool_name (list[str]) – A list of tool names for which code snippets are requested.
- Returns:
A list containing the code of the specified tools.
- Return type:
list[str]
- retrieve_tool_description(tool_name)[source]¶
Returns the descriptions of specified tools based on their names.
This method iterates over a list of tool names and retrieves the description for each tool from the generated_tools dictionary. It compiles and returns a list of these descriptions.
- Parameters:
tool_name (list[str]) – A list of tool names for which descriptions are requested.
- Returns:
A list containing the descriptions of the specified tools.
- Return type:
list[str]
- retrieve_tool_name(query, k=10)[source]¶
Retrieves related tool names based on a similarity search against a query.
This method performs a similarity search in the vector database for the given query and retrieves the names of the top k most similar tools. It prints the number of tools being retrieved and their names.
- Parameters:
query (str) – The query string to search for similar tools.
k (int, optional) – The maximum number of similar tools to retrieve. Defaults to 10.
- Returns:
- A list of tool names that are most similar to the query,
up to k tools. Returns an empty list if no tools are found or if k is 0.
- Return type:
list[str]
- property tool_names¶
Retrieve all tool class names from the generated tools.
This property provides access to the names of all tools stored in the generated_tools dictionary, facilitating enumeration over tool names.
- Returns:
A view of the dictionary’s keys which are the names of the tools.
- Return type:
KeysView[str]
- class oscopilot.tool_repository.manager.action_node.ActionNode(name, description, node_type)[source]¶
Bases:
objectRepresents an action node in a workflow or execution graph, encapsulating details like the action’s name, description, return value, relevant code snippets, next actions, execution status, and action type.
- _name¶
The name of the action.
- Type:
str
- _description¶
A brief description of what the action does.
- Type:
str
- _return_val¶
The value returned by the action upon execution.
- Type:
str
- _relevant_code¶
A dictionary mapping relevant code snippets or references associated with the action.
- Type:
dict
- _next_action¶
A dictionary mapping subsequent actions that depend on the current action.
- Type:
dict
- _status¶
The execution status of the action, indicating whether it has been successfully executed.
- Type:
bool
- _type¶
The type of the action, categorizing its purpose or method of execution.
- Type:
str
- property description¶
Returns the description of the action.
- Returns:
The action’s description.
- Return type:
str
- property name¶
Returns the name of the action.
- Returns:
The action’s name.
- Return type:
str
- property next_action¶
Returns subsequent actions that depend on the current action.
- Returns:
A mapping of subsequent actions.
- Return type:
dict
- property node_type¶
Returns the type of the action.
- Returns:
The action’s type.
- Return type:
str
- property relevant_action¶
Returns the relevant code snippets or references associated with the action.
- Returns:
The action’s relevant code snippets or references.
- Return type:
dict
- property return_val¶
Returns the return value of the action.
- Returns:
The value returned by the action upon execution.
- Return type:
str
- property status¶
Returns the execution status of the action.
- Returns:
True if the action has been executed successfully, False otherwise.
- Return type:
bool
- oscopilot.tool_repository.manager.tool_manager.print_error_and_exit(message)[source]¶
Prints an error message to standard output and exits the program with a status code of 1.
This function is typically used to handle critical errors from which the program cannot recover. It ensures that the error message is visible to the user before the program terminates.
- Parameters:
message (str) – The error message to be printed.
- oscopilot.tool_repository.manager.tool_manager.add_tool(toolManager, tool_name, tool_path)[source]¶
Adds a new tool to the tool manager with the given name and code loaded from the specified path.
This function reads the tool’s code from a file, extracts a description from the code using a predefined pattern, and then adds the tool to the tool manager using the extracted information. If the tool’s description is not found within the code, the function will print an error message and exit.
- Parameters:
toolManager (ToolManager) – The instance of ToolManager to which the tool will be added.
tool_name (str) – The name of the tool to be added.
tool_path (str) – The file system path to the source code of the tool.
Note
The function expects the tool’s code to contain a description defined as a string literal assigned to self._description within the code. The description must be enclosed in double quotes for it to be successfully extracted.
- oscopilot.tool_repository.manager.tool_manager.delete_tool(toolManager, tool_name)[source]¶
Deletes a tool from the tool manager and prints a success message.
This function calls the delete_tool method of the given ToolManager instance to remove the specified tool. Upon successful deletion, it prints a message indicating the operation was successful.
- Parameters:
toolManager (ToolManager) – An instance of the ToolManager class.
tool_name (str) – The name of the tool to be deleted.
- oscopilot.tool_repository.manager.tool_manager.get_open_api_doc_path()[source]¶
Determines the file system path to the ‘openapi.json’ file located in the same directory as this script.
- Returns:
The absolute path to the ‘openapi.json’ file.
- Return type:
str
- oscopilot.tool_repository.manager.tool_manager.get_open_api_description_pair()[source]¶
Extracts and returns a mapping of OpenAPI path names to their descriptions.
This function loads the OpenAPI specification from a ‘openapi.json’ file located in the same directory as this script. It then iterates over the paths defined in the OpenAPI specification, creating a dictionary that maps each path name to its description (summary). If a path supports both ‘get’ and ‘post’ operations, the description for the ‘post’ operation is preferred.
- Returns:
A dictionary mapping OpenAPI path names to their summary descriptions.
- Return type:
dict
- oscopilot.tool_repository.manager.tool_manager.main()[source]¶
The main entry point for managing generated tools for the FRIDAY project.
This function sets up a command-line interface for adding or deleting tools within the FRIDAY project. It supports flags for adding a new tool, deleting an existing tool, and specifies the name and path of the tool for the respective operations. Based on the arguments provided, it initializes a ToolManager instance and performs the requested add or delete operation.
The ‘–add’ flag requires the ‘–tool_name’ and ‘–tool_path’ arguments to specify the name and the path of the tool to be added. The ‘–delete’ flag requires only the ‘–tool_name’ argument.
- Usage:
python script.py –add –tool_name <name> –tool_path <path> python script.py –delete –tool_name <name>
- Raises:
SystemExit – If no operation type is specified or required arguments are missing, the program will print an error message and exit with a status code of 1.