💡 All-in-one AI framework for semantic search, LLM orchestration and language model workflows
Python agentsaiai-agentsembeddingsinformation-retrievallanguage-modellarge-language-modelsllmnlppythonragretrieval-augmented-generationsearchsearch-enginesemantic-searchsentence-embeddingstransformerstxtaivector-databasevector-search
Add entity extraction pipeline notebook and documentation #203
davidmezzetti8ba533388b6b
diff --git a/docs/examples.md b/docs/examples.mdindex 138a0a6f..7bd2285a 100644--- a/docs/examples.md+++ b/docs/examples.md@@ -33,6 +33,7 @@ Transform data with NLP-backed pipelines.| [Extract text from documents](https://github.com/neuml/txtai/blob/master/examples/10_Extract_text_from_documents.ipynb) | Extract text from PDF, Office, HTML and more | [](https://colab.research.google.com/github/neuml/txtai/blob/master/examples/10_Extract_text_from_documents.ipynb) || [Transcribe audio to text](https://github.com/neuml/txtai/blob/master/examples/11_Transcribe_audio_to_text.ipynb) | Convert audio files to text | [](https://colab.research.google.com/github/neuml/txtai/blob/master/examples/11_Transcribe_audio_to_text.ipynb) || [Translate text between languages](https://github.com/neuml/txtai/blob/master/examples/12_Translate_text_between_languages.ipynb) | Streamline machine translation and language detection | [](https://colab.research.google.com/github/neuml/txtai/blob/master/examples/12_Translate_text_between_languages.ipynb) |+| [Generate image captions and detect objects](https://github.com/neuml/txtai/blob/master/examples/25_Generate_image_captions_and_detect_objects.ipynb) | Captions and object detection for images | [](https://colab.research.google.com/github/neuml/txtai/blob/master/examples/25_Generate_image_captions_and_detect_objects.ipynb) || [API Gallery](https://github.com/neuml/txtai/blob/master/examples/08_API_Gallery.ipynb) | Using txtai in JavaScript, Java, Rust and Go | [](https://colab.research.google.com/github/neuml/txtai/blob/master/examples/08_API_Gallery.ipynb) |## Workflows@@ -44,6 +45,7 @@ Efficiently process data at scale.| [Run pipeline workflows](https://github.com/neuml/txtai/blob/master/examples/14_Run_pipeline_workflows.ipynb) | Simple yet powerful constructs to efficiently process data | [](https://colab.research.google.com/github/neuml/txtai/blob/master/examples/14_Run_pipeline_workflows.ipynb) || [Transform tabular data with composable workflows](https://github.com/neuml/txtai/blob/master/examples/22_Transform_tabular_data_with_composable_workflows.ipynb) | Transform, index and search tabular data | [](https://colab.research.google.com/github/neuml/txtai/blob/master/examples/22_Transform_tabular_data_with_composable_workflows.ipynb) || [Tensor workflows](https://github.com/neuml/txtai/blob/master/examples/23_Tensor_workflows.ipynb) | Performant processing of large tensor arrays | [](https://colab.research.google.com/github/neuml/txtai/blob/master/examples/23_Tensor_workflows.ipynb) |+| [Entity extraction workflows](https://github.com/neuml/txtai/blob/master/examples/26_Entity_extraction_workflows.ipynb) | Identify entity/label combinations | [](https://colab.research.google.com/github/neuml/txtai/blob/master/examples/26_Entity_extraction_workflows.ipynb) |## Model Trainingdiff --git a/docs/pipeline/image/caption.md b/docs/pipeline/image/caption.mdindex ac228a1e..0eacec3a 100644--- a/docs/pipeline/image/caption.md+++ b/docs/pipeline/image/caption.md@@ -17,6 +17,12 @@ caption = Caption()caption("path to image file")```+See the link below for a more detailed example.++| Notebook | Description | |+|:----------|:-------------|------:|+| [Generate image captions and detect objects](https://github.com/neuml/txtai/blob/master/examples/25_Generate_image_captions_and_detect_objects.ipynb) | Captions and object detection for images | [](https://colab.research.google.com/github/neuml/txtai/blob/master/examples/25_Generate_image_captions_and_detect_objects.ipynb) |+## Configuration-driven examplePipelines are run with Python or configuration. Pipelines can be instantiated in [configuration](../../../api/configuration/#pipeline) using the lower case name of the pipeline. Configuration-driven pipelines are run with [workflows](../../../workflow/#configuration-driven-example) or the [API](../../../api).diff --git a/docs/pipeline/image/objects.md b/docs/pipeline/image/objects.mdindex 8fcbe0b1..77b39b59 100644--- a/docs/pipeline/image/objects.md+++ b/docs/pipeline/image/objects.md@@ -17,6 +17,12 @@ objects = Objects()objects("path to image file")```+See the link below for a more detailed example.++| Notebook | Description | |+|:----------|:-------------|------:|+| [Generate image captions and detect objects](https://github.com/neuml/txtai/blob/master/examples/25_Generate_image_captions_and_detect_objects.ipynb) | Captions and object detection for images | [](https://colab.research.google.com/github/neuml/txtai/blob/master/examples/25_Generate_image_captions_and_detect_objects.ipynb) |+## Configuration-driven examplePipelines are run with Python or configuration. Pipelines can be instantiated in [configuration](../../../api/configuration/#pipeline) using the lower case name of the pipeline. Configuration-driven pipelines are run with [workflows](../../../workflow/#configuration-driven-example) or the [API](../../../api).diff --git a/docs/pipeline/index.md b/docs/pipeline/index.mdindex 8ea38c06..2afec18f 100644--- a/docs/pipeline/index.md+++ b/docs/pipeline/index.md@@ -21,6 +21,7 @@ The following is a list of the default pipelines available in txtai.- [Caption](image/caption)- [Objects](image/objects)- Text+ - [Entity](text/entity)- [Extractive QA](text/extractor)- [Labeling](text/labels)- [Similarity](text/similarity)diff --git a/docs/pipeline/text/entity.md b/docs/pipeline/text/entity.mdnew file mode 100644index 00000000..5b1ecd95--- /dev/null+++ b/docs/pipeline/text/entity.md@@ -0,0 +1,69 @@+# Entity+++++The entity extraction pipeline applies a token classifier to text and extracts known entity/label combinations.++## Example++The following shows a simple example using this pipeline.++```python+from txtai.pipeline import Entity++# Create and run pipeline+entity = Entity()+entity("Canada's last fully intact ice shelf has suddenly collapsed, " \+ "forming a Manhattan-sized iceberg")+```++See the link below for a more detailed example.++| Notebook | Description | |+|:----------|:-------------|------:|+| [Entity extraction workflows](https://github.com/neuml/txtai/blob/master/examples/26_Entity_extraction_workflows.ipynb) | Identify entity/label combinations | [](https://colab.research.google.com/github/neuml/txtai/blob/master/examples/26_Entity_extraction_workflows.ipynb) |++## Configuration-driven example++Pipelines are run with Python or configuration. Pipelines can be instantiated in [configuration](../../../api/configuration/#pipeline) using the lower case name of the pipeline. Configuration-driven pipelines are run with [workflows](../../../workflow/#configuration-driven-example) or the [API](../../../api).++### config.yml+```yaml+# Create pipeline using lower case class name+entity:++# Run pipeline with workflow+workflow:+ entity:+ tasks:+ - action: entity+```++### Run with Workflows++```python+from txtai.api import API++# Create and run pipeline with workflow+app = API("config.yml")+list(app.workflow("entity", ["Canada's last fully intact ice shelf has suddenly collapsed, forming a Manhattan-sized iceberg"]))+```++### Run with API++```bash+CONFIG=config.yml uvicorn "txtai.api:app" &++curl \+ -X POST "http://localhost:8000/workflow" \+ -H "Content-Type: application/json" \+ -d '{"name":"entity", "elements": ["Canadas last fully intact ice shelf has suddenly collapsed, forming a Manhattan-sized iceberg"]}'+```++## Methods++Python documentation for the pipeline.++### ::: txtai.pipeline.Entity.__init__+### ::: txtai.pipeline.Entity.__call__diff --git a/examples/26_Entity_extraction_workflows.ipynb b/examples/26_Entity_extraction_workflows.ipynbnew file mode 100644index 00000000..15fbbed9--- /dev/null+++ b/examples/26_Entity_extraction_workflows.ipynb@@ -0,0 +1,272 @@+{+ "nbformat": 4,+ "nbformat_minor": 0,+ "metadata": {+ "kernelspec": {+ "name": "python3",+ "display_name": "Python 3",+ "language": "python"+ },+ "language_info": {+ "name": "python",+ "version": "3.7.6",+ "mimetype": "text/x-python",+ "codemirror_mode": {+ "name": "ipython",+ "version": 3+ },+ "pygments_lexer": "ipython3",+ "nbconvert_exporter": "python",+ "file_extension": ".py"+ },+ "colab": {+ "name": "26 - Entity extraction workflows",+ "provenance": [],+ "collapsed_sections": []+ }+ },+ "cells": [+ {+ "cell_type": "markdown",+ "metadata": {+ "id": "POWZoSJR6XzK"+ },+ "source": [+ "# Entity extraction workflows\n",+ "\n",+ "_This notebook is part of a tutorial series on [txtai](https://github.com/neuml/txtai), an AI-powered semantic search platform._\n",+ "\n",+ "Entity extraction is the process of identifying names, locations, organizations and other entity-like tokens in unstructured text. Entity extraction can organize data into topics and/or feed downstream machine learning pipelines.\n",+ "\n",+ "This notebook will show how to use the entity extraction pipeline in txtai with workflows."+ ]+ },+ {+ "cell_type": "markdown",+ "metadata": {+ "id": "qa_PPKVX6XzN"+ },+ "source": [+ "# Install dependencies\n",+ "\n",+ "Install `txtai` and all dependencies."+ ]+ },+ {+ "cell_type": "code",+ "metadata": {+ "_uuid": "8f2839f25d086af736a60e9eeb907d3b93b6e0e5",+ "_cell_guid": "b1076dfc-b9ad-4769-8c92-a6c4dae69d19",+ "trusted": true,+ "_kg_hide-output": true,+ "id": "24q-1n5i6XzQ"+ },+ "source": [+ "%%capture\n",+ "!pip install git+https://github.com/neuml/txtai#egg=txtai[api]"+ ],+ "execution_count": null,+ "outputs": []+ },+ {+ "cell_type": "markdown",+ "source": [+ "# Extract entities\n",+ "\n",+ "Let's get right to it! The following example creates an entity pipeline and extracts entities from text. \n"+ ],+ "metadata": {+ "id": "0p3WCDniUths"+ }+ },+ {+ "cell_type": "code",+ "metadata": {+ "_uuid": "d629ff2d2480ee46fbb7e2d37f6b5fab8052498a",+ "_cell_guid": "79c7e3d0-c299-4dcb-8224-4455121ee9b0",+ "trusted": true,+ "id": "2j_CFGDR6Xzp",+ "colab": {+ "base_uri": "https://localhost:8080/"+ },+ "outputId": "6276538e-5b06-4038-ae0f-0aa642e86814"+ },+ "source": [+ "from txtai.pipeline import Entity\n",+ "\n",+ "data = [\"US tops 5 million confirmed virus cases\",\n",+ " \"Canada's last fully intact ice shelf has suddenly collapsed, forming a Manhattan-sized iceberg\",\n",+ " \"Beijing mobilises invasion craft along coast as Taiwan tensions escalate\",\n",+ " \"The National Park Service warns against sacrificing slower friends in a bear attack\",\n",+ " \"Maine man wins $1M from $25 lottery ticket\",\n",+ " \"Make huge profits without work, earn up to $100,000 a day\"]\n",+ "\n",+ "entity = Entity()\n",+ "\n",+ "for x, e in enumerate(entity(data)):\n",+ " print(data[x])\n",+ " print(f\" {e}\", \"\\n\")"+ ],+ "execution_count": null,+ "outputs": [+ {+ "output_type": "stream",+ "name": "stdout",+ "text": [+ "US tops 5 million confirmed virus cases\n",+ " [('US', 'LOC', 0.999273955821991)] \n",+ "\n",+ "Canada's last fully intact ice shelf has suddenly collapsed, forming a Manhattan-sized iceberg\n",+ " [('Canada', 'LOC', 0.999609649181366), ('Manhattan', 'MISC', 0.651396632194519)] \n",+ "\n",+ "Beijing mobilises invasion craft along coast as Taiwan tensions escalate\n",+ " [('Beijing', 'LOC', 0.9996659755706787), ('Taiwan', 'LOC', 0.9996755123138428)] \n",+ "\n",+ "The National Park Service warns against sacrificing slower friends in a bear attack\n",+ " [('National Park Service', 'ORG', 0.9993489384651184)] \n",+ "\n",+ "Maine man wins $1M from $25 lottery ticket\n",+ " [('Maine', 'LOC', 0.9987521171569824)] \n",+ "\n",+ "Make huge profits without work, earn up to $100,000 a day\n",+ " [] \n",+ "\n"+ ]+ }+ ]+ },+ {+ "cell_type": "markdown",+ "source": [+ "The section above is running an entity extraction pipeline for each row in data. The outputs are the token(s) identified as part of an entity, the type of entity and score or confidence in the prediction."+ ],+ "metadata": {+ "id": "bVd_qHh97IlJ"+ }+ },+ {+ "cell_type": "markdown",+ "source": [+ "# Feed entities to a workflow\n",+ "\n",+ "The next section demonstrates how the entity extraction pipeline can be used as part of a workflow. This workflow uses the output entities and builds an embeddings index for each row. This effectively computes entity embeddings to compare the row similarity with a focus on mentioned entities."+ ],+ "metadata": {+ "id": "fY71Dyyt8Arv"+ }+ },+ {+ "cell_type": "code",+ "source": [+ "from txtai.embeddings import Embeddings, Documents\n",+ "from txtai.workflow import Workflow, Task\n",+ "\n",+ "# Create workflow with an entity pipeline output into a documents collection\n",+ "documents = Documents()\n",+ "workflow = Workflow([Task(lambda x: entity(x, flatten=True, join=True)), Task(documents.add, unpack=False)])\n",+ "\n",+ "# Run workflow\n",+ "for _ in workflow([(x, row, None) for x, row in enumerate(data)]):\n",+ " pass\n",+ "\n",+ "embeddings = Embeddings({\"path\": \"sentence-transformers/nli-mpnet-base-v2\"})\n",+ "embeddings.index(documents)\n",+ "\n",+ "for query in [\"North America\", \"Asia Pacific\"]:\n",+ " index = embeddings.search(query, 1)[0][0]\n",+ " print(query, \"\\t\", data[index])"+ ],+ "metadata": {+ "colab": {+ "base_uri": "https://localhost:8080/"+ },+ "id": "yB3e6HVM8dRJ",+ "outputId": "8424ce5d-9652-421d-d0bd-261b911ea7d6"+ },+ "execution_count": null,+ "outputs": [+ {+ "output_type": "stream",+ "name": "stdout",+ "text": [+ "North America \t Canada's last fully intact ice shelf has suddenly collapsed, forming a Manhattan-sized iceberg\n",+ "Asia Pacific \t Beijing mobilises invasion craft along coast as Taiwan tensions escalate\n"+ ]+ }+ ]+ },+ {+ "cell_type": "markdown",+ "source": [+ "# Run workflow YAML\n",+ "\n",+ "Below is the same example using workflow YAML."+ ],+ "metadata": {+ "id": "u_0WiV1NvbCK"+ }+ },+ {+ "cell_type": "code",+ "source": [+ "workflow = \"\"\"\n",+ "writable: true\n",+ "embeddings:\n",+ " path: sentence-transformers/nli-mpnet-base-v2\n",+ "\n",+ "entity:\n",+ "\n",+ "workflow:\n",+ " index:\n",+ " tasks:\n",+ " - action: entity\n",+ " args: [null, \"simple\", true, true]\n",+ " - action: index\n",+ "\"\"\"\n",+ "\n",+ "from txtai.api import API\n",+ "\n",+ "# Create and run workflow\n",+ "app = API(workflow)\n",+ "for _ in app.workflow(\"index\", [(x, row, None) for x, row in enumerate(data)]):\n",+ " pass\n",+ "\n",+ "# Run queries\n",+ "for query in [\"North America\", \"Asia Pacific\"]:\n",+ " index = app.search(query)[0][\"id\"]\n",+ " print(query, \"\\t\", data[index])"+ ],+ "metadata": {+ "colab": {+ "base_uri": "https://localhost:8080/"+ },+ "id": "3qmRdU7LvhQ3",+ "outputId": "e01453fc-5bbf-41a6-e557-64fa453f80ba"+ },+ "execution_count": null,+ "outputs": [+ {+ "output_type": "stream",+ "name": "stdout",+ "text": [+ "North America \t Canada's last fully intact ice shelf has suddenly collapsed, forming a Manhattan-sized iceberg\n",+ "Asia Pacific \t Beijing mobilises invasion craft along coast as Taiwan tensions escalate\n"+ ]+ }+ ]+ },+ {+ "cell_type": "markdown",+ "source": [+ "# Wrapping up\n",+ "\n",+ "This notebook introduced entity extraction pipelines with txtai. This pipeline supports a number of different configurations to help feed downstream systems and/or directly use the entities.\n",+ "\n",+ "As with other pipelines, the entity extraction pipeline can be used standalone in Python, as an API service or as part of a workflow!"+ ],+ "metadata": {+ "id": "i3kkN5Vpx8ks"+ }+ }+ ]+}diff --git a/mkdocs.yml b/mkdocs.ymlindex 72a596bc..cbbe6852 100644--- a/mkdocs.yml+++ b/mkdocs.yml@@ -66,6 +66,7 @@ nav:- Caption: pipeline/image/caption.md- Objects: pipeline/image/objects.md- Text:+ - Entity: pipeline/text/entity.md- Extractor: pipeline/text/extractor.md- Labels: pipeline/text/labels.md- Similarity: pipeline/text/similarity.md