{"id":5727,"date":"2023-12-18T13:59:53","date_gmt":"2023-12-18T05:59:53","guid":{"rendered":"https:\/\/aict.nkust.edu.tw\/digitrans\/?p=5727"},"modified":"2024-12-13T20:38:01","modified_gmt":"2024-12-13T12:38:01","slug":"%e5%b0%87rag%e8%88%87semantic-search%e8%9e%8d%e5%85%a5llm%ef%bc%9a%e6%8f%90%e5%8d%87%e6%ba%96%e7%a2%ba%e6%80%a7%e8%88%87%e6%95%88%e7%8e%87","status":"publish","type":"post","link":"https:\/\/aict.nkust.edu.tw\/digitrans\/?p=5727","title":{"rendered":"\u5c07RAG\u8207Semantic Search\u878d\u5165LLM\uff1a\u63d0\u5347\u6e96\u78ba\u6027\u8207\u6548\u7387"},"content":{"rendered":"\n<p>2023-12-18 | Karen<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"data-h2-0-657db82bfd8978000180b7eb\">\u524d\u8a00<\/h2>\n\n\n\n<p>\u524d\u5e7e\u7bc7\u5206\u4eab\u4e86 IBM Watsonx.ai \u5e73\u53f0\uff0c\u4ee5\u53ca\u5728\u5e73\u53f0\u4e0a\u4f7f\u7528 LLM \u5b8c\u6210\u5ba2\u6236\u9ad4\u9a57\u5206\u6790\u3001\u8207LLM\u4e32\u9023\u8655\u7406\u8f03\u8907\u96dc\u7684\u554f\u984c\u3002\u5728\u9019\u4e00\u7bc7\u4e2d\uff0c\u6211\u5011\u60f3\u4f86\u5617\u8a66\u4f7f\u7528<strong>\u6aa2\u7d22\u589e\u5f37\u751f\u6210\uff08RAG\uff09<\/strong>\u7684\u6280\u8853\uff0cRAG \u901a\u904e\u6574\u5408\u5916\u90e8\u6578\u64da\u4f86\u589e\u5f37\u57fa\u790e\u6a21\u578b\u7684\u56de\u7b54\u80fd\u529b\uff0c\u9019\u4e0d\u50c5\u80fd\u89e3\u6c7a\u6a21\u578b\u8a13\u7df4\u6578\u64da\u7684\u5c40\u9650\u6027\u554f\u984c\uff0c\u9084\u53ef\u4ee5\u63d0\u4f9b\u66f4\u7cbe\u6e96\u548c\u76f8\u95dc\u7684\u4fe1\u606f\uff0c\u751a\u81f3\u662f\u56de\u7b54\u5c08\u696d\u9818\u57df\u554f\u984c\uff0c\u4ee5\u53ca\u8655\u7406\u5927\u578b\u6587\u6a94\u4efb\u52d9\u6642\uff0c\u80fd\u5920\u66f4\u7cbe\u6e96\uff0c\u4e14\u4e0d\u5bb9\u6613\u767c\u751f\u6191\u7a7a\u60f3\u50cf\u7684\u554f\u984c\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"data-h2-1-657db82bfd8978000180b7eb\">RAG\u57fa\u672c\u4ecb\u7d39<\/h2>\n\n\n\n<p>RAG \u6280\u8853\u662f\u4e00\u7a2e\u7d50\u5408\u4e86\u641c\u5c0b\u6aa2\u7d22\u548c\u751f\u6210\u80fd\u529b\u7684\u81ea\u7136\u8a9e\u8a00\u8655\u7406\u67b6\u69cb\uff0c\u5b83\u53ef\u4ee5\u5f9e\u5916\u90e8\u77e5\u8b58\u5eab\u641c\u5c0b\u76f8\u95dc\u4fe1\u606f\uff0c\u7136\u5f8c\u4f7f\u7528\u9019\u4e9b\u4fe1\u606f\u4f86\u751f\u6210\u56de\u61c9\u6216\u5b8c\u6210\u7279\u5b9a\u7684 NLP \u4efb\u52d9\u3002<\/p>\n\n\n\n<p>RAG \u6a21\u578b\u4e3b\u8981\u7531\u5169\u500b\u90e8\u5206\u69cb\u6210\uff1a\u6aa2\u7d22\u5668\u548c\u751f\u6210\u5668\u3002<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u6aa2\u7d22\u5668\u8ca0\u8cac\u5f9e\u5916\u90e8\u77e5\u8b58\u5eab\uff08\u4f8b\u5982\uff0c\u6587\u672c\u6578\u64da\u5eab\u6216\u9810\u5148\u8a13\u7df4\u7684\u77e5\u8b58\u5d4c\u5165\uff09\u4e2d\u6aa2\u7d22\u76f8\u95dc\u7684\u77e5\u8b58\u8a0a\u606f<\/li>\n\n\n\n<li>\u751f\u6210\u5668\u6703\u5229\u7528\u6aa2\u7d22\u5230\u7684\u77e5\u8b58\u4f86\u751f\u6210\u56de\u61c9\u3002<\/li>\n<\/ol>\n\n\n\n<p>\u900f\u904e\u9019\u500b\u67b6\u69cb\uff0c\u6a21\u578b\u53ef\u4ee5\u5f9e\u5927\u91cf\u7684\u6587\u672c\u6578\u64da\u4e2d\u627e\u5230\u8207\u554f\u984c\u76f8\u95dc\u7684\u7b54\u6848\uff0c\u4e26\u751f\u6210\u7d50\u69cb\u5316\u7684\u56de\u61c9\u3002RAG \u6280\u8853\u7684\u512a\u9ede\u662f\u5229\u7528\u5927\u91cf\u7684\u771f\u5be6\u6578\u64da\uff0c\u5f97\u51fa\u66f4\u6e96\u78ba\u53ca\u66f4\u5177\u6709\u8a9e\u5883\u7684\u56de\u7b54\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"data-h2-2-657db82bfd8978000180b7eb\">\u70ba\u4ec0\u9ebc\u9700\u8981 RAG<\/h2>\n\n\n\n<p>\u5b83\u4e3b\u8981\u89e3\u6c7a\u5927\u578b\u8a9e\u8a00\u6a21\u578b\u8655\u7406\u5373\u6642\u8a0a\u606f\uff0c\u4ee5\u53ca\u66f4\u65b0\u8cc7\u8a0a\u7684\u9650\u5236:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u8655\u7406\u5373\u6642\u6216\u6700\u65b0\u4fe1\u606f\uff1a<\/strong> \u50b3\u7d71\u7684\u5927\u578b\u8a9e\u8a00\u6a21\u578b\u5728\u7279\u5b9a\u6642\u9593\u9ede\u7684\u6578\u64da\u5feb\u7167\u4e0a\u9032\u884c\u8a13\u7df4\uff0c\u56e0\u6b64\u5b83\u5011\u50c5\u80fd\u56de\u7b54\u8a13\u7df4\u6578\u64da\u4e2d\u5305\u542b\u7684\u4fe1\u606f\u3002\u4f8b\u5982\uff0c\u554f\u4e00\u500b LLM \u4eca\u5929\u7684\u5929\u6c23\u5982\u4f55\uff0c\u5b83\u53ef\u80fd\u7121\u6cd5\u56de\u7b54\uff0c\u56e0\u70ba\u4eca\u5929\u7684\u5929\u6c23\u4fe1\u606f\u4e0d\u5305\u542b\u5728\u5b83\u7684\u8a13\u7df4\u6578\u64da\u4e2d\u3002RAG \u900f\u904e\u63a2\u7d22\u5373\u6642\u5929\u6c23\u7684 API \u6216\u76f8\u95dc\u4fe1\u606f\u4f86\u7372\u5f97\u8cc7\u8a0a\uff0c\u4e26\u5728\u95b1\u8b80\u9019\u4e9b API \u7684\u7d50\u679c\u5f8c\uff0c\u751f\u6210\u9700\u8981\u7684\u7b54\u6848\uff0c\u4ee5\u5f4c\u88dc\u8655\u7406\u5373\u6642\u554f\u984c\u7684\u9650\u5236\u3002<\/li>\n\n\n\n<li><strong>\u63d0\u4f9b\u66f4\u6e96\u78ba\u548c\u76f8\u95dc\u7684\u56de\u7b54\uff1a<\/strong> \u901a\u904e\u6aa2\u7d22\u548c\u6574\u5408\u8207\u7528\u6236\u67e5\u8a62\u76f8\u95dc\u7684\u5916\u90e8\u4fe1\u606f\uff0cRAG \u80fd\u5920\u4f7f\u6a21\u578b\u63d0\u4f9b\u66f4\u6e96\u78ba\u3001\u66f4\u5177\u76f8\u95dc\u6027\u7684\u7b54\u6848\u3002\u9019\u5c0d\u65bc\u9700\u8981\u5c08\u696d\u77e5\u8b58\u6216\u7279\u5b9a\u9818\u57df\u4fe1\u606f\u7684\u67e5\u8a62\u7279\u5225\u6709\u7528\uff0c\u9019\u4e9b\u5c08\u696d\u77e5\u8b58\u53ef\u80fd\u4f86\u81ea\u67d0\u4e9b\u9577\u7bc7\u7684\u6587\u4ef6\u4e2d\uff0c\u90a3\u5c31\u5f88\u9069\u5408\u900f\u904e RAG \u53bb\u6aa2\u7d22\u8207\u751f\u6210\uff0c\u4e14\u751f\u6210\u7684\u5167\u5bb9\u4e5f\u6703\u6839\u64da\u6b64\u9577\u6587\uff0c\u5c0e\u51fa\u7cbe\u78ba\u7684\u56de\u7b54\u3002<\/li>\n\n\n\n<li><strong>\u64f4\u5c55\u6a21\u578b\u7684\u61c9\u7528\u7bc4\u570d\uff1a<\/strong> RAG \u901a\u904e\u6574\u5408\u4e0d\u540c\u7684\u6578\u64da\u6e90\uff08\u5982\u5be6\u6642API\u6578\u64da\u3001\u5c08\u696d\u6587\u4ef6\u7b49\uff09\uff0c\u4f7f\u6a21\u578b\u80fd\u5920\u89e3\u7b54\u539f\u672c\u8d85\u51fa\u5176\u8a13\u7df4\u7bc4\u570d\u7684\u554f\u984c\uff0c\u5f9e\u800c\u64f4\u5c55\u4e86\u6a21\u578b\u7684\u61c9\u7528\u7bc4\u570d\u3002<\/li>\n\n\n\n<li><strong>\u512a\u5316\u6210\u672c\u548c\u6548\u7387\uff1a<\/strong> \u5c0d\u65bc\u9700\u8981\u8655\u7406\u5927\u91cf\u6587\u6a94\u6216\u6578\u64da\u7684\u5834\u666f\uff0cRAG \u53ef\u4ee5\u901a\u904e\u53ea\u6aa2\u7d22\u548c\u8655\u7406\u8207\u7279\u5b9a\u67e5\u8a62\u6700\u76f8\u95dc\u7684\u4fe1\u606f\uff0c\u4f86\u63d0\u9ad8\u8655\u7406\u6548\u7387\u548c\u964d\u4f4e\u904b\u7b97\u6210\u672c\u3002<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"data-h2-3-657db82bfd8978000180b7eb\">\u8a9e\u7fa9\u6aa2\u7d22\uff08Semantic Search\uff09\u57fa\u672c\u4ecb\u7d39<\/h2>\n\n\n\n<p>\u8207RAG\u7dca\u5bc6\u76f8\u9023\u7684\u53e6\u4e00\u9805\u95dc\u9375\u6280\u8853\u662f\u8a9e\u7fa9\u6aa2\u7d22\u3002<strong>\u8a9e\u7fa9\u6aa2\u7d22\u6280\u8853\u4e3b\u8981\u7528\u65bc\u5f9e\u5927\u898f\u6a21\u6578\u64da\u96c6\u4e2d\u5feb\u901f\u8b58\u5225\u51fa\u8207\u7279\u5b9a\u67e5\u8a62\u8a9e\u7fa9\u4e0a\u6700\u76f8\u95dc\u7684\u4fe1\u606f\u7247\u6bb5<\/strong>\u3002\u9019\u7a2e\u6280\u8853\u901a\u904e\u5c07\u6587\u672c\u8f49\u63db\u70ba\u6578\u503c\u5411\u91cf\uff08\u5d4c\u5165\uff09\uff0c\u4e26\u8a08\u7b97\u9019\u4e9b\u5411\u91cf\u4e4b\u9593\u7684\u76f8\u4f3c\u5ea6\uff0c\u4f86\u78ba\u5b9a\u54ea\u4e9b\u6587\u672c\u7247\u6bb5\u8207\u7528\u6236\u7684\u67e5\u8a62\u6700\u70ba\u5339\u914d\u3002<\/p>\n\n\n\n<p>\u5982\u679c\u60f3\u8b93\u6a21\u578b\u56de\u7b54\u95dc\u65bc\u4e00\u672c\u6f5b\u6c34\u8247\u624b\u518a\uff08\u9577\u9054492\u9801\uff09\u7684\u554f\u984c\uff0c\u76f4\u63a5\u5c07\u6574\u672c\u624b\u518a\u4f5c\u70ba\u4e0a\u4e0b\u6587\u63d0\u4f9b\u7d66\u6a21\u578b\u662f\u4e0d\u5207\u5be6\u969b\u7684\u3002\u9019\u662f\u56e0\u70ba\u9019\u6a23\u7684\u5927\u91cf\u6587\u672c\u6703\u8d85\u904e\u6a21\u578b\u7684\u8655\u7406\u80fd\u529b\u548c\u6700\u5927\u6a19\u8a18\u9650\u5236\u3002\u6b64\u5916\uff0c\u7531\u65bc\u57fa\u65bc\u4f7f\u7528\u7684 token \u6578\u8a08\u8cbb\uff0c\u904e\u9577\u7684\u63d0\u793a\u6703\u5c0e\u81f4\u6210\u672c\u589e\u52a0\u3002<\/p>\n\n\n\n<p>\u9019\u7a2e\u60c5\u6cc1\u4e0b\uff0c<strong>\u8a9e\u7fa9\u6aa2\u7d22\uff08Semantic Search\uff09\u6280\u8853<\/strong>\u5c31\u986f\u5f97\u975e\u5e38\u91cd\u8981\u3002\u5176\u6838\u5fc3\u601d\u60f3\u662f\u5c07\u5927\u578b\u6587\u6a94\u5206\u89e3\u6210\u8f03\u5c0f\u7684\u6587\u672c\u7247\u6bb5\u3002\u9019\u6a23\u4e0d\u50c5\u4f7f\u5f97\u6587\u672c\u8655\u7406\u548c\u6aa2\u7d22\u8b8a\u5f97\u66f4\u9ad8\u6548\uff0c\u4e5f\u4f7f\u6574\u500b\u904e\u7a0b\u66f4\u7d93\u6fdf\u5be6\u7528\u3002<\/p>\n\n\n\n<p>\u5177\u9ad4\u4f86\u8aaa\uff0c\u6211\u5011\u5c07\u6587\u672c\u7247\u6bb5\u8f49\u63db\u6210 vector\uff0c\u7136\u5f8c\u6e2c\u91cf\u9019\u4e9b\u5411\u91cf\u4e4b\u9593\u7684\u8ddd\u96e2\uff0c\u5f9e\u800c\u8b58\u5225\u51fa\u8207\u7528\u6236\u63d0\u554f\u5728\u8a9e\u7fa9\u4e0a\u6700\u63a5\u8fd1\u7684\u6587\u672c\u7247\u6bb5\u3002\u9019\u6a23\uff0c\u6211\u5011\u5c31\u53ea\u9700\u5c07\u6700\u76f8\u95dc\u7684\u6587\u672c\u90e8\u5206\u4f5c\u70ba\u4e0a\u4e0b\u6587\u63d0\u4f9b\u7d66\u6a21\u578b\uff0c\u800c\u4e0d\u662f\u6574\u500b\u5927\u578b\u6587\u6a94\u3002\u7e3d\u800c\u8a00\u4e4b\uff0c\u900f\u904e\u5c07\u5927\u578b\u6587\u6a94\u5206\u89e3\u4e26\u4f7f\u7528\u8a9e\u7fa9\u6aa2\u7d22\u6280\u8853\uff0c\u6211\u5011\u53ef\u4ee5\u66f4\u9ad8\u6548\u5730\u5229\u7528\u8a9e\u8a00\u6a21\u578b\u4f86\u56de\u7b54\u8907\u96dc\u548c\u5c08\u696d\u7684\u554f\u984c\uff0c\u540c\u6642\u63a7\u5236\u6210\u672c\u548c\u63d0\u5347\u56de\u7b54\u7684\u6e96\u78ba\u6027\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"data-h2-4-657db82bfd8978000180b7eb\">\u5be6\u4f5c<\/h2>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"data-h2-5-657db82bfd8978000180b7eb\">RAG \u56de\u7b54 PDF \u554f\u984c<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"data-h3-0-657db82bfd8978000180b7eb\">\u5c0e\u5165 pdf \u6587\u4ef6<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">def pdf_to_text(path: str,<br>                start_page: int = 1,<br>                end_page: Optional[int or None] = None) -&gt; list[str]:<br>    \"\"\"<br>    Converts PDF to plain text.<br><br>    Params:<br>        path (str): Path to the PDF file.<br>        start_page (int): Page to start getting text from.<br>        end_page (int): Last page to get text from.<br>    \"\"\"<br>    loader = PyPDFLoader(path)<br>    pages = loader.load()<br>    total_pages = len(pages)<br><br>    if end_page is None:<br>        end_page = len(pages)<br><br>    text_list = []<br>    for i in range(start_page-1, end_page):<br>        text = pages[i].page_content<br>        text = text.replace('\\n', ' ')<br>        text = re.sub(r'\\s+', ' ', text)<br>        text_list.append(text)<br><br>    return text_list<br>    <br>text_list = pdf_to_text(\"pdfs\/DQ2.pdf\")<br>print(text_list[:2])<\/pre>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/d2a6d2ofes041u.cloudfront.net\/resize?compression=6&amp;norotation=true&amp;url=https%3A%2F%2Fimages.vocus.cc%2F2353c272-4b94-401d-abb5-5e8c08eab2cb.png&amp;width=740&amp;sign=loAUND3IQh_0-NlVBvUlV1qK8CHz3E98fhWQwVeijUU\" alt=\"raw-image\"\/><\/figure>\n<\/div>\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/d2a6d2ofes041u.cloudfront.net\/resize?compression=6&amp;norotation=true&amp;url=https%3A%2F%2Fimages.vocus.cc%2F2353c272-4b94-401d-abb5-5e8c08eab2cb.png&amp;width=740&amp;sign=loAUND3IQh_0-NlVBvUlV1qK8CHz3E98fhWQwVeijUU\" alt=\"zoomable\"\/><\/figure>\n<\/div>\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/d2a6d2ofes041u.cloudfront.net\/resize?compression=6&amp;norotation=true&amp;url=https%3A%2F%2Fimages.vocus.cc%2F2353c272-4b94-401d-abb5-5e8c08eab2cb.png&amp;width=740&amp;sign=loAUND3IQh_0-NlVBvUlV1qK8CHz3E98fhWQwVeijUU\" alt=\"zoomable\"\/><\/figure>\n<\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"data-h3-1-657db82bfd8978000180b7eb\">\u6587\u5b57\u9810\u8655\u7406<\/h3>\n\n\n\n<p>\u4ee5\u9801\u70ba\u55ae\u4f4d\u5206\u5272\uff0c\u4e26\u8a18\u9304\u6bcf\u500b\u7247\u6bb5\u7684\u9801\u78bc\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">def text_to_chunks(texts: list[str],<br>                   start_page: int = 1) -&gt; list[list[str]]:<br>    \"\"\"<br>    Splits the text into equally distributed chunks.<br><br>    Args:<br>        texts (str): List of texts to be converted into chunks.<br>        word_length (int): Maximum number of words in each chunk.<br>        start_page (int): Starting page number for the chunks.<br>    \"\"\"<br>    text_toks = [t.split(' ') for t in texts]<br>    chunks = []<br><br>    for idx, words in enumerate(text_toks):<br><br>        chunk = ' '.join(words).strip()<br>        chunk = f'[Page no. {idx+start_page}]' + ' ' + '\"' + chunk + '\"'<br>        chunks.append(chunk)<br><br>    return chunks<br>    <br>chunks = text_to_chunks(text_list)<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"data-h3-2-657db82bfd8978000180b7eb\">\u8f49\u63db\u6210 vector \u4f7f\u7528 text embeddings<\/h3>\n\n\n\n<p>\u61c9\u7528 hugging dace \u7684 <strong>universal-sentence-encoder_4 <\/strong>\u6a21\u578b\u5c07\u6587\u5b57\u8f49\u63db\u6210 vector<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">def get_text_embedding(texts: list[list[str]],<br>                       batch: int = 1000) -&gt; list[Any]:<br>        \"\"\"<br>        Get the embeddings from the text.<br><br>        Args:<br>            texts (list(str)): List of chucks of text.<br>            batch (int): Batch size.<br>        \"\"\"<br>        embeddings = []<br>        for i in range(0, len(texts), batch):<br>            text_batch = texts[i:(i+batch)]<br>            # Embeddings model<br>            emb_batch = emb_function(text_batch)<br>            embeddings.append(emb_batch)<br>        embeddings = np.vstack(embeddings)<br>        return embeddings<br>        <br>embeddings = get_text_embedding(chunks)<br><br>print(embeddings.shape)<br>print(f\"Our text was embedded into {embeddings.shape[1]} dimensions\")<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"data-h3-3-657db82bfd8978000180b7eb\">\u53ef\u8996\u5316 Semantic Search<\/h3>\n\n\n\n<p>\u525b\u525b\u8f49\u63db\u6210 384 \u7dad\u5ea6\u7684\u5411\u91cf\uff0c\u6211\u5011\u4f7f\u7528 t-SNE \u7b97\u6cd5\uff0c\u964d\u5230\u4e8c\u7dad\uff0c\u8b93\u6211\u5011\u53ef\u4ee5\u5c31\u5716\u5f62\u505a\u89c0\u5bdf<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># Create a t-SNE model<br>tsne = TSNE(n_components=2, random_state=42)<br>embeddings_with_question = np.vstack([embeddings, emb_question])<br>embeddings_2d = tsne.fit_transform(embeddings_with_question)<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">def visualize_embeddings(embeddings_2d: np.ndarray,<br>                         question: Optional[bool] = False,<br>                         neighbors: Optional[np.ndarray] = None) -&gt; None:<br>    \"\"\"<br>    Visualize 384-dimensional embeddings in 2D using t-SNE, label each data point with its index,<br>    and optionally plot a question data point as a red dot with the label 'q'.<br><br>    Args:<br>        embeddings (numpy.array): An array of shape (num_samples, 384) containing the embeddings.<br>        question (numpy.array, optional): An additional 384-dimensional embedding for the question.<br>                                          Default is None.<br>    \"\"\"<br><br>    # Scatter plot the 2D embeddings and label each data point with its index<br>    plt.figure(figsize=(10, 8))<br>    num_samples = embeddings.shape[0]<br>    if neighbors is not None:<br>        for i, (x, y) in enumerate(embeddings_2d[:num_samples]):<br>            if i in neighbors:<br>                plt.scatter(x, y, color='purple', alpha=0.7)<br>                plt.annotate(str(i), xy=(x, y), xytext=(5, 2), textcoords='offset points', color='black')<br>            else:<br>                plt.scatter(x, y, color='blue', alpha=0.7)<br>                plt.annotate(str(i), xy=(x, y), xytext=(5, 2), textcoords='offset points', color='black')<br>    else:<br>        for i, (x, y) in enumerate(embeddings_2d[:num_samples]):<br>            plt.scatter(x, y, color='blue', alpha=0.7)<br>            plt.annotate(str(i), xy=(x, y), xytext=(5, 2), textcoords='offset points', color='black')<br><br>    # Plot the question data point if provided<br>    if question:<br>        x, y = embeddings_2d[-1]  # Last point corresponds to the question<br>        plt.scatter(x, y, color='red', label='q')<br>        plt.annotate('q', xy=(x, y), xytext=(5, 2), textcoords='offset points', color='black')<br><br>    plt.title('t-SNE Visualization of 384-dimensional Embeddings')<br>    plt.xlabel('Dimension 1')<br>    plt.ylabel('Dimension 2')<br>    plt.show()<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">visualize_embeddings(embeddings_2d[:-1])<\/pre>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter is-resized\"><img decoding=\"async\" src=\"https:\/\/d2a6d2ofes041u.cloudfront.net\/resize?compression=6&amp;norotation=true&amp;url=https%3A%2F%2Fimages.vocus.cc%2F9bfba657-8ff1-4b97-8906-a3a22db8b4c8.png&amp;width=740&amp;sign=W4fY79fI2-UBU9XDIsbFxgJmCgJaruWUs4WibrGf94Y\" alt=\"raw-image\" style=\"width:616px;height:auto\"\/><\/figure>\n<\/div>\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter is-resized\"><img decoding=\"async\" src=\"https:\/\/d2a6d2ofes041u.cloudfront.net\/resize?compression=6&amp;norotation=true&amp;url=https%3A%2F%2Fimages.vocus.cc%2F9bfba657-8ff1-4b97-8906-a3a22db8b4c8.png&amp;width=740&amp;sign=W4fY79fI2-UBU9XDIsbFxgJmCgJaruWUs4WibrGf94Y\" alt=\"zoomable\" style=\"width:628px;height:auto\"\/><\/figure>\n<\/div>\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter is-resized\"><img decoding=\"async\" src=\"https:\/\/d2a6d2ofes041u.cloudfront.net\/resize?compression=6&amp;norotation=true&amp;url=https%3A%2F%2Fimages.vocus.cc%2F9bfba657-8ff1-4b97-8906-a3a22db8b4c8.png&amp;width=740&amp;sign=W4fY79fI2-UBU9XDIsbFxgJmCgJaruWUs4WibrGf94Y\" alt=\"zoomable\" style=\"width:645px;height:auto\"\/><\/figure>\n<\/div>\n\n\n<p>\u6211\u5011\u8981\u900f\u904e\u6587\u5b57\u7247\u6bb5\u7684\u8ddd\u96e2\uff0c\u4f86\u627e\u5230\u53ef\u80fd\u5305\u542b\u554f\u984c\u7b54\u6848\u7684\u7247\u6bb5\uff0c\u9019\u88e1\u7684\u8ddd\u96e2\u9078\u7528\u6b50\u5e7e\u91cc\u5fb7\u8ddd\u96e2\u8a08\u7b97\u76f8\u4f3c\u5ea6\u8207\u63a5\u8fd1\u6027\uff0c\u63a1\u7528<strong>\u6700\u8fd1\u9130\u5c45<\/strong>(NearestNeighbors)\u7b97\u6cd5\uff0c\u9078\u64c7\u6700\u63a5\u8fd1\u7684 k \u500b\u6578\u64da\uff0ck \u5b9a\u70ba 5\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">nn_2d = NearestNeighbors(n_neighbors=5, metric='cosine')<br>nn_2d.fit(embeddings_2d[:-1])<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">neighbors = nn_2d.kneighbors(embeddings_2d[-1].reshape(1, -1), return_distance=False)<br>neighbors<\/pre>\n\n\n\n<p>\u518d\u5c07\u5b83\u53ef\u8996\u5316<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">visualize_embeddings(embeddings_2d, True, neighbors)<\/pre>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter is-resized\"><img decoding=\"async\" src=\"https:\/\/d2a6d2ofes041u.cloudfront.net\/resize?compression=6&amp;norotation=true&amp;url=https%3A%2F%2Fimages.vocus.cc%2F099547b8-c53b-4aee-86c5-b922fdf227fa.png&amp;width=740&amp;sign=lQz2X1u-553G6lUPgkuUmr9NrQotwaP1X3bfeRU8xCU\" alt=\"raw-image\" style=\"width:594px;height:auto\"\/><\/figure>\n<\/div>\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter is-resized\"><img decoding=\"async\" src=\"https:\/\/d2a6d2ofes041u.cloudfront.net\/resize?compression=6&amp;norotation=true&amp;url=https%3A%2F%2Fimages.vocus.cc%2F099547b8-c53b-4aee-86c5-b922fdf227fa.png&amp;width=740&amp;sign=lQz2X1u-553G6lUPgkuUmr9NrQotwaP1X3bfeRU8xCU\" alt=\"zoomable\" style=\"width:587px;height:auto\"\/><\/figure>\n<\/div>\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter is-resized\"><img decoding=\"async\" src=\"https:\/\/d2a6d2ofes041u.cloudfront.net\/resize?compression=6&amp;norotation=true&amp;url=https%3A%2F%2Fimages.vocus.cc%2F099547b8-c53b-4aee-86c5-b922fdf227fa.png&amp;width=740&amp;sign=lQz2X1u-553G6lUPgkuUmr9NrQotwaP1X3bfeRU8xCU\" alt=\"zoomable\" style=\"width:577px;height:auto\"\/><\/figure>\n<\/div>\n\n\n<p>\u7d2b\u8272\u7684\u9ede\u662f\u6bd4\u8f03\u9760\u8fd1\u7684\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"data-h3-4-657db82bfd8978000180b7eb\">\u4f7f\u7528 Semantic Search\u627e\u6700\u8fd1\u7684\u7247\u6bb5<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">question = '\u5982\u4f55\u67e5\u8a62\u6210\u4ea4\u660e\u7d30?'<br>emb_question = emb_function([question])<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">neighbors = nn.kneighbors(emb_question, return_distance=False)<br>neighbors<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"data-h3-5-657db82bfd8978000180b7eb\">\u6e96\u5099 prompt<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">def build_prompt(question):<br>    prompt = \"\"<br><br><br>    prompt += \"[INST] &lt;&lt;SYS&gt;&gt;You are an AI assistant tasked with providing answers by summarizing related documents. You should follow these rules:\\n\"\\<br>              \"1. Summarize the content from the provided documents, using the following format:\\n\"\\<br>              \"Topic of the Document: Describe the topic of the document.\\n\"\\<br>              \"Step by Step Instruction: Provide user question-specific instructions or information from the document.\\n\"\\<br>              \"2. If no relevant information is found in the chat history, respond with \\\"I can't answer the question\\\".\\n\"\\<br>              \"By adhering to these rules, you will help users find accurate and valuable information.\\n\"\\<br>              \"&lt;&lt;\/SYS&gt;&gt;\\n\"<br><br>    prompt += 'Search results:\\n'<br><br>    for c in topn_chunks:<br>        prompt += c + '\\n\\n'<br>    prompt += f\"\\n\\n\\nQuery: {question}\\n\\nAnswer: [\/INST]\"<br><br>    return prompt<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">prompt = build_prompt(question)<br>print(prompt)<\/pre>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter is-resized\"><img decoding=\"async\" src=\"https:\/\/d2a6d2ofes041u.cloudfront.net\/resize?compression=6&amp;norotation=true&amp;url=https%3A%2F%2Fimages.vocus.cc%2Fcf70011b-2c36-44d7-b793-28c04cdecad6.png&amp;width=740&amp;sign=2eFp0HliRUyClCHLHkSXaXLYJwG2Zslq8xLQmz_kLQ8\" alt=\"raw-image\" style=\"width:633px;height:auto\"\/><\/figure>\n<\/div>\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter is-resized\"><img decoding=\"async\" src=\"https:\/\/d2a6d2ofes041u.cloudfront.net\/resize?compression=6&amp;norotation=true&amp;url=https%3A%2F%2Fimages.vocus.cc%2Fcf70011b-2c36-44d7-b793-28c04cdecad6.png&amp;width=740&amp;sign=2eFp0HliRUyClCHLHkSXaXLYJwG2Zslq8xLQmz_kLQ8\" alt=\"zoomable\" style=\"width:628px;height:auto\"\/><\/figure>\n<\/div>\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter is-resized\"><img decoding=\"async\" src=\"https:\/\/d2a6d2ofes041u.cloudfront.net\/resize?compression=6&amp;norotation=true&amp;url=https%3A%2F%2Fimages.vocus.cc%2Fcf70011b-2c36-44d7-b793-28c04cdecad6.png&amp;width=740&amp;sign=2eFp0HliRUyClCHLHkSXaXLYJwG2Zslq8xLQmz_kLQ8\" alt=\"zoomable\" style=\"width:638px;height:auto\"\/><\/figure>\n<\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"data-h3-6-657db82bfd8978000180b7eb\">\u6700\u5f8c\u4e86\uff01 inference<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">def send_to_watsonxai(prompts,<br>                    model_name=\"meta-llama\/llama-2-70b-chat\",<br>                    decoding_method=\"greedy\",<br>                    max_new_tokens=512,<br>                    min_new_tokens=0,<br>                    repetition_penalty=1.0<br>                    ):<br>    '''<br>   helper function for sending prompts and params to Watsonx.ai<br><br>    Args:<br>        prompts:list list of text prompts<br>        decoding:str Watsonx.ai parameter \"sample\" or \"greedy\"<br>        max_new_tok:int Watsonx.ai parameter for max new tokens\/response returned<br>        temperature:float Watsonx.ai parameter for temperature (range 0&gt;2)<br>        repetition_penalty:float Watsonx.ai parameter for repetition penalty (range 1.0 to 2.0)<br><br>    Returns: None<br>        prints response<br>    '''<br><br><br>    # Instantiate parameters for text generation<br>    model_params = {<br>        GenParams.DECODING_METHOD: decoding_method,<br>        GenParams.MIN_NEW_TOKENS: min_new_tokens,<br>        GenParams.MAX_NEW_TOKENS: max_new_tokens,<br>        GenParams.RANDOM_SEED: 42,<br>        GenParams.TEMPERATURE: 0,<br>        GenParams.REPETITION_PENALTY: repetition_penalty,<br>    }<br><br><br>    # Instantiate a model proxy object to send your requests<br>    model = Model(<br>        model_id=model_name,<br>        params=model_params,<br>        credentials=creds,<br>        project_id=project_id)<br><br><br>    for prompt in prompts:<br>        print(model.generate_text(prompt))<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"># Example questions for the DQ2:<br>#   \u53ef\u4ee5\u600e\u9ebc\u67e5\u8a62\u6210\u4ea4\u72c0\u6cc1?<br>#   \u6709\u8fa6\u6cd5\u67e5\u8a62\u539f\u7269\u6599\u884c\u60c5\u55ce?<br>#   \u6211\u60f3\u67e5\u8a62\u6307\u6578\u7684\u59d4\u8cb7\u8ddf\u59d4\u8ce3\u5206\u6790<br>#   DQ2 \u53ef\u4ee5\u505a\u5230\u4ec0\u9ebc\u529f\u80fd?<br><br>question = \"\u6709\u8fa6\u6cd5\u67e5\u8a62\u539f\u7269\u6599\u884c\u60c5\u55ce?\"<br>prompt = build_prompt(question)<br><br>send_to_watsonxai(prompts=[prompt], min_new_tokens=1)<\/pre>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/d2a6d2ofes041u.cloudfront.net\/resize?compression=6&amp;norotation=true&amp;url=https%3A%2F%2Fimages.vocus.cc%2Fd4bcd74b-6e6f-4258-bd1c-3a137a4ed517.png&amp;width=740&amp;sign=F0EFDgjQ5Hc6Zd501lRt6XGgS97APy6lw1tk58RMjDU\" alt=\"raw-image\"\/><\/figure>\n<\/div>\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/d2a6d2ofes041u.cloudfront.net\/resize?compression=6&amp;norotation=true&amp;url=https%3A%2F%2Fimages.vocus.cc%2Fd4bcd74b-6e6f-4258-bd1c-3a137a4ed517.png&amp;width=740&amp;sign=F0EFDgjQ5Hc6Zd501lRt6XGgS97APy6lw1tk58RMjDU\" alt=\"zoomable\"\/><\/figure>\n<\/div>\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/d2a6d2ofes041u.cloudfront.net\/resize?compression=6&amp;norotation=true&amp;url=https%3A%2F%2Fimages.vocus.cc%2Fd4bcd74b-6e6f-4258-bd1c-3a137a4ed517.png&amp;width=740&amp;sign=F0EFDgjQ5Hc6Zd501lRt6XGgS97APy6lw1tk58RMjDU\" alt=\"zoomable\"\/><\/figure>\n<\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"data-h2-6-657db82bfd8978000180b7eb\">\u5c0f\u5fc3\u5f97<\/h2>\n\n\n\n<p>\u5728\u4e86\u89e3 RAG\uff08\u6aa2\u7d22\u589e\u5f37\u751f\u6210\uff09\u8207\u8a9e\u7fa9\u6aa2\u7d22\uff08Semantic Search\uff09\u6280\u8853\u5f8c\uff0c\u6211\u767c\u73fe\u96d6\u7136\u5b83\u5011\u5728\u6280\u8853\u5c64\u9762\u4e0a\u6bd4\u4e4b\u524d\u6211\u5206\u4eab\u7684\u5167\u5bb9\u4f86\u5f97\u8907\u96dc\uff0c\u4f46\u5be6\u969b\u4e0a\u5176\u6838\u5fc3\u6982\u5ff5\u975e\u5e38\u660e\u78ba\u4e14\u76f4\u89c0\u3002\u9019\u4e9b\u6280\u8853\u7684\u51fa\u73fe\uff0c\u4e3b\u8981\u662f\u70ba\u4e86\u89e3\u6c7a\u7576\u524d\u5927\u578b\u8a9e\u8a00\u6a21\u578b\uff08LLM\uff09\u5728\u8655\u7406\u5373\u6642\u66f4\u65b0\u8cc7\u8a0a\u6216\u7279\u5b9a\u9818\u57df\u6df1\u5ea6\u77e5\u8b58\u65b9\u9762\u7684\u5c40\u9650\u6027\u3002RAG \u548c\u8a9e\u7fa9\u6aa2\u7d22\u9019\u4e9b\u201c\u5de7\u5999\u7684\u65b9\u6cd5\u201d\u5be6\u969b\u4e0a\u4ee3\u8868\u4e86\u5728\u73fe\u6709\u6280\u8853\u689d\u4ef6\u4e0b\u7684\u5275\u65b0\u8207\u7a81\u7834\u3002\u6211\u8a8d\u70ba\uff0c\u96a8\u8457\u6280\u8853\u7684\u4e0d\u65b7\u767c\u5c55\uff0c\u9019\u4e9b\u9650\u5236\u5f88\u53ef\u80fd\u6703\u5f88\u5feb\u88ab\u514b\u670d\u3002\u8207\u6b64\u540c\u6642\uff0c\u9019\u4e9b\u65b9\u6cd5\u5728\u7bc0\u7d04\u8a08\u7b97\u8cc7\u6e90\u548c\u63d0\u9ad8\u56de\u7b54\u7cbe\u78ba\u5ea6\u65b9\u9762\u7684\u6f5b\u529b\u4e5f\u4ee4\u6211\u89ba\u5f97\u5b83\u5011\u975e\u5e38\u503c\u5f97\u5617\u8a66\u548c\u6df1\u5165\u63a2\u7a76\uff0cRAG \u548c\u8a9e\u7fa9\u6aa2\u7d22\u4e0d\u50c5\u662f\u7576\u524d\u554f\u984c\u7684\u6709\u6548\u89e3\u6c7a\u65b9\u6848\uff0c\u4e5f\u53ef\u80fd\u70ba\u672a\u4f86\u8a9e\u8a00\u6a21\u578b\u7684\u767c\u5c55\u63d0\u4f9b\u65b0\u7684\u601d\u8def\u548c\u9748\u611f\uff0c\u611f\u8b1d\u770b\u5230\u9019\u88e1\u7684\u4f60\uff0c\u6211\u5011\u4e0b\u6b21\u898b\uff01<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"data-h2-7-657db82bfd8978000180b7eb\">\u53c3\u8003<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>RAG \u6aa2\u7d22\u589e\u5f37\u751f\u6210\u2014 \u8b93\u5927\u578b\u8a9e\u8a00\u6a21\u578b\u66f4\u8070\u660e\u7684\u79d8\u5bc6\u6b66\u5668. \u770b\u5b8c\u6587\u7ae0\u5f8c\u6b61\u8fce\u6309\u9f13\u52f5\uff0c\u8a02\u95b1\uff0c\u4e26\u5206\u4eab\u7d66\u6240\u6709\u60f3\u77e5\u9053\u6b64\u985e\u77e5\u8b58\u7684\u6240\u6709\u4eba\uff01 | by SimonLiu | Nov, 2023 | InfuseAI<\/li>\n\n\n\n<li>Universal Sentence Encoder<\/li>\n\n\n\n<li>Lab watsonx_pdf_rag<\/li>\n\n\n\n<li>\u5168\u7aef LLM \u61c9\u7528\u958b\u767c-Day29-Langchain \u7684 chain<\/li>\n<\/ul>\n\n\n\n<p>\u8cc7\u6599\u4f86\u6e90:<a href=\"https:\/\/vocus.cc\/article\/657db82bfd8978000180b7eb\" data-type=\"link\" data-id=\"https:\/\/vocus.cc\/article\/657db82bfd8978000180b7eb\">https:\/\/vocus.cc\/article\/657db82bfd8978000180b7eb<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>2023-12-18 | Karen \u524d\u8a00 \u524d\u5e7e\u7bc7\u5206\u4eab\u4e86 IBM Watsonx.ai \u5e73\u53f0\uff0c\u4ee5\u53ca\u5728\u5e73\u53f0\u4e0a\u4f7f\u7528 LLM \u5b8c\u6210\u5ba2\u6236\u9ad4\u9a57\u5206\u6790\u3001\u8207LLM\u4e32\u9023\u8655\u7406\u8f03\u8907\u96dc\u7684\u554f\u984c\u3002\u5728\u9019\u4e00\u7bc7\u4e2d\uff0c&hellip;<\/p>\n","protected":false},"author":9,"featured_media":5728,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[579,4],"tags":[26,562,40],"class_list":["post-5727","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-579","category-industry-news","tag-ai","tag-rag","tag-40"],"gutentor_comment":0,"jetpack_featured_media_url":"https:\/\/i0.wp.com\/aict.nkust.edu.tw\/digitrans\/wp-content\/uploads\/2024\/04\/%E8%9E%A2%E5%B9%95%E6%93%B7%E5%8F%96%E7%95%AB%E9%9D%A2-2024-04-09-140815.png?fit=906%2C876&ssl=1","jetpack-related-posts":[],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/aict.nkust.edu.tw\/digitrans\/index.php?rest_route=\/wp\/v2\/posts\/5727","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/aict.nkust.edu.tw\/digitrans\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/aict.nkust.edu.tw\/digitrans\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/aict.nkust.edu.tw\/digitrans\/index.php?rest_route=\/wp\/v2\/users\/9"}],"replies":[{"embeddable":true,"href":"https:\/\/aict.nkust.edu.tw\/digitrans\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5727"}],"version-history":[{"count":1,"href":"https:\/\/aict.nkust.edu.tw\/digitrans\/index.php?rest_route=\/wp\/v2\/posts\/5727\/revisions"}],"predecessor-version":[{"id":5729,"href":"https:\/\/aict.nkust.edu.tw\/digitrans\/index.php?rest_route=\/wp\/v2\/posts\/5727\/revisions\/5729"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/aict.nkust.edu.tw\/digitrans\/index.php?rest_route=\/wp\/v2\/media\/5728"}],"wp:attachment":[{"href":"https:\/\/aict.nkust.edu.tw\/digitrans\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5727"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/aict.nkust.edu.tw\/digitrans\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5727"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/aict.nkust.edu.tw\/digitrans\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5727"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}