From a8e15d66f74cf3e86e8ea08509c6017b5c881f9a Mon Sep 17 00:00:00 2001 From: Arthur DANJOU Date: Wed, 18 Feb 2026 18:34:29 +0100 Subject: [PATCH] Refactor FAQ matching output and adjust similarity threshold - Updated execution counts for code cells to reflect changes. - Enhanced output formatting for matched FAQs, including clearer question and answer presentation. - Adjusted the similarity threshold in the `match_faq` function to allow for looser matching (default set to 0.5). - Improved documentation for the threshold parameter to clarify its usage. --- .../TP1 embeddings.ipynb | 37 ++++++++++--------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/M2/Natural Language Process/TP1 embeddings.ipynb b/M2/Natural Language Process/TP1 embeddings.ipynb index cbead49..e1982b3 100644 --- a/M2/Natural Language Process/TP1 embeddings.ipynb +++ b/M2/Natural Language Process/TP1 embeddings.ipynb @@ -65,7 +65,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": { "id": "R88Cd1WAp5Br" }, @@ -1826,7 +1826,7 @@ }, { "cell_type": "code", - "execution_count": 39, + "execution_count": 45, "metadata": {}, "outputs": [ { @@ -1834,17 +1834,20 @@ "output_type": "stream", "text": [ "\n", - "Q: I forgot my password, what should I do?\n", - "No matching FAQ found\n", + "Question: I forgot my password, what should I do?\n", + " - Match: How do I reset my password? (similarity: 0.6909)\n", + " - Answer: Go to Settings > Security > Reset Password\n", "\n", - "Q: How much does delivery cost?\n", - "No matching FAQ found\n", + "Question: How much does delivery cost?\n", + " - Match: What are your shipping costs? (similarity: 0.5593)\n", + " - Answer: Shipping is free for orders over $50\n", "\n", - "Q: Can I return items I purchased?\n", - "No matching FAQ found\n", + "Question: Can I return items I purchased?\n", + " - Match: Do you accept returns? (similarity: 0.6974)\n", + " - Answer: Yes, within 30 days with original receipt\n", "\n", - "Q: What is the meaning of life?\n", - "No matching FAQ found\n" + "Question: What is the meaning of life?\n", + " No matching FAQ found (below threshold)\n" ] } ], @@ -1857,13 +1860,13 @@ " \"What payment methods do you accept?\": \"We accept credit cards, PayPal, and bank transfers\",\n", "}\n", "\n", - "def match_faq(user_question: str, faqs: dict, threshold: float = 0.7) -> tuple[str, str, float] | None:\n", + "def match_faq(user_question: str, faqs: dict, threshold: float = 0.5) -> tuple[str, str, float] | None:\n", " \"\"\"Find the best matching FAQ for a user question.\n", "\n", " Args:\n", " user_question: Question from user\n", " faqs: Dictionary of FAQ questions and answers\n", - " threshold: Minimum similarity to return a match\n", + " threshold: Minimum similarity to return a match (default 0.5 for loose matching, use 0.7+ for strict)\n", "\n", " Returns:\n", " Tuple of (matching_question, answer, similarity) or None\n", @@ -1900,12 +1903,12 @@ "for query in test_queries:\n", " result = match_faq(query, faqs)\n", " if result:\n", - " print(f\"\\nQ: {query}\")\n", - " print(f\"Match: {result[0]} (similarity: {result[2]:.2f})\")\n", - " print(f\"A: {result[1]}\")\n", + " print(f\"\\nQuestion: {query}\")\n", + " print(f\" - Match: {result[0]} (similarity: {result[2]:.4f})\")\n", + " print(f\" - Answer: {result[1]}\")\n", " else:\n", - " print(f\"\\nQ: {query}\")\n", - " print(\"No matching FAQ found\")\n" + " print(f\"\\nQuestion: {query}\")\n", + " print(\" No matching FAQ found (below threshold)\")\n" ] }, {