Skip to content

armgit / txtai

Public Imported from GitHub
0

💡 All-in-one AI framework for semantic search, LLM orchestration and language model workflows

Update text extraction notebook, closes #604

davidmezzetti77f9545a6d5f
diff --git a/examples/10_Extract_text_from_documents.ipynb b/examples/10_Extract_text_from_documents.ipynb
index c3d9c96e..e6463a04 100644
--- a/examples/10_Extract_text_from_documents.ipynb
+++ b/examples/10_Extract_text_from_documents.ipynb
@@ -19,9 +19,9 @@
"source": [
"# Extract text from documents\n",
"\n",
- "Up to this point, all the examples have been working with sections of text, which have already been split through some other means. What happens if we're working with documents? First we need to get the text out of these documents, then figure out how to index to best support similarity search.\n",
+ "Up to this point, all the examples have been working with sections of text, which have already been split through some other means. What happens if we're working with documents? First we need to get the text out of these documents, then figure out how to index to best support vector search.\n",
"\n",
- "This notebook shows how documents can have text extracted and segmented to support similarity search."
+ "This notebook shows how documents can have text extracted and split to support vector search and retrieval augmented generation (RAG)."
]
},
{
@@ -45,14 +45,14 @@
"!pip install git+https://github.com/neuml/txtai#egg=txtai[pipeline]\n",
"\n",
"# Get test data\n",
- "!wget -N https://github.com/neuml/txtai/releases/download/v3.5.0/tests.tar.gz\n",
+ "!wget -N https://github.com/neuml/txtai/releases/download/v6.2.0/tests.tar.gz\n",
"!tar -xvzf tests.tar.gz\n",
"\n",
"# Install NLTK\n",
"import nltk\n",
"nltk.download('punkt')"
],
- "execution_count": null,
+ "execution_count": 19,
"outputs": []
},
{
@@ -81,7 +81,7 @@
"# Create textractor model\n",
"textractor = Textractor()"
],
- "execution_count": null,
+ "execution_count": 20,
"outputs": []
},
{
@@ -100,28 +100,28 @@
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
- "height": 106
+ "height": 118
},
"id": "-K2YJJzsVtfq",
- "outputId": "a18f5b12-f90c-4d8d-e0b7-64350d76a086"
+ "outputId": "7754c508-264a-41fa-9843-83460719820f"
},
"source": [
"textractor(\"txtai/article.pdf\")"
],
- "execution_count": null,
+ "execution_count": 21,
"outputs": [
{
"output_type": "execute_result",
"data": {
+ "text/plain": [
+ "'Introducing txtai, an AI-powered search engine \\nbuilt on Transformers\\n\\nAdd Natural Language Understanding to any application\\n\\nSearch is the base of many applications. Once data starts to pile up, users want to be able to find it. It’s \\nthe foundation of the internet and an ever-growing challenge that is never solved or done.\\n\\nThe field of Natural Language Processing (NLP) is rapidly evolving with a number of new \\ndevelopments. Large-scale general language models are an exciting new capability allowing us to add \\namazing functionality quickly with limited compute and people. Innovation continues with new models\\nand advancements coming in at what seems a weekly basis.\\n\\nThis article introduces txtai, an AI-powered search engine that enables Natural Language \\nUnderstanding (NLU) based search in any application.\\n\\nIntroducing txtai\\ntxtai builds an AI-powered index over sections of text. txtai supports building text indices to perform \\nsimilarity searches and create extractive question-answering based systems. txtai also has functionality \\nfor zero-shot classification. txtai is open source and available on GitHub.\\n\\ntxtai and/or the concepts behind it has already been used to power the Natural Language Processing \\n(NLP) applications listed below:\\n\\n• paperai — AI-powered literature discovery and review engine for medical/scientific papers\\n• tldrstory — AI-powered understanding of headlines and story text\\n• neuspo — Fact-driven, real-time sports event and news site\\n• codequestion — Ask coding questions directly from the terminal\\n\\nBuild an Embeddings index\\nFor small lists of texts, the method above works. But for larger repositories of documents, it doesn’t \\nmake sense to tokenize and convert all embeddings for each query. txtai supports building pre-\\ncomputed indices which significantly improves performance.\\n\\nBuilding on the previous example, the following example runs an index method to build and store the \\ntext embeddings. In this case, only the query is converted to an embeddings vector each search.\\n\\nhttps://github.com/neuml/codequestion\\nhttps://neuspo.com/\\nhttps://github.com/neuml/tldrstory\\nhttps://github.com/neuml/paperai\\n - Introducing txtai, an AI-powered search engine built on Transformers\\n - Add Natural Language Understanding to any application\\n - Introducing txtai\\n - Build an Embeddings index'"
+ ],
"application/vnd.google.colaboratory.intrinsic+json": {
"type": "string"
- },
- "text/plain": [
- "'Introducing txtai, an AI-powered search engine built on Transformers Add Natural Language Understanding to any application Search is the base of many applications. Once data starts to pile up, users want to be able to find it. It’s the foundation of the internet and an ever-growing challenge that is never solved or done. The field of Natural Language Processing (NLP) is rapidly evolving with a number of new developments. Large-scale general language models are an exciting new capability allowing us to add amazing functionality quickly with limited compute and people. Innovation continues with new models and advancements coming in at what seems a weekly basis. This article introduces txtai, an AI-powered search engine that enables Natural Language Understanding (NLU) based search in any application. Introducing txtai txtai builds an AI-powered index over sections of text. txtai supports building text indices to perform similarity searches and create extractive question-answering based systems. txtai also has functionality for zero-shot classification. txtai is open source and available on GitHub. txtai and/or the concepts behind it has already been used to power the Natural Language Processing (NLP) applications listed below: • paperai — AI-powered literature discovery and review engine for medical/scientific papers • tldrstory — AI-powered understanding of headlines and story text • neuspo — Fact-driven, real-time sports event and news site • codequestion — Ask coding questions directly from the terminal Build an Embeddings index For small lists of texts, the method above works. But for larger repositories of documents, it doesn’t make sense to tokenize and convert all embeddings for each query. txtai supports building pre- computed indices which significantly improves performance. Building on the previous example, the following example runs an index method to build and store the text embeddings. In this case, only the query is converted to an embeddings vector each search. https://github.com/neuml/codequestion https://neuspo.com/ https://github.com/neuml/tldrstory https://github.com/neuml/paperai Introducing txtai, an AI-powered search engine built on Transformers Add Natural Language Understanding to any application Introducing txtai Build an Embeddings index'"
- ]
+ }
},
"metadata": {},
- "execution_count": 3
+ "execution_count": 21
}
]
},
@@ -140,7 +140,9 @@
"id": "1w2bhBCPOUdu"
},
"source": [
- "# Extract sentences"
+ "# Extract sentences\n",
+ "\n",
+ "Sentence extraction uses a model that specializes in sentence detection. This call returns a list of sentences."
]
},
{
@@ -150,39 +152,39 @@
"base_uri": "https://localhost:8080/"
},
"id": "PKZVK5vuOTqB",
- "outputId": "cb8d688e-9d55-41a2-de26-a86d4e54b23d"
+ "outputId": "a31e182e-037b-4e29-c1b0-0f6815e3b2c9"
},
"source": [
"textractor = Textractor(sentences=True)\n",
"textractor(\"txtai/article.pdf\")"
],
- "execution_count": null,
+ "execution_count": 22,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
- "['Introducing txtai, an AI-powered search engine built on Transformers Add Natural Language Understanding to any application Search is the base of many applications.',\n",
+ "['Introducing txtai, an AI-powered search engine \\nbuilt on Transformers\\n\\nAdd Natural Language Understanding to any application\\n\\nSearch is the base of many applications.',\n",
" 'Once data starts to pile up, users want to be able to find it.',\n",
- " 'It’s the foundation of the internet and an ever-growing challenge that is never solved or done.',\n",
- " 'The field of Natural Language Processing (NLP) is rapidly evolving with a number of new developments.',\n",
- " 'Large-scale general language models are an exciting new capability allowing us to add amazing functionality quickly with limited compute and people.',\n",
- " 'Innovation continues with new models and advancements coming in at what seems a weekly basis.',\n",
- " 'This article introduces txtai, an AI-powered search engine that enables Natural Language Understanding (NLU) based search in any application.',\n",
- " 'Introducing txtai txtai builds an AI-powered index over sections of text.',\n",
- " 'txtai supports building text indices to perform similarity searches and create extractive question-answering based systems.',\n",
- " 'txtai also has functionality for zero-shot classification.',\n",
+ " 'It’s \\nthe foundation of the internet and an ever-growing challenge that is never solved or done.',\n",
+ " 'The field of Natural Language Processing (NLP) is rapidly evolving with a number of new \\ndevelopments.',\n",
+ " 'Large-scale general language models are an exciting new capability allowing us to add \\namazing functionality quickly with limited compute and people.',\n",
+ " 'Innovation continues with new models\\nand advancements coming in at what seems a weekly basis.',\n",
+ " 'This article introduces txtai, an AI-powered search engine that enables Natural Language \\nUnderstanding (NLU) based search in any application.',\n",
+ " 'Introducing txtai\\ntxtai builds an AI-powered index over sections of text.',\n",
+ " 'txtai supports building text indices to perform \\nsimilarity searches and create extractive question-answering based systems.',\n",
+ " 'txtai also has functionality \\nfor zero-shot classification.',\n",
" 'txtai is open source and available on GitHub.',\n",
- " 'txtai and/or the concepts behind it has already been used to power the Natural Language Processing (NLP) applications listed below: • paperai — AI-powered literature discovery and review engine for medical/scientific papers • tldrstory — AI-powered understanding of headlines and story text • neuspo — Fact-driven, real-time sports event and news site • codequestion — Ask coding questions directly from the terminal Build an Embeddings index For small lists of texts, the method above works.',\n",
- " 'But for larger repositories of documents, it doesn’t make sense to tokenize and convert all embeddings for each query.',\n",
- " 'txtai supports building pre- computed indices which significantly improves performance.',\n",
- " 'Building on the previous example, the following example runs an index method to build and store the text embeddings.',\n",
+ " 'txtai and/or the concepts behind it has already been used to power the Natural Language Processing \\n(NLP) applications listed below:\\n\\n• paperai — AI-powered literature discovery and review engine for medical/scientific papers\\n• tldrstory — AI-powered understanding of headlines and story text\\n• neuspo — Fact-driven, real-time sports event and news site\\n• codequestion — Ask coding questions directly from the terminal\\n\\nBuild an Embeddings index\\nFor small lists of texts, the method above works.',\n",
+ " 'But for larger repositories of documents, it doesn’t \\nmake sense to tokenize and convert all embeddings for each query.',\n",
+ " 'txtai supports building pre-\\ncomputed indices which significantly improves performance.',\n",
+ " 'Building on the previous example, the following example runs an index method to build and store the \\ntext embeddings.',\n",
" 'In this case, only the query is converted to an embeddings vector each search.',\n",
- " 'https://github.com/neuml/codequestion https://neuspo.com/ https://github.com/neuml/tldrstory https://github.com/neuml/paperai Introducing txtai, an AI-powered search engine built on Transformers Add Natural Language Understanding to any application Introducing txtai Build an Embeddings index']"
+ " 'https://github.com/neuml/codequestion\\nhttps://neuspo.com/\\nhttps://github.com/neuml/tldrstory\\nhttps://github.com/neuml/paperai\\n - Introducing txtai, an AI-powered search engine built on Transformers\\n - Add Natural Language Understanding to any application\\n - Introducing txtai\\n - Build an Embeddings index']"
]
},
"metadata": {},
- "execution_count": 4
+ "execution_count": 22
}
]
},
@@ -201,7 +203,9 @@
"id": "z1H8XYkaSoP4"
},
"source": [
- "# Extract paragraphs"
+ "# Extract paragraphs\n",
+ "\n",
+ "Paragraph detection looks for consecutive newlines. This call returns a list of paragraphs."
]
},
{
@@ -211,35 +215,146 @@
"base_uri": "https://localhost:8080/"
},
"id": "9VUito4ISoAe",
- "outputId": "21770771-faa4-4340-a1ab-7fb0a8177f4c"
+ "outputId": "08079380-a7c8-4886-ecc3-de9f02be4584"
},
"source": [
"textractor = Textractor(paragraphs=True)\n",
- "textractor(\"txtai/article.pdf\")"
+ "for paragraph in textractor(\"txtai/article.pdf\"):\n",
+ " print(paragraph, \"\\n----\")"
],
- "execution_count": null,
+ "execution_count": 23,
"outputs": [
{
- "output_type": "execute_result",
- "data": {
- "text/plain": [
- "['Introducing txtai, an AI-powered search engine built on Transformers',\n",
- " 'Add Natural Language Understanding to any application',\n",
- " 'Search is the base of many applications. Once data starts to pile up, users want to be able to find it. It’s the foundation of the internet and an ever-growing challenge that is never solved or done.',\n",
- " 'The field of Natural Language Processing (NLP) is rapidly evolving with a number of new developments. Large-scale general language models are an exciting new capability allowing us to add amazing functionality quickly with limited compute and people. Innovation continues with new models and advancements coming in at what seems a weekly basis.',\n",
- " 'This article introduces txtai, an AI-powered search engine that enables Natural Language Understanding (NLU) based search in any application.',\n",
- " 'Introducing txtai txtai builds an AI-powered index over sections of text. txtai supports building text indices to perform similarity searches and create extractive question-answering based systems. txtai also has functionality for zero-shot classification. txtai is open source and available on GitHub.',\n",
- " 'txtai and/or the concepts behind it has already been used to power the Natural Language Processing (NLP) applications listed below:',\n",
- " '• paperai — AI-powered literature discovery and review engine for medical/scientific papers • tldrstory — AI-powered understanding of headlines and story text • neuspo — Fact-driven, real-time sports event and news site • codequestion — Ask coding questions directly from the terminal',\n",
- " 'Build an Embeddings index For small lists of texts, the method above works. But for larger repositories of documents, it doesn’t make sense to tokenize and convert all embeddings for each query. txtai supports building pre- computed indices which significantly improves performance.',\n",
- " 'Building on the previous example, the following example runs an index method to build and store the text embeddings. In this case, only the query is converted to an embeddings vector each search.',\n",
- " 'https://github.com/neuml/codequestion https://neuspo.com/ https://github.com/neuml/tldrstory https://github.com/neuml/paperai',\n",
- " 'Introducing txtai, an AI-powered search engine built on Transformers Add Natural Language Understanding to any application',\n",
- " 'Introducing txtai Build an Embeddings index']"
- ]
- },
- "metadata": {},
- "execution_count": 5
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "Introducing txtai, an AI-powered search engine \n",
+ "built on Transformers \n",
+ "----\n",
+ "Add Natural Language Understanding to any application \n",
+ "----\n",
+ "Search is the base of many applications. Once data starts to pile up, users want to be able to find it. It’s \n",
+ "the foundation of the internet and an ever-growing challenge that is never solved or done. \n",
+ "----\n",
+ "The field of Natural Language Processing (NLP) is rapidly evolving with a number of new \n",
+ "developments. Large-scale general language models are an exciting new capability allowing us to add \n",
+ "amazing functionality quickly with limited compute and people. Innovation continues with new models\n",
+ "and advancements coming in at what seems a weekly basis. \n",
+ "----\n",
+ "This article introduces txtai, an AI-powered search engine that enables Natural Language \n",
+ "Understanding (NLU) based search in any application. \n",
+ "----\n",
+ "Introducing txtai\n",
+ "txtai builds an AI-powered index over sections of text. txtai supports building text indices to perform \n",
+ "similarity searches and create extractive question-answering based systems. txtai also has functionality \n",
+ "for zero-shot classification. txtai is open source and available on GitHub. \n",
+ "----\n",
+ "txtai and/or the concepts behind it has already been used to power the Natural Language Processing \n",
+ "(NLP) applications listed below: \n",
+ "----\n",
+ "• paperai — AI-powered literature discovery and review engine for medical/scientific papers\n",
+ "• tldrstory — AI-powered understanding of headlines and story text\n",
+ "• neuspo — Fact-driven, real-time sports event and news site\n",
+ "• codequestion — Ask coding questions directly from the terminal \n",
+ "----\n",
+ "Build an Embeddings index\n",
+ "For small lists of texts, the method above works. But for larger repositories of documents, it doesn’t \n",
+ "make sense to tokenize and convert all embeddings for each query. txtai supports building pre-\n",
+ "computed indices which significantly improves performance. \n",
+ "----\n",
+ "Building on the previous example, the following example runs an index method to build and store the \n",
+ "text embeddings. In this case, only the query is converted to an embeddings vector each search. \n",
+ "----\n",
+ "https://github.com/neuml/codequestion\n",
+ "https://neuspo.com/\n",
+ "https://github.com/neuml/tldrstory\n",
+ "https://github.com/neuml/paperai\n",
+ " - Introducing txtai, an AI-powered search engine built on Transformers\n",
+ " - Add Natural Language Understanding to any application\n",
+ " - Introducing txtai\n",
+ " - Build an Embeddings index \n",
+ "----\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "# Extract sections\n",
+ "\n",
+ "Section extraction is format dependent. If page breaks are available, each section is a page. Otherwise, this call returns logical sections such by headings."
+ ],
+ "metadata": {
+ "id": "Ae6dRQ2LvN-w"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "textractor = Textractor(sections=True)\n",
+ "print(\"\\n[PAGE BREAK]\\n\".join(section for section in textractor(\"txtai/article.pdf\")))"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "nQ6ev2UMwqnh",
+ "outputId": "3d45491d-3547-4218-d30c-ba0c4d161256"
+ },
+ "execution_count": 24,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "Introducing txtai, an AI-powered search engine \n",
+ "built on Transformers\n",
+ "\n",
+ "Add Natural Language Understanding to any application\n",
+ "\n",
+ "Search is the base of many applications. Once data starts to pile up, users want to be able to find it. It’s \n",
+ "the foundation of the internet and an ever-growing challenge that is never solved or done.\n",
+ "\n",
+ "The field of Natural Language Processing (NLP) is rapidly evolving with a number of new \n",
+ "developments. Large-scale general language models are an exciting new capability allowing us to add \n",
+ "amazing functionality quickly with limited compute and people. Innovation continues with new models\n",
+ "and advancements coming in at what seems a weekly basis.\n",
+ "\n",
+ "This article introduces txtai, an AI-powered search engine that enables Natural Language \n",
+ "Understanding (NLU) based search in any application.\n",
+ "\n",
+ "Introducing txtai\n",
+ "txtai builds an AI-powered index over sections of text. txtai supports building text indices to perform \n",
+ "similarity searches and create extractive question-answering based systems. txtai also has functionality \n",
+ "for zero-shot classification. txtai is open source and available on GitHub.\n",
+ "\n",
+ "txtai and/or the concepts behind it has already been used to power the Natural Language Processing \n",
+ "(NLP) applications listed below:\n",
+ "\n",
+ "• paperai — AI-powered literature discovery and review engine for medical/scientific papers\n",
+ "• tldrstory — AI-powered understanding of headlines and story text\n",
+ "• neuspo — Fact-driven, real-time sports event and news site\n",
+ "• codequestion — Ask coding questions directly from the terminal\n",
+ "\n",
+ "Build an Embeddings index\n",
+ "For small lists of texts, the method above works. But for larger repositories of documents, it doesn’t \n",
+ "make sense to tokenize and convert all embeddings for each query. txtai supports building pre-\n",
+ "computed indices which significantly improves performance.\n",
+ "\n",
+ "Building on the previous example, the following example runs an index method to build and store the \n",
+ "text embeddings. In this case, only the query is converted to an embeddings vector each search.\n",
+ "\n",
+ "https://github.com/neuml/codequestion\n",
+ "https://neuspo.com/\n",
+ "https://github.com/neuml/tldrstory\n",
+ "https://github.com/neuml/paperai\n",
+ "[PAGE BREAK]\n",
+ "- Introducing txtai, an AI-powered search engine built on Transformers\n",
+ " - Add Natural Language Understanding to any application\n",
+ " - Introducing txtai\n",
+ " - Build an Embeddings index\n"
+ ]
}
]
}