{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Route based on previous annotator\n",
"\n",
"This example demonstrates how to set up an agent to automatically route a task depending on which annotator it was annotated by. This allows for more complicated flows where you track the performance of an annotator and can conditionally route based on that.\n",
"\n",
"### Example Workflow\n",
"\n",
"This workflow allows for conditional routing of tasks, note the Agent stage follows the Annotate stage and has multiple pathways. To use this example effectively, ensure your workflow matches or closely resembles the one shown. Without the previous stage, we will have no such field and without pathways, we have no option for dynamic routing\n",
"\n",
"[📖 Here](https://docs.encord.com/platform-documentation/Annotate/annotate-projects/annotate-workflows-and-templates#creating-workflows) is the documentation for creating a workflow with Encord.\n",
"\n",
"The code in this notebook is for the **annotator name routing** agent."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Installation\n",
"\n",
"Ensure that you have the `encord-agents` library installed."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"!python -m pip install encord-agents"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Encord Authentication\n",
"\n",
"Encord uses ssh-keys for authentication. The following is a code cell for setting the `ENCORD_SSH_KEY` environment variable. It contains the raw content of your private ssh key file.\n",
"\n",
"If you have not setup an ssh key, see our [documentation](https://agents-docs.encord.com/authentication/).\n",
"\n",
"> 💡 In colab, you can set the key once in the secrets in the left sidebar and load it in new notebooks. IF YOU ARE NOT RUNNING THE CODE IN THE COLLAB NOTEBOOK, you must set the environment variable directly.\n",
"> ```python\n",
"> os.environ[\"ENCORD_SSH_KEY\"] = \"\"\"paste-private-key-here\"\"\"\n",
"> ```"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"\n",
"os.environ[\"ENCORD_SSH_KEY\"] = \"private_key_file_content\"\n",
"# or you can set a path to a file\n",
"# os.environ[\"ENCORD_SSH_KEY_FILE\"] = \"/path/to/your/private/key\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### [Alternative] Temporary Key\n",
"There's also the option of generating a temporary (fresh) ssh key pair via the code cell below.\n",
"Please follow the instructions printed when executing the code."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# ⚠️ Safe to skip if you have authenticated already\n",
"import os\n",
"\n",
"from encord_agents.utils.colab import generate_public_private_key_pair_with_instructions\n",
"\n",
"private_key_path, public_key_path = generate_public_private_key_pair_with_instructions()\n",
"os.environ[\"ENCORD_SSH_KEY_FILE\"] = private_key_path.as_posix()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Define the Agent\n",
"\n",
"In the following code cell, we define the custom code that routes based on prior annotator name.\n",
"\n",
"Ensure that you add:\n",
"- ``: You obtain it from the Project page in the Encord platform.\n",
"- ``: Can be found from the workflow graph or the agent name can be used\n",
"- ``: It will be listed when you expand the agent node of the Workflow on the Project page. (You can also use the pathway name that you gave it)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from encord.workflow.stages.agent import AgentTask\n",
"\n",
"from encord_agents.tasks import Runner\n",
"\n",
"runner = Runner(project_hash=\"\")\n",
"\n",
"\n",
"@runner.stage(\"\")\n",
"def by_file_name(task: AgentTask) -> str:\n",
" last_annotated_by = task.last_annotated_by\n",
" if \"expert\" in last_annotated_by:\n",
" return \"Expert\"\n",
" else:\n",
" return \"Novice\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- **Task Runner:** The code initializes a runner to process tasks.\n",
"- **Task Routing:** It defines a stage implementation that:\n",
" - Extracts the last annotated by\n",
" - Identifies whether expert is in the email i.e: john+expert@acme.org\n",
" - Routes the task onto further review dependant whether it's an expert"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Running the Agent\n",
"\n",
"The `runner` object is callable which means that you can just call it to route your tasks."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Run the agent\n",
"runner()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Outcome\n",
"\n",
"Your agent now routes tasks based on the previous annotator and routes them appropriately through the Workflow.\n",
"\n",
"> 💡 To run this as a command-line interface, save the code in an `agents.py` file and replace: \n",
"> ```python\n",
"> runner()\n",
"> ``` \n",
"> with: \n",
"> ```python\n",
"> if __name__ == \"__main__\":\n",
"> runner.run()\n",
"> ``` \n",
"> This lets you set parameters like the project hash from the command line: \n",
"> ```bash\n",
"> python agent.py --project-hash \"...\"\n",
"> ```\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"colab": {
"provenance": [],
"toc_visible": true
},
"kernelspec": {
"display_name": "encord-agents-Cw_LL1Rx-py3.11",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3"
}
},
"nbformat": 4,
"nbformat_minor": 0
}