Skip to content

Send a phone call from AI agent, in an API call. Or, directly call the bot from the configured phone number!

Python

perf: Import compressed content into AI Search

Clémence Lesné90f539aaa70f
diff --git a/examples/import-training.ipynb b/examples/import-training.ipynb
index b362c4f..c47b745 100644
--- a/examples/import-training.ipynb
+++ b/examples/import-training.ipynb
@@ -9,11 +9,77 @@
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 9,
"metadata": {},
- "outputs": [],
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Requirement already satisfied: azure-ai-documentintelligence==1.0.0b1 in /Users/clemlesne/.pyenv/versions/3.11.3/lib/python3.11/site-packages (1.0.0b1)\n",
+ "Requirement already satisfied: azure-search-documents==11.4.0 in /Users/clemlesne/.pyenv/versions/3.11.3/lib/python3.11/site-packages (11.4.0)\n",
+ "Requirement already satisfied: unidecode==1.3.8 in /Users/clemlesne/.pyenv/versions/3.11.3/lib/python3.11/site-packages (1.3.8)\n",
+ "Requirement already satisfied: nltk==3.8.1 in /Users/clemlesne/.pyenv/versions/3.11.3/lib/python3.11/site-packages (3.8.1)\n",
+ "Requirement already satisfied: isodate<1.0.0,>=0.6.1 in /Users/clemlesne/.pyenv/versions/3.11.3/lib/python3.11/site-packages (from azure-ai-documentintelligence==1.0.0b1) (0.6.1)\n",
+ "Requirement already satisfied: azure-core<2.0.0,>=1.28.0 in /Users/clemlesne/.pyenv/versions/3.11.3/lib/python3.11/site-packages (from azure-ai-documentintelligence==1.0.0b1) (1.29.6)\n",
+ "Requirement already satisfied: azure-common~=1.1 in /Users/clemlesne/.pyenv/versions/3.11.3/lib/python3.11/site-packages (from azure-search-documents==11.4.0) (1.1.28)\n",
+ "Requirement already satisfied: click in /Users/clemlesne/.pyenv/versions/3.11.3/lib/python3.11/site-packages (from nltk==3.8.1) (8.1.4)\n",
+ "Requirement already satisfied: joblib in /Users/clemlesne/.pyenv/versions/3.11.3/lib/python3.11/site-packages (from nltk==3.8.1) (1.3.1)\n",
+ "Requirement already satisfied: regex>=2021.8.3 in /Users/clemlesne/.pyenv/versions/3.11.3/lib/python3.11/site-packages (from nltk==3.8.1) (2023.6.3)\n",
+ "Requirement already satisfied: tqdm in /Users/clemlesne/.pyenv/versions/3.11.3/lib/python3.11/site-packages (from nltk==3.8.1) (4.65.0)\n",
+ "Requirement already satisfied: anyio<5.0,>=3.0 in /Users/clemlesne/.pyenv/versions/3.11.3/lib/python3.11/site-packages (from azure-core<2.0.0,>=1.28.0->azure-ai-documentintelligence==1.0.0b1) (3.7.1)\n",
+ "Requirement already satisfied: requests>=2.21.0 in /Users/clemlesne/.pyenv/versions/3.11.3/lib/python3.11/site-packages (from azure-core<2.0.0,>=1.28.0->azure-ai-documentintelligence==1.0.0b1) (2.31.0)\n",
+ "Requirement already satisfied: six>=1.11.0 in /Users/clemlesne/.pyenv/versions/3.11.3/lib/python3.11/site-packages (from azure-core<2.0.0,>=1.28.0->azure-ai-documentintelligence==1.0.0b1) (1.16.0)\n",
+ "Requirement already satisfied: typing-extensions>=4.6.0 in /Users/clemlesne/.pyenv/versions/3.11.3/lib/python3.11/site-packages (from azure-core<2.0.0,>=1.28.0->azure-ai-documentintelligence==1.0.0b1) (4.8.0)\n",
+ "Requirement already satisfied: idna>=2.8 in /Users/clemlesne/.pyenv/versions/3.11.3/lib/python3.11/site-packages (from anyio<5.0,>=3.0->azure-core<2.0.0,>=1.28.0->azure-ai-documentintelligence==1.0.0b1) (3.4)\n",
+ "Requirement already satisfied: sniffio>=1.1 in /Users/clemlesne/.pyenv/versions/3.11.3/lib/python3.11/site-packages (from anyio<5.0,>=3.0->azure-core<2.0.0,>=1.28.0->azure-ai-documentintelligence==1.0.0b1) (1.3.0)\n",
+ "Requirement already satisfied: charset-normalizer<4,>=2 in /Users/clemlesne/.pyenv/versions/3.11.3/lib/python3.11/site-packages (from requests>=2.21.0->azure-core<2.0.0,>=1.28.0->azure-ai-documentintelligence==1.0.0b1) (3.2.0)\n",
+ "Requirement already satisfied: urllib3<3,>=1.21.1 in /Users/clemlesne/.pyenv/versions/3.11.3/lib/python3.11/site-packages (from requests>=2.21.0->azure-core<2.0.0,>=1.28.0->azure-ai-documentintelligence==1.0.0b1) (1.26.16)\n",
+ "Requirement already satisfied: certifi>=2017.4.17 in /Users/clemlesne/.pyenv/versions/3.11.3/lib/python3.11/site-packages (from requests>=2.21.0->azure-core<2.0.0,>=1.28.0->azure-ai-documentintelligence==1.0.0b1) (2023.5.7)\n",
+ "Note: you may need to restart the kernel to use updated packages.\n"
+ ]
+ }
+ ],
"source": [
- "%pip install azure-ai-documentintelligence==1.0.0b1 azure-search-documents==11.4.0 unidecode==1.3.8"
+ "%pip install azure-ai-documentintelligence==1.0.0b1 azure-search-documents==11.4.0 unidecode==1.3.8 nltk==3.8.1"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Plus, install NLTK data."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 11,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ "[nltk_data] Downloading package stopwords to\n",
+ "[nltk_data] /Users/clemlesne/nltk_data...\n",
+ "[nltk_data] Package stopwords is already up-to-date!\n"
+ ]
+ },
+ {
+ "data": {
+ "text/plain": [
+ "True"
+ ]
+ },
+ "execution_count": 11,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "from nltk import download\n",
+ "\n",
+ "download(\"stopwords\")"
]
},
{
@@ -25,7 +91,7 @@
},
{
"cell_type": "code",
- "execution_count": 6,
+ "execution_count": 15,
"metadata": {},
"outputs": [],
"source": [
@@ -34,6 +100,8 @@
"from azure.core.credentials import AzureKeyCredential\n",
"from azure.core.exceptions import HttpResponseError\n",
"from azure.search.documents import SearchClient\n",
+ "from nltk.stem import SnowballStemmer\n",
+ "from nltk.tokenize import word_tokenize\n",
"from textwrap import dedent\n",
"from typing import Dict, Tuple\n",
"from unidecode import unidecode\n",
@@ -50,7 +118,7 @@
" credential=doc_credential,\n",
")\n",
"\n",
- "search_endpoint = \"https://groupama-diva-poc.search.windows.net\"\n",
+ "search_endpoint = \"https://claim-ai.search.windows.net\"\n",
"search_credential = AzureKeyCredential(\"xxx\")\n",
"search_client = SearchClient(\n",
" endpoint=search_endpoint,\n",
@@ -63,12 +131,14 @@
"cell_type": "markdown",
"metadata": {},
"source": [
- "Next, transform PDF in Markdown text. We are using Document Intelligence for that."
+ "Next, transform PDF in Markdown text. We are using Document Intelligence for that.\n",
+ "\n",
+ "Be warned that this step can take a few minutes, depending on the size of the PDF. From a minute for a few pages to 20 minutes for a 1000 pages PDF."
]
},
{
"cell_type": "code",
- "execution_count": 7,
+ "execution_count": 17,
"metadata": {},
"outputs": [
{
@@ -80,33 +150,20 @@
"Starting dataset/DISPOSITIONS GENERALES GROUPAMA COHESION.pdf\n",
"Starting dataset/CG-groupama-habitation 56221-062018.pdf\n",
"Starting dataset/conditions_generales_multirisque_climatique_2022.pdf\n",
+ "Starting dataset/Code des assurances 2024-1.pdf\n",
"Starting dataset/conditions_generales_groupama.pdf\n",
"Starting dataset/Conditions Générales Chasse-01.pdf\n",
"Starting dataset/Groupama-URD2021-FR-ecobook.pdf\n",
+ "Starting dataset/Code des assurances 2024-2.pdf\n",
+ "Starting dataset/Code des assurances 2024-3.pdf\n",
"Starting dataset/CG MRH GROUPAMA.pdf\n",
"Starting dataset/Groupama-Offre.pdf\n",
"Starting dataset/conditions-generales-assurance-auto-groupama.pdf\n",
"Starting dataset/GROUPAMA_Document-dEnregistrement-Universel_2019.pdf\n",
"Starting dataset/Groupama_Sante_Prevoyance_CG-2.pdf\n",
- "Waiting for results...\n"
- ]
- },
- {
- "name": "stderr",
- "output_type": "stream",
- "text": [
- "Unclosed client session\n",
- "client_session: <aiohttp.client.ClientSession object at 0x112da6d10>\n",
- "Unclosed client session\n",
- "client_session: <aiohttp.client.ClientSession object at 0x111893690>\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "Ended dataset/CYBER_SECURITE_3350-93501-032023-Web.pdf\n",
+ "Waiting for results...\n",
"Ended dataset/Groupama-Offre.pdf\n",
+ "Ended dataset/CYBER_SECURITE_3350-93501-032023-Web.pdf\n",
"Ended dataset/Conditions Générales Chasse-01.pdf\n",
"Ended dataset/DISPOSITIONS GENERALES GROUPAMA COHESION.pdf\n",
"Ended dataset/conditions_generales_groupama.pdf\n",
@@ -115,38 +172,10 @@
"Ended dataset/conditions_generales_multirisque_climatique_2022.pdf\n",
"Ended dataset/conditions-generales-assurance-auto-groupama.pdf\n",
"Ended dataset/CG-groupama-habitation 56221-062018.pdf\n",
- "Ended dataset/20210300_CG_GH_3350-56221.pdf\n"
- ]
- },
- {
- "name": "stderr",
- "output_type": "stream",
- "text": [
- "Unable to retrieve continuation token: cannot pickle '_io.BufferedReader' object\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "Failed dataset/20210300_CG_GH_3350-56221.pdf: (Timeout) The operation was timeout.\n",
- "Code: Timeout\n",
- "Message: The operation was timeout.\n"
- ]
- },
- {
- "name": "stderr",
- "output_type": "stream",
- "text": [
- "Unable to retrieve continuation token: cannot pickle '_io.BufferedReader' object\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "Failed dataset/20210300_CG_GH_3350-56221.pdf: Operation returned an invalid status 'Gateway Timeout'\n",
- "Content: upstream request timeout\n"
+ "Ended dataset/20210300_CG_GH_3350-56221.pdf\n",
+ "Ended dataset/Code des assurances 2024-2.pdf\n",
+ "Ended dataset/Code des assurances 2024-3.pdf\n",
+ "Ended dataset/Code des assurances 2024-1.pdf\n"
]
}
],
@@ -187,26 +216,37 @@
"cell_type": "markdown",
"metadata": {},
"source": [
- "Split the Markdown text into smaller blocks, we'll call chuncks. Each block is divided, at a minimum, by level 4 headers."
+ "Split the Markdown text into smaller blocks, we'll call chuncks. Each block is divided, at a minimum, by level 4 headers. Each block content is minified with a stemmer."
]
},
{
"cell_type": "code",
- "execution_count": 11,
+ "execution_count": 19,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
- "Created 6144 chunks\n"
+ "Created 8768 chunks\n"
]
}
],
"source": [
+ "lang = \"french\"\n",
+ "stemmer = SnowballStemmer(lang)\n",
+ "\n",
+ "\n",
+ "def compress(text: str) -> str:\n",
+ " tokenized = word_tokenize(text, language=lang)\n",
+ " tokens = [stemmer.stem(token) for token in tokenized]\n",
+ " prompt = \" \".join(tokens)\n",
+ " return prompt\n",
+ "\n",
+ "\n",
"def data(content: str, source_uri: str, title: str, iterator: int) -> dict[str, str]:\n",
" return {\n",
- " \"content\": ' '.join(re.sub('[^a-zA-Z0-9.,;:?!/#]', ' ', unidecode(content)).split()), # Remove all non syntax characters\n",
+ " \"content\": content,\n",
" \"id\": f\"{'_'.join(re.sub('[^a-z0-9]', ' ', unidecode(source_uri).lower()).split())}-{iterator}\", # Use deterministic ID to avoid duplicates after a new run\n",
" \"source_uri\": unidecode(source_uri).lower(),\n",
" \"title\": ' '.join(re.sub('[^a-z0-9]', ' ', unidecode(title).lower()).split()), # Remove all special characters\n",
@@ -225,43 +265,43 @@
" h3_title = h3_section.split(\"\\n\")[0].strip()\n",
" for h4_section in h3_section.split(\"\\n###\"):\n",
" h4_title = h4_section.split(\"\\n\")[0].strip()\n",
- " h4_content = \" \".join(h4_section.split(\"\\n\")[1:])\n",
+ " h4_content = compress(\" \".join(h4_section.split(\"\\n\")[1:]))\n",
" content = dedent(f\"\"\"\n",
" # {h1_title}\n",
" ## {h2_title}\n",
" ### {h3_title}\n",
" #### {h4_title}\n",
- " {h4_content.strip()}\n",
+ " {h4_content}\n",
" \"\"\")\n",
" documents.append(data(content, source, h4_title, iterator))\n",
" iterator += 1\n",
"\n",
" else: # No H4\n",
- " h3_content = \" \".join(h3_section.split(\"\\n\")[1:])\n",
+ " h3_content = compress(\" \".join(h3_section.split(\"\\n\")[1:]))\n",
" content = dedent(f\"\"\"\n",
" # {h1_title}\n",
" ## {h2_title}\n",
" ### {h3_title}\n",
- " {h3_content.strip()}\n",
+ " {h3_content}\n",
" \"\"\")\n",
" documents.append(data(content, source, h3_title, iterator))\n",
" iterator += 1\n",
"\n",
" else: # No H3\n",
- " h2_content = \" \".join(h2_section.split(\"\\n\")[1:])\n",
+ " h2_content = compress(\" \".join(h2_section.split(\"\\n\")[1:]))\n",
" content = dedent(f\"\"\"\n",
" # {h1_title}\n",
" ## {h2_title}\n",
- " {h2_content.strip()}\n",
+ " {h2_content}\n",
" \"\"\")\n",
" documents.append(data(content, source, h2_title, iterator))\n",
" iterator += 1\n",
"\n",
" else: # No H2\n",
- " h1_content = \" \".join(h1_section.split(\"\\n\")[1:])\n",
+ " h1_content = compress(\" \".join(h1_section.split(\"\\n\")[1:]))\n",
" content = dedent(f\"\"\"\n",
" # {h1_title}\n",
- " {h1_content.strip()}\n",
+ " {h1_content}\n",
" \"\"\")\n",
" documents.append(data(content, source, h1_title, iterator))\n",
" iterator += 1\n",
@@ -279,15 +319,15 @@
},
{
"cell_type": "code",
- "execution_count": 13,
+ "execution_count": 20,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
- "Uploading 6144 documents to Azure Search\n",
- "There are 9942 documents in the index\n"
+ "Uploading 8768 documents to Azure Search\n",
+ "There are 13628 documents in the index\n"
]
}
],