Action chaining and history for agents
[](https://github.com/AutonomousResearchGroup/agentaction/actions/workflows/test.yml)
[](https://badge.fury.io/py/agentaction)
# Why 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.
# Installation
```bash
pip install agentaction
```
# Quickstart
Create a directory for your action modules:
```bash
mkdir actions
```
In this directory, you can create Python files (`.py`) that define your actions. Each file should define a `get_actions` function that returns a list of action dictionaries. Here is a sample action file `sample_action.py`:
```python
def sample_function(args):
# Your function logic here
return "Hello, " + args["name"]
def get_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:
```python
from actions_manager import import_actions, use_action
# Import the actions
import_actions("./actions")
# Use an action
result = use_action("sample_function", {"name": "John"})
actions = search_actions("hello")
print(result) # Should print: {"success": True, "output": "Hello, John"}
```
You can use the `get_available_actions` and `get_action` functions to search for and retrieve actions, respectively. And, don't forget to use the `add_to_action_history` function to keep track of which actions your agent has performed.
## Usage Guide
### Action Creation and Addition
```python
from actions_manager import add_action
action = {
"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 Execution
```python
from actions_manager import use_action
result = use_action("your_function_name", {"arg1": "value1", "arg2": "value2"})
```
### Search for Relevant Actions
```python
from actions_manager import get_available_actions
actions = get_available_actions("query_text")
```
## API Documentation
### `compose_action_prompt(action: dict, values: dict) -> str`
Generates a prompt for a given action based on provided values.
### `get_actions() -> dict`
Retrieves all the actions present in the global `actions` dictionary.
### `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) -> list`
Retrieves the most recent executed actions.
### `get_last_action() -> str or None`
Retrieves the last executed action from the action history.
### `get_available_actions(search_text: str) -> list`
Retrieves the available actions based on relevance and last action.
### `get_formatted_actions(search_text: str) -> list`
Retrieve a dict containing the available actions in several formats
### `get_action_from_memory(action_name) -> dict or None`
Retrieve an action from memory based on the action's name.
### `search_actions(search_text: str, n_results: int=5) -> list`
Searches for actions based on a query text.
### `use_action(function_name: str, arguments: dict) -> dict`
Executes 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 None`
Retrieves a specific action by its name from the 'actions' dictionary.
### `remove_action(name: str) -> bool`
Removes 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 Welcome
If 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.
Raw data
{
"_id": null,
"home_page": "https://github.com/AutonomousResearchGroup/agentaction",
"name": "agentaction",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "",
"author": "Moon",
"author_email": "shawmakesmagic@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/17/fb/a3dc77bed76c33d1bb8bf5430803629fd2d35d68374f915e07d643b9dc83/agentaction-0.1.7.tar.gz",
"platform": null,
"description": "\nAction chaining and history for agents\n\n\n[](https://github.com/AutonomousResearchGroup/agentaction/actions/workflows/test.yml)\n[](https://badge.fury.io/py/agentaction)\n\n# Why Use This?\nThis 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.\n\nThis package facilitates action creation, retrieval, and management, all while supporting vector search powered by chromadb to efficiently locate relevant actions.\n\n# Installation\n\n```bash\npip install agentaction\n```\n\n# Quickstart\n\nCreate a directory for your action modules:\n\n```bash\nmkdir actions\n```\n\nIn this directory, you can create Python files (`.py`) that define your actions. Each file should define a `get_actions` function that returns a list of action dictionaries. Here is a sample action file `sample_action.py`:\n\n```python\ndef sample_function(args):\n # Your function logic here\n return \"Hello, \" + args[\"name\"]\n\ndef get_actions():\n return [\n {\n \"prompt\": \"Say hello\",\n \"builder\": None,\n \"handler\": sample_function,\n \"suggestion_after_actions\": [],\n \"never_after_actions\": [],\n \"function\": {\n \"name\": \"sample_function\",\n \"description\": \"Says hello to a person\",\n \"args\": [\"name\"]\n }\n }\n ]\n```\n\nNow you can use the action manager in your agent. Here's a simple example:\n\n```python\nfrom actions_manager import import_actions, use_action\n\n# Import the actions\nimport_actions(\"./actions\")\n\n# Use an action\nresult = use_action(\"sample_function\", {\"name\": \"John\"})\nactions = search_actions(\"hello\")\nprint(result) # Should print: {\"success\": True, \"output\": \"Hello, John\"}\n```\n\nYou can use the `get_available_actions` and `get_action` functions to search for and retrieve actions, respectively. And, don't forget to use the `add_to_action_history` function to keep track of which actions your agent has performed.\n\n## Usage Guide\n\n### Action Creation and Addition\n```python\nfrom actions_manager import add_action\n\naction = {\n \"prompt\": \"Action Prompt\",\n \"builder\": None, # the function that is called to build the action prompt\n \"handler\": your_function_name, # the function that is called when the action is executed\n \"suggestion_after_actions\": [\"other_action_name1\", \"other_action_name2\"],\n \"never_after_actions\": [\"action_name3\", \"action_name4\"],\n \"function\": {\n \"name\": \"your_function_name\",\n \"description\": \"Your function description\",\n \"args\": [\"arg1\", \"arg2\"]\n }\n}\n\nadd_action(\"your_function_name\", action)\n```\n\n### Action Execution\n```python\nfrom actions_manager import use_action\n\nresult = use_action(\"your_function_name\", {\"arg1\": \"value1\", \"arg2\": \"value2\"})\n```\n\n### Search for Relevant Actions\n```python\nfrom actions_manager import get_available_actions\n\nactions = get_available_actions(\"query_text\")\n```\n\n## API Documentation\n\n### `compose_action_prompt(action: dict, values: dict) -> str`\nGenerates a prompt for a given action based on provided values.\n\n### `get_actions() -> dict`\nRetrieves all the actions present in the global `actions` dictionary.\n\n### `add_to_action_history(action_name: str, action_arguments: dict={}, success: bool=True)`\nAdds an executed action to the action history.\n\n### `get_action_history(n_results: int=20) -> list`\nRetrieves the most recent executed actions.\n\n### `get_last_action() -> str or None`\nRetrieves the last executed action from the action history.\n\n### `get_available_actions(search_text: str) -> list`\nRetrieves the available actions based on relevance and last action.\n\n### `get_formatted_actions(search_text: str) -> list`\nRetrieve a dict containing the available actions in several formats\n\n### `get_action_from_memory(action_name) -> dict or None`\nRetrieve an action from memory based on the action's name.\n### `search_actions(search_text: str, n_results: int=5) -> list`\nSearches for actions based on a query text.\n\n### `use_action(function_name: str, arguments: dict) -> dict`\nExecutes a specific action by its function name.\n\n### `add_action(name: str, action: dict)`\nAdds an action to the actions dictionary and 'actions' collection in memory.\n\n### `get_action(name: str) -> dict or None`\nRetrieves a specific action by its name from the 'actions' dictionary.\n\n### `remove_action(name: str) -> bool`\nRemoves a specific action by name.\n\n### `import_actions(actions_dir: str)`\nImports all the actions present in the 'actions_dir' directory. The actions returned are then added to the 'actions' dictionary.\n\n### `clear_actions()`\nWipes the 'actions' collection in memory and resets the 'actions' dictionary.\n\n\n# Contributions Welcome\n\nIf 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.\n\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Action chaining and history for agents",
"version": "0.1.7",
"project_urls": {
"Homepage": "https://github.com/AutonomousResearchGroup/agentaction"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "f953c7ee5ccecb42aaf1bc615b8bcf2a5dab2aa06c337b1cc2f5092a245390ed",
"md5": "92c76ac426ef48dc3caec0fce48612bf",
"sha256": "2a96b8e843a86db30f91c1ba0e9e88d74bb100d716dde0d962b052c29637152b"
},
"downloads": -1,
"filename": "agentaction-0.1.7-py3-none-any.whl",
"has_sig": false,
"md5_digest": "92c76ac426ef48dc3caec0fce48612bf",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 6990,
"upload_time": "2023-08-02T01:00:07",
"upload_time_iso_8601": "2023-08-02T01:00:07.338675Z",
"url": "https://files.pythonhosted.org/packages/f9/53/c7ee5ccecb42aaf1bc615b8bcf2a5dab2aa06c337b1cc2f5092a245390ed/agentaction-0.1.7-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "17fba3dc77bed76c33d1bb8bf5430803629fd2d35d68374f915e07d643b9dc83",
"md5": "8834ce5afec2aac54f82844872937b72",
"sha256": "72bf750a615b6d7cc6a828a7cd200da5e504606f7b4ce6f7a357d3f862a31d14"
},
"downloads": -1,
"filename": "agentaction-0.1.7.tar.gz",
"has_sig": false,
"md5_digest": "8834ce5afec2aac54f82844872937b72",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 6584,
"upload_time": "2023-08-02T01:00:08",
"upload_time_iso_8601": "2023-08-02T01:00:08.957410Z",
"url": "https://files.pythonhosted.org/packages/17/fb/a3dc77bed76c33d1bb8bf5430803629fd2d35d68374f915e07d643b9dc83/agentaction-0.1.7.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-08-02 01:00:08",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "AutonomousResearchGroup",
"github_project": "agentaction",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"requirements": [
{
"name": "agentmemory",
"specs": []
}
],
"lcname": "agentaction"
}