{"id":1327,"date":"2024-08-30T18:21:54","date_gmt":"2024-08-30T10:21:54","guid":{"rendered":"https:\/\/www.fanyamin.com\/wordpress\/?p=1327"},"modified":"2024-08-30T18:21:54","modified_gmt":"2024-08-30T10:21:54","slug":"%e5%a6%82%e4%bd%95%e9%80%9a%e8%bf%87%e7%9f%a5%e8%af%86%e5%9b%be%e8%b0%b1%e5%af%b9%e5%a4%a7%e8%af%ad%e8%a8%80%e6%a8%a1%e5%9e%8b%e8%bf%94%e5%9b%9e%e7%9a%84%e7%bb%93%e6%9e%9c%e5%81%9a-rag","status":"publish","type":"post","link":"https:\/\/www.fanyamin.com\/wordpress\/?p=1327","title":{"rendered":"\u5982\u4f55\u901a\u8fc7\u77e5\u8bc6\u56fe\u8c31\u5bf9\u5927\u8bed\u8a00\u6a21\u578b\u8fd4\u56de\u7684\u7ed3\u679c\u505a RAG?"},"content":{"rendered":"<p>\u901a\u8fc7\u77e5\u8bc6\u56fe\u8c31\uff08Knowledge Graph, KG\uff09\u5bf9\u5927\u8bed\u8a00\u6a21\u578b\uff08LLM\uff09\u8fd4\u56de\u7684\u7ed3\u679c\u8fdb\u884c\u68c0\u7d22\u589e\u5f3a\u751f\u6210\uff08Retrieval-Augmented Generation, RAG\uff09\u53ef\u4ee5\u63d0\u9ad8\u751f\u6210\u5185\u5bb9\u7684\u51c6\u786e\u6027\u548c\u4e00\u81f4\u6027\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u6982\u8ff0\u5982\u4f55\u5b9e\u73b0\u8fd9\u4e00\u76ee\u6807\u7684\u6b65\u9aa4\uff1a<\/p>\n<h3>1. <strong>\u6784\u5efa\u6216\u4f7f\u7528\u73b0\u6709\u7684\u77e5\u8bc6\u56fe\u8c31<\/strong><\/h3>\n<ul>\n<li><strong>\u6570\u636e\u6765\u6e90<\/strong>: \u77e5\u8bc6\u56fe\u8c31\u901a\u5e38\u7531\u7ed3\u6784\u5316\u6570\u636e\uff08\u5982\u5173\u7cfb\u6570\u636e\u5e93\uff09\u3001\u534a\u7ed3\u6784\u5316\u6570\u636e\uff08\u5982JSON\u3001XML\uff09\u3001\u4ee5\u53ca\u975e\u7ed3\u6784\u5316\u6570\u636e\uff08\u5982\u6587\u6863\u3001\u7f51\u9875\uff09\u6784\u5efa\u800c\u6210\u3002<\/li>\n<li><strong>\u5b58\u50a8\u548c\u67e5\u8be2<\/strong>: \u53ef\u4ee5\u4f7f\u7528Neo4j\u3001Memgraph\u7b49\u56fe\u6570\u636e\u5e93\u6765\u5b58\u50a8\u77e5\u8bc6\u56fe\u8c31\uff0c\u5e76\u901a\u8fc7Cypher\u6216\u5176\u4ed6\u67e5\u8be2\u8bed\u8a00\u8fdb\u884c\u67e5\u8be2\u3002<\/li>\n<\/ul>\n<h3>2. <strong>\u7ed3\u5408\u77e5\u8bc6\u56fe\u8c31\u8fdb\u884c\u67e5\u8be2\u589e\u5f3a<\/strong><\/h3>\n<ul>\n<li><strong>\u67e5\u8be2\u6269\u5c55<\/strong>: \u5728\u7528\u6237\u53d1\u51fa\u67e5\u8be2\u540e\uff0c\u4f7f\u7528\u77e5\u8bc6\u56fe\u8c31\u5bf9\u67e5\u8be2\u8fdb\u884c\u6269\u5c55\u3002\u4f8b\u5982\uff0c\u5982\u679c\u7528\u6237\u8be2\u95ee\u67d0\u4e00\u7279\u5b9a\u9886\u57df\u7684\u95ee\u9898\uff0c\u53ef\u4ee5\u4f7f\u7528\u77e5\u8bc6\u56fe\u8c31\u6269\u5c55\u76f8\u5173\u9886\u57df\u7684\u6982\u5ff5\u548c\u5173\u7cfb\u3002<\/li>\n<li><strong>\u4e0a\u4e0b\u6587\u68c0\u7d22<\/strong>: \u5728\u751f\u6210\u7b54\u6848\u4e4b\u524d\uff0c\u4f7f\u7528\u77e5\u8bc6\u56fe\u8c31\u5bf9\u76f8\u5173\u4e0a\u4e0b\u6587\u4fe1\u606f\u8fdb\u884c\u68c0\u7d22\uff0c\u8fd9\u4e9b\u4fe1\u606f\u53ef\u4ee5\u5305\u62ec\u5b9e\u4f53\u4e4b\u95f4\u7684\u5173\u7cfb\u3001\u5c5e\u6027\u3001\u5386\u53f2\u8bb0\u5f55\u7b49\u3002<\/li>\n<\/ul>\n<h3>3. <strong>\u4e0e\u5927\u8bed\u8a00\u6a21\u578b\u7ed3\u5408<\/strong><\/h3>\n<ul>\n<li><strong>\u9884\u5904\u7406<\/strong>: \u5728\u5c06\u7528\u6237\u7684\u67e5\u8be2\u8f93\u5165\u5927\u8bed\u8a00\u6a21\u578b\u4e4b\u524d\uff0c\u5148\u7528\u77e5\u8bc6\u56fe\u8c31\u68c0\u7d22\u76f8\u5173\u7684\u4e0a\u4e0b\u6587\u4fe1\u606f\uff0c\u5e76\u5c06\u8fd9\u4e9b\u4fe1\u606f\u9644\u52a0\u5230\u67e5\u8be2\u4e2d\uff0c\u4ee5\u589e\u5f3a\u6a21\u578b\u7684\u7406\u89e3\u3002<\/li>\n<li><strong>\u751f\u6210\u4e0e\u9a8c\u8bc1<\/strong>: \u5927\u8bed\u8a00\u6a21\u578b\u751f\u6210\u521d\u6b65\u7b54\u6848\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u77e5\u8bc6\u56fe\u8c31\u5bf9\u751f\u6210\u7684\u5185\u5bb9\u8fdb\u884c\u9a8c\u8bc1\u548c\u4fee\u6b63\u3002\u4f8b\u5982\uff0c\u9a8c\u8bc1\u751f\u6210\u7684\u5b9e\u4f53\u5173\u7cfb\u662f\u5426\u6b63\u786e\uff0c\u6216\u5bf9\u5173\u952e\u4e8b\u5b9e\u8fdb\u884c\u4ea4\u53c9\u9a8c\u8bc1\u3002<\/li>\n<li><strong>\u540e\u5904\u7406<\/strong>: \u4f7f\u7528\u77e5\u8bc6\u56fe\u8c31\u8fdb\u884c\u540e\u5904\u7406\uff0c\u4ee5\u786e\u4fdd\u751f\u6210\u7684\u7ed3\u679c\u4e0e\u77e5\u8bc6\u56fe\u8c31\u4e2d\u7684\u5df2\u77e5\u4e8b\u5b9e\u4e00\u81f4\u3002<\/li>\n<\/ul>\n<h3>4. <strong>\u5b9e\u73b0\u6d41\u7a0b<\/strong><\/h3>\n<ul>\n<li><strong>\u7528\u6237\u67e5\u8be2<\/strong>: \u63a5\u6536\u5230\u7528\u6237\u67e5\u8be2\u540e\uff0c\u7cfb\u7edf\u9996\u5148\u901a\u8fc7\u77e5\u8bc6\u56fe\u8c31\u68c0\u7d22\u76f8\u5173\u5b9e\u4f53\u548c\u5173\u7cfb\u3002<\/li>\n<li><strong>\u68c0\u7d22\u589e\u5f3a<\/strong>: \u5c06\u68c0\u7d22\u5230\u7684\u4fe1\u606f\u4e0e\u7528\u6237\u67e5\u8be2\u4e00\u8d77\u4f20\u9012\u7ed9\u5927\u8bed\u8a00\u6a21\u578b\uff0c\u589e\u5f3a\u751f\u6210\u8fc7\u7a0b\u3002<\/li>\n<li><strong>\u7ed3\u679c\u9a8c\u8bc1<\/strong>: \u5bf9\u751f\u6210\u7684\u7ed3\u679c\u8fdb\u884c\u9a8c\u8bc1\uff0c\u786e\u4fdd\u5176\u4e0e\u77e5\u8bc6\u56fe\u8c31\u4e2d\u7684\u4fe1\u606f\u4e00\u81f4\u3002<\/li>\n<li><strong>\u7ed3\u679c\u8fd4\u56de<\/strong>: \u5c06\u7ecf\u8fc7\u9a8c\u8bc1\u7684\u7b54\u6848\u8fd4\u56de\u7ed9\u7528\u6237\u3002<\/li>\n<\/ul>\n<h3>5. <strong>\u6280\u672f\u5b9e\u73b0<\/strong><\/h3>\n<ul>\n<li><strong>Python\u4e0e\u56fe\u6570\u636e\u5e93<\/strong>: \u4f7f\u7528Python\u811a\u672c\u8fde\u63a5\u56fe\u6570\u636e\u5e93\uff08\u5982Neo4j\uff09\uff0c\u901a\u8fc7Cypher\u67e5\u8be2\u83b7\u53d6\u77e5\u8bc6\u56fe\u8c31\u4e2d\u7684\u4fe1\u606f\u3002<\/li>\n<li><strong>\u4e0e\u5927\u8bed\u8a00\u6a21\u578b\u96c6\u6210<\/strong>: \u4f7f\u7528Python\u5e93\uff08\u5982OpenAI\u7684API\uff09\u4e0e\u5927\u8bed\u8a00\u6a21\u578b\u4ea4\u4e92\uff0c\u5c06\u68c0\u7d22\u5230\u7684\u4e0a\u4e0b\u6587\u4fe1\u606f\u4f20\u9012\u7ed9\u6a21\u578b\uff0c\u5e76\u83b7\u53d6\u751f\u6210\u7684\u7b54\u6848\u3002<\/li>\n<\/ul>\n<h3>6. <strong>\u793a\u4f8b\u4ee3\u7801<\/strong><\/h3>\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684Python\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u7ed3\u5408Neo4j\u7684\u77e5\u8bc6\u56fe\u8c31\u4e0eOpenAI\u7684\u5927\u8bed\u8a00\u6a21\u578b\u8fdb\u884cRAG\uff1a<\/p>\n<pre><code class=\"language-python\">   from neo4j import GraphDatabase\n   import openai\n\n   # \u8fde\u63a5\u5230Neo4j\u6570\u636e\u5e93\n   uri = \"bolt:\/\/localhost:7687\"\n   driver = GraphDatabase.driver(uri, auth=(\"neo4j\", \"password\"))\n\n   def retrieve_kg_context(query):\n       with driver.session() as session:\n           result = session.run(query)\n           context = [record[\"context\"] for record in result]\n           return \" \".join(context)\n\n   def generate_response_with_context(prompt, context):\n       combined_prompt = f\"{context}\\n\\n{prompt}\"\n       response = openai.Completion.create(\n           engine=\"text-davinci-003\",\n           prompt=combined_prompt,\n           max_tokens=150\n       )\n       return response.choices[0].text.strip()\n\n   # \u793a\u4f8b\u67e5\u8be2\n   kg_query = \"MATCH (n:Entity)-[r:RELATION]->(m) WHERE n.name='example' RETURN m.name AS context\"\n   context = retrieve_kg_context(kg_query)\n\n   # \u7528\u6237\u8f93\u5165\n   user_query = \"Explain the relationship between example and its connected entities.\"\n\n   # \u751f\u6210\u7ed3\u679c\n   response = generate_response_with_context(user_query, context)\n   print(response)<\/code><\/pre>\n<p>\u901a\u8fc7\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u77e5\u8bc6\u56fe\u8c31\u53ef\u4ee5\u663e\u8457\u589e\u5f3a\u5927\u8bed\u8a00\u6a21\u578b\u7684\u751f\u6210\u80fd\u529b\uff0c\u4f7f\u5176\u8f93\u51fa\u66f4\u52a0\u7cbe\u786e\u548c\u6709\u610f\u4e49\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u901a\u8fc7\u77e5\u8bc6\u56fe\u8c31\uff08Knowledge Graph, KG\uff09\u5bf9\u5927\u8bed\u8a00\u6a21\u578b\uff08LLM\uff09\u8fd4\u56de\u7684\u7ed3\u679c\u8fdb\u884c\u68c0\u7d22\u589e\u5f3a\u751f\u6210\uff08Retrieval-Augmented Generation, RAG\uff09\u53ef\u4ee5\u63d0\u9ad8\u751f\u6210\u5185\u5bb9\u7684\u51c6\u786e\u6027\u548c\u4e00\u81f4\u6027\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u6982\u8ff0\u5982\u4f55\u5b9e\u73b0\u8fd9\u4e00\u76ee\u6807\u7684\u6b65\u9aa4\uff1a 1. \u6784\u5efa\u6216\u4f7f\u7528\u73b0\u6709\u7684\u77e5\u8bc6\u56fe\u8c31 \u6570\u636e\u6765\u6e90: \u77e5\u8bc6\u56fe\u8c31\u901a\u5e38\u7531\u7ed3\u6784\u5316\u6570\u636e\uff08\u5982\u5173\u7cfb\u6570\u636e\u5e93\uff09\u3001\u534a\u7ed3\u6784\u5316\u6570\u636e\uff08\u5982JSON\u3001XML\uff09\u3001\u4ee5\u53ca\u975e\u7ed3\u6784\u5316\u6570\u636e\uff08\u5982\u6587\u6863\u3001\u7f51\u9875\uff09\u6784\u5efa\u800c\u6210\u3002 \u5b58\u50a8\u548c\u67e5\u8be2: \u53ef\u4ee5\u4f7f\u7528Neo4j\u3001Memgraph\u7b49\u56fe\u6570\u636e\u5e93\u6765\u5b58\u50a8\u77e5\u8bc6\u56fe\u8c31\uff0c\u5e76\u901a\u8fc7Cypher\u6216\u5176\u4ed6\u67e5\u8be2\u8bed\u8a00\u8fdb\u884c\u67e5\u8be2\u3002 2. \u7ed3\u5408\u77e5\u8bc6\u56fe\u8c31\u8fdb\u884c\u67e5\u8be2\u589e\u5f3a \u67e5\u8be2\u6269\u5c55: \u5728\u7528\u6237\u53d1\u51fa\u67e5\u8be2\u540e\uff0c\u4f7f\u7528\u77e5\u8bc6\u56fe\u8c31\u5bf9\u67e5\u8be2\u8fdb\u884c\u6269\u5c55\u3002\u4f8b\u5982\uff0c\u5982\u679c\u7528\u6237\u8be2\u95ee\u67d0\u4e00\u7279\u5b9a\u9886\u57df\u7684\u95ee\u9898\uff0c\u53ef\u4ee5\u4f7f\u7528\u77e5\u8bc6\u56fe\u8c31\u6269\u5c55\u76f8\u5173\u9886\u57df\u7684\u6982\u5ff5\u548c\u5173\u7cfb\u3002 \u4e0a\u4e0b\u6587\u68c0\u7d22: \u5728\u751f\u6210\u7b54\u6848\u4e4b\u524d\uff0c\u4f7f\u7528\u77e5\u8bc6\u56fe\u8c31\u5bf9\u76f8\u5173\u4e0a\u4e0b\u6587\u4fe1\u606f\u8fdb\u884c\u68c0\u7d22\uff0c\u8fd9\u4e9b\u4fe1\u606f\u53ef\u4ee5\u5305\u62ec\u5b9e\u4f53\u4e4b\u95f4\u7684\u5173\u7cfb\u3001\u5c5e\u6027\u3001\u5386\u53f2\u8bb0\u5f55\u7b49\u3002 3. \u4e0e\u5927\u8bed\u8a00\u6a21\u578b\u7ed3\u5408 \u9884\u5904\u7406: \u5728\u5c06\u7528\u6237\u7684\u67e5\u8be2\u8f93\u5165\u5927\u8bed\u8a00\u6a21\u578b\u4e4b\u524d\uff0c\u5148\u7528\u77e5\u8bc6\u56fe\u8c31\u68c0\u7d22\u76f8\u5173\u7684\u4e0a\u4e0b\u6587\u4fe1\u606f\uff0c\u5e76\u5c06\u8fd9\u4e9b\u4fe1\u606f\u9644\u52a0\u5230\u67e5\u8be2\u4e2d\uff0c\u4ee5\u589e\u5f3a\u6a21\u578b\u7684\u7406\u89e3\u3002 \u751f\u6210\u4e0e\u9a8c\u8bc1: \u5927\u8bed\u8a00\u6a21\u578b\u751f\u6210\u521d\u6b65\u7b54\u6848\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u77e5\u8bc6\u56fe\u8c31\u5bf9\u751f\u6210\u7684\u5185\u5bb9\u8fdb\u884c\u9a8c\u8bc1\u548c\u4fee\u6b63\u3002\u4f8b\u5982\uff0c\u9a8c\u8bc1\u751f\u6210\u7684\u5b9e\u4f53\u5173\u7cfb\u662f\u5426\u6b63\u786e\uff0c\u6216\u5bf9\u5173\u952e\u4e8b\u5b9e\u8fdb\u884c\u4ea4\u53c9\u9a8c\u8bc1\u3002 \u540e\u5904\u7406: \u4f7f\u7528\u77e5\u8bc6\u56fe\u8c31\u8fdb\u884c\u540e\u5904\u7406\uff0c\u4ee5\u786e\u4fdd\u751f\u6210\u7684\u7ed3\u679c\u4e0e\u77e5\u8bc6\u56fe\u8c31\u4e2d\u7684\u5df2\u77e5\u4e8b\u5b9e\u4e00\u81f4\u3002 4. \u5b9e\u73b0\u6d41\u7a0b \u7528\u6237\u67e5\u8be2: \u63a5\u6536\u5230\u7528\u6237\u67e5\u8be2\u540e\uff0c\u7cfb\u7edf\u9996\u5148\u901a\u8fc7\u77e5\u8bc6\u56fe\u8c31\u68c0\u7d22\u76f8\u5173\u5b9e\u4f53\u548c\u5173\u7cfb\u3002 \u68c0\u7d22\u589e\u5f3a: \u5c06\u68c0\u7d22\u5230\u7684\u4fe1\u606f\u4e0e\u7528\u6237\u67e5\u8be2\u4e00\u8d77\u4f20\u9012\u7ed9\u5927\u8bed\u8a00\u6a21\u578b\uff0c\u589e\u5f3a\u751f\u6210\u8fc7\u7a0b\u3002 \u7ed3\u679c\u9a8c\u8bc1: \u5bf9\u751f\u6210\u7684\u7ed3\u679c\u8fdb\u884c\u9a8c\u8bc1\uff0c\u786e\u4fdd\u5176\u4e0e\u77e5\u8bc6\u56fe\u8c31\u4e2d\u7684\u4fe1\u606f\u4e00\u81f4\u3002 \u7ed3\u679c\u8fd4\u56de: \u5c06\u7ecf\u8fc7\u9a8c\u8bc1\u7684\u7b54\u6848\u8fd4\u56de\u7ed9\u7528\u6237\u3002 5. \u6280\u672f\u5b9e\u73b0 Python\u4e0e\u56fe\u6570\u636e\u5e93: \u4f7f\u7528Python\u811a\u672c\u8fde\u63a5\u56fe\u6570\u636e\u5e93\uff08\u5982Neo4j\uff09\uff0c\u901a\u8fc7Cypher\u67e5\u8be2\u83b7\u53d6\u77e5\u8bc6\u56fe\u8c31\u4e2d\u7684\u4fe1\u606f\u3002 \u4e0e\u5927\u8bed\u8a00\u6a21\u578b\u96c6\u6210: \u4f7f\u7528Python\u5e93\uff08\u5982OpenAI\u7684API\uff09\u4e0e\u5927\u8bed\u8a00\u6a21\u578b\u4ea4\u4e92\uff0c\u5c06\u68c0\u7d22\u5230\u7684\u4e0a\u4e0b\u6587\u4fe1\u606f\u4f20\u9012\u7ed9\u6a21\u578b\uff0c\u5e76\u83b7\u53d6\u751f\u6210\u7684\u7b54\u6848\u3002 6. \u793a\u4f8b\u4ee3\u7801 \u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684Python\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u7ed3\u5408Neo4j\u7684\u77e5\u8bc6\u56fe\u8c31\u4e0eOpenAI\u7684\u5927\u8bed\u8a00\u6a21\u578b\u8fdb\u884cRAG\uff1a from neo4j import GraphDatabase import openai # \u8fde\u63a5\u5230Neo4j\u6570\u636e\u5e93 uri = &#8220;bolt:\/\/localhost:7687&#8221; [&hellip;] <a class=\"read-more\" href=\"https:\/\/www.fanyamin.com\/wordpress\/?p=1327\" title=\"Permanent Link to: \u5982\u4f55\u901a\u8fc7\u77e5\u8bc6\u56fe\u8c31\u5bf9\u5927\u8bed\u8a00\u6a21\u578b\u8fd4\u56de\u7684\u7ed3\u679c\u505a RAG?\">&rarr;Read&nbsp;more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-1327","post","type-post","status-publish","format-standard","hentry","category-5"],"_links":{"self":[{"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/1327"}],"collection":[{"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1327"}],"version-history":[{"count":1,"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/1327\/revisions"}],"predecessor-version":[{"id":1328,"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/1327\/revisions\/1328"}],"wp:attachment":[{"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1327"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1327"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1327"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}