Package: agentaction
Action chaining and history for agentsWhy Use This?This package helps manage and simplify the task of handling actions for an agent, especially a looping agent with chained functions. Actions can be anything, but the intended purpose is to work with openai function calling or other JSON/function calling LLM completion paradigms.This package facilitates action creation, retrieval, and management, all while supporting vector search powered by chromadb to efficiently locate relevant actions.InstallationpipinstallagentactionQuickstartCreate a directory for your action modules:mkdiractionsIn this directory, you can create Python files (.py) that define your actions. Each file should define aget_actionsfunction that returns a list of action dictionaries. Here is a sample action filesample_action.py:defsample_function(args):# Your function logic herereturn"Hello, "+args["name"]defget_actions():return[{"prompt":"Say hello","builder":None,"handler":sample_function,"suggestion_after_actions":[],"never_after_actions":[],"function":{"name":"sample_function","description":"Says hello to a person","args":["name"]}}]Now you can use the action manager in your agent. Here's a simple example:fromactions_managerimportimport_actions,use_action# Import the actionsimport_actions("./actions")# Use an actionresult=use_action("sample_function",{"name":"John"})actions=search_actions("hello")print(result)# Should print: {"success": True, "output": "Hello, John"}You can use theget_available_actionsandget_actionfunctions to search for and retrieve actions, respectively. And, don't forget to use theadd_to_action_historyfunction to keep track of which actions your agent has performed.Usage GuideAction Creation and Additionfromactions_managerimportadd_actionaction={"prompt":"Action Prompt","builder":None,# the function that is called to build the action prompt"handler":your_function_name,# the function that is called when the action is executed"suggestion_after_actions":["other_action_name1","other_action_name2"],"never_after_actions":["action_name3","action_name4"],"function":{"name":"your_function_name","description":"Your function description","args":["arg1","arg2"]}}add_action("your_function_name",action)Action Executionfromactions_managerimportuse_actionresult=use_action("your_function_name",{"arg1":"value1","arg2":"value2"})Search for Relevant Actionsfromactions_managerimportget_available_actionsactions=get_available_actions("query_text")API Documentationcompose_action_prompt(action: dict, values: dict) -> strGenerates a prompt for a given action based on provided values.get_actions() -> dictRetrieves all the actions present in the globalactionsdictionary.add_to_action_history(action_name: str, action_arguments: dict={}, success: bool=True)Adds an executed action to the action history.get_action_history(n_results: int=20) -> listRetrieves the most recent executed actions.get_last_action() -> str or NoneRetrieves the last executed action from the action history.get_available_actions(search_text: str) -> listRetrieves the available actions based on relevance and last action.get_formatted_actions(search_text: str) -> listRetrieve a dict containing the available actions in several formatsget_action_from_memory(action_name) -> dict or NoneRetrieve an action from memory based on the action's name.search_actions(search_text: str, n_results: int=5) -> listSearches for actions based on a query text.use_action(function_name: str, arguments: dict) -> dictExecutes a specific action by its function name.add_action(name: str, action: dict)Adds an action to the actions dictionary and 'actions' collection in memory.get_action(name: str) -> dict or NoneRetrieves a specific action by its name from the 'actions' dictionary.remove_action(name: str) -> boolRemoves a specific action by name.import_actions(actions_dir: str)Imports all the actions present in the 'actions_dir' directory. The actions returned are then added to the 'actions' dictionary.clear_actions()Wipes the 'actions' collection in memory and resets the 'actions' dictionary.Contributions WelcomeIf you like this library and want to contribute in any way, please feel free to submit a PR and I will review it. Please note that the goal here is simplicity and accesibility, using common language and few dependencies.