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.
This commit is contained in:
2026-02-18 18:34:29 +01:00
parent 1d926e2ed1
commit a8e15d66f7

View File

@@ -65,7 +65,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 1,
"metadata": { "metadata": {
"id": "R88Cd1WAp5Br" "id": "R88Cd1WAp5Br"
}, },
@@ -1826,7 +1826,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 39, "execution_count": 45,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
@@ -1834,17 +1834,20 @@
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"\n", "\n",
"Q: I forgot my password, what should I do?\n", "Question: I forgot my password, what should I do?\n",
"No matching FAQ found\n", " - Match: How do I reset my password? (similarity: 0.6909)\n",
" - Answer: Go to Settings > Security > Reset Password\n",
"\n", "\n",
"Q: How much does delivery cost?\n", "Question: How much does delivery cost?\n",
"No matching FAQ found\n", " - Match: What are your shipping costs? (similarity: 0.5593)\n",
" - Answer: Shipping is free for orders over $50\n",
"\n", "\n",
"Q: Can I return items I purchased?\n", "Question: Can I return items I purchased?\n",
"No matching FAQ found\n", " - Match: Do you accept returns? (similarity: 0.6974)\n",
" - Answer: Yes, within 30 days with original receipt\n",
"\n", "\n",
"Q: What is the meaning of life?\n", "Question: What is the meaning of life?\n",
"No matching FAQ found\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", " \"What payment methods do you accept?\": \"We accept credit cards, PayPal, and bank transfers\",\n",
"}\n", "}\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", " \"\"\"Find the best matching FAQ for a user question.\n",
"\n", "\n",
" Args:\n", " Args:\n",
" user_question: Question from user\n", " user_question: Question from user\n",
" faqs: Dictionary of FAQ questions and answers\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", "\n",
" Returns:\n", " Returns:\n",
" Tuple of (matching_question, answer, similarity) or None\n", " Tuple of (matching_question, answer, similarity) or None\n",
@@ -1900,12 +1903,12 @@
"for query in test_queries:\n", "for query in test_queries:\n",
" result = match_faq(query, faqs)\n", " result = match_faq(query, faqs)\n",
" if result:\n", " if result:\n",
" print(f\"\\nQ: {query}\")\n", " print(f\"\\nQuestion: {query}\")\n",
" print(f\"Match: {result[0]} (similarity: {result[2]:.2f})\")\n", " print(f\" - Match: {result[0]} (similarity: {result[2]:.4f})\")\n",
" print(f\"A: {result[1]}\")\n", " print(f\" - Answer: {result[1]}\")\n",
" else:\n", " else:\n",
" print(f\"\\nQ: {query}\")\n", " print(f\"\\nQuestion: {query}\")\n",
" print(\"No matching FAQ found\")\n" " print(\" No matching FAQ found (below threshold)\")\n"
] ]
}, },
{ {