使用OpenAI评估RAG响应准确性:文档基础性指标

高级

这是一个Engineering、AI领域的自动化工作流,包含 25 个节点。主要使用 Set、Evaluation、HttpRequest、ManualTrigger、Agent 等节点,结合人工智能技术实现智能自动化。 使用OpenAI评估RAG响应准确性:文档基础性指标

前置要求
  • 可能需要目标 API 的认证凭证
  • OpenAI API Key
工作流预览
可视化展示节点连接关系,支持缩放和平移
导出工作流
复制以下 JSON 配置到 n8n 导入,即可使用此工作流
{
  "meta": {
    "instanceId": "408f9fb9940c3cb18ffdef0e0150fe342d6e655c3a9fac21f0f644e8bedabcd9",
    "templateCredsSetupCompleted": true
  },
  "nodes": [
    {
      "id": "976ded9c-2080-484e-ad64-eb3c6e671961",
      "name": "当点击\"执行工作流\"时",
      "type": "n8n-nodes-base.manualTrigger",
      "position": [
        -760,
        -320
      ],
      "parameters": {},
      "typeVersion": 1
    },
    {
      "id": "348dfa43-be32-48a8-a9e1-a0641852517e",
      "name": "获取数据表",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        -540,
        -320
      ],
      "parameters": {
        "url": "https://bitcoin.org/bitcoin.pdf",
        "options": {}
      },
      "typeVersion": 4.2
    },
    {
      "id": "e4854cab-8bc9-4e63-911f-a9a4f5993855",
      "name": "简单向量存储",
      "type": "@n8n/n8n-nodes-langchain.vectorStoreInMemory",
      "position": [
        -300,
        -320
      ],
      "parameters": {
        "mode": "insert",
        "memoryKey": "evaluations_document_groundness",
        "clearStore": true
      },
      "typeVersion": 1.1
    },
    {
      "id": "f69492e2-c85a-451c-9e02-f5902f214da6",
      "name": "OpenAI 嵌入",
      "type": "@n8n/n8n-nodes-langchain.embeddingsOpenAi",
      "position": [
        -320,
        -100
      ],
      "parameters": {
        "options": {}
      },
      "credentials": {
        "openAiApi": {
          "id": "8gccIjcuf3gvaoEr",
          "name": "OpenAi account"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "65131cfb-ac44-48fa-a648-033c0611e8ea",
      "name": "默认数据加载器",
      "type": "@n8n/n8n-nodes-langchain.documentDefaultDataLoader",
      "position": [
        -200,
        -100
      ],
      "parameters": {
        "options": {
          "metadata": {
            "metadataValues": [
              {
                "name": "origin",
                "value": "evaluations"
              }
            ]
          }
        },
        "dataType": "binary"
      },
      "typeVersion": 1
    },
    {
      "id": "0800667d-7b33-4c8b-bc61-d12ccf94a640",
      "name": "递归字符文本分割器",
      "type": "@n8n/n8n-nodes-langchain.textSplitterRecursiveCharacterTextSplitter",
      "position": [
        -120,
        100
      ],
      "parameters": {
        "options": {}
      },
      "typeVersion": 1
    },
    {
      "id": "04a8a558-07cf-41f4-85a9-37e79be90b52",
      "name": "AI Agent",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "position": [
        -300,
        620
      ],
      "parameters": {
        "options": {
          "systemMessage": "You are a helpful reference assistant who fully grounds their answer using retrieved knowledgebase document. It is important not to give any information from other sources as the goal is to be faithful to the contents of the knowledgebase.",
          "returnIntermediateSteps": true
        }
      },
      "typeVersion": 2
    },
    {
      "id": "6f6961f7-73c3-4e16-942e-e2da096fec94",
      "name": "OpenAI 聊天模型",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "position": [
        -320,
        845
      ],
      "parameters": {
        "model": {
          "__rl": true,
          "mode": "list",
          "value": "gpt-4o-mini"
        },
        "options": {}
      },
      "credentials": {
        "openAiApi": {
          "id": "8gccIjcuf3gvaoEr",
          "name": "OpenAi account"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "a063f080-9227-471f-8330-3da574e275d1",
      "name": "简单向量存储1",
      "type": "@n8n/n8n-nodes-langchain.vectorStoreInMemory",
      "position": [
        -200,
        847.5
      ],
      "parameters": {
        "mode": "retrieve-as-tool",
        "toolName": "bitcoin_whitepaper",
        "memoryKey": "evaluations_document_groundness",
        "toolDescription": "Call this tool to query over the bitcoin whitepaper to answer technical questions about bitcoin."
      },
      "typeVersion": 1.1
    },
    {
      "id": "85584b59-0970-4bd9-ac96-f31a0046f20e",
      "name": "Embeddings OpenAI1",
      "type": "@n8n/n8n-nodes-langchain.embeddingsOpenAi",
      "position": [
        -120,
        980
      ],
      "parameters": {
        "options": {}
      },
      "credentials": {
        "openAiApi": {
          "id": "8gccIjcuf3gvaoEr",
          "name": "OpenAi account"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "a99b3732-f8ac-45d3-a2f5-7a1177dc5742",
      "name": "当收到聊天消息时",
      "type": "@n8n/n8n-nodes-langchain.chatTrigger",
      "position": [
        -540,
        725
      ],
      "webhookId": "638886a2-37cc-4edd-9b9f-95b09b255ffb",
      "parameters": {
        "options": {}
      },
      "typeVersion": 1.1
    },
    {
      "id": "c21ee596-bc9c-4bb3-8cac-f243a2d5102b",
      "name": "评估",
      "type": "n8n-nodes-base.evaluation",
      "position": [
        240,
        620
      ],
      "parameters": {
        "operation": "checkIfEvaluating"
      },
      "typeVersion": 4.6
    },
    {
      "id": "64104870-1796-4fcc-8c0d-cc4302de5167",
      "name": "无操作,不执行任何操作",
      "type": "n8n-nodes-base.noOp",
      "position": [
        460,
        720
      ],
      "parameters": {},
      "typeVersion": 1
    },
    {
      "id": "91e0a42e-6afc-463b-8f25-7117b057159d",
      "name": "获取文档",
      "type": "n8n-nodes-base.set",
      "position": [
        460,
        480
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "a1e75813-76a9-49ba-a8c1-1f2cd62d647c",
              "name": "output",
              "type": "string",
              "value": "={{ $json.output }}"
            },
            {
              "id": "94f08596-6060-4fb6-9998-ac1b3f0e730d",
              "name": "documents",
              "type": "array",
              "value": "={{\n$json.intermediateSteps\n  .find(step => step.action.tool === 'bitcoin_whitepaper')\n  .observation\n}}"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "8fba17b7-c24b-4a26-a998-e3d2f7acc481",
      "name": "当获取数据集行时",
      "type": "n8n-nodes-base.evaluationTrigger",
      "position": [
        -760,
        525
      ],
      "parameters": {
        "sheetName": {
          "__rl": true,
          "mode": "list",
          "value": 633230352,
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1YOnu2JJjlxd787AuYcg-wKbkjyjyZFgASYVV0jsij5Y/edit#gid=633230352",
          "cachedResultName": "RAG Document Groundedness"
        },
        "documentId": {
          "__rl": true,
          "mode": "list",
          "value": "1YOnu2JJjlxd787AuYcg-wKbkjyjyZFgASYVV0jsij5Y",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1YOnu2JJjlxd787AuYcg-wKbkjyjyZFgASYVV0jsij5Y/edit?usp=drivesdk",
          "cachedResultName": "96. Evaluations Test"
        }
      },
      "credentials": {
        "googleSheetsOAuth2Api": {
          "id": "XHvC7jIRR8A2TlUl",
          "name": "Google Sheets account"
        }
      },
      "typeVersion": 4.6
    },
    {
      "id": "611a6e12-f52b-4b3c-b74c-238a4f75f612",
      "name": "重新映射输入",
      "type": "n8n-nodes-base.set",
      "position": [
        -540,
        525
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "cc14c8c8-5a61-46db-b1c7-6d9ddfb2e4c8",
              "name": "chatInput",
              "type": "string",
              "value": "={{ $json.input }}"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "0c1e25b8-9145-443e-8c9f-34eb62aa7192",
      "name": "OpenAI 聊天模型1",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "position": [
        660,
        660
      ],
      "parameters": {
        "model": {
          "__rl": true,
          "mode": "list",
          "value": "gpt-4.1-mini",
          "cachedResultName": "gpt-4.1-mini"
        },
        "options": {}
      },
      "credentials": {
        "openAiApi": {
          "id": "8gccIjcuf3gvaoEr",
          "name": "OpenAi account"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "a9d8a911-9519-4497-9127-0a2e3da9ae1b",
      "name": "结构化输出解析器",
      "type": "@n8n/n8n-nodes-langchain.outputParserStructured",
      "position": [
        860,
        660
      ],
      "parameters": {
        "jsonSchemaExample": "{\n  \"rating\": 1,\n  \"reason\": \"The date of birth of Einstein is mentioned clearly in the context.\"\n}"
      },
      "typeVersion": 1.2
    },
    {
      "id": "038f408d-51f0-4625-b39d-e326f24850b4",
      "name": "文档基础性",
      "type": "@n8n/n8n-nodes-langchain.chainLlm",
      "position": [
        660,
        480
      ],
      "parameters": {
        "text": "=# Document and AI-generated Response\n## Documents\n{{ $json.documents\n  .map(doc => `* ${doc.text.parseJson().pageContent.replaceAll('\\n', ' ')}`)\n  .join('\\n')\n}}\n\n## AI-generated Response\n{{ $('Get Documents').first().json.output }}",
        "batching": {},
        "messages": {
          "messageValues": [
            {
              "message": "=# Instruction\nYou are an expert evaluator. Your task is to evaluate the quality of the responses generated by AI models.\nWe will provide you with the user input and an AI-generated responses.\nYou should first read the user input carefully for analyzing the task, and then evaluate the quality of the responses based on the criteria provided in the Evaluation section below.\nYou will assign the response a rating following the Rating Rubric and Evaluation Steps. Give step-by-step explanations for your rating, and only choose ratings from the Rating Rubric.\n\n# Evaluation\n## Metric Definition\nYou will be assessing groundedness, which measures the ability to provide or reference information included only in the user prompt.\n\n## Criteria\nGroundedness: The response contains information included only in the document. The response does not reference any outside information.\n\n## Rating Rubric\n1: (Fully grounded). All aspects of the response are attributable to the context.\n0: (Not fully grounded). The entire response or a portion of the response is not attributable to the context provided by the documents.\n\n## Evaluation Steps\nSTEP 1: Assess the response in aspects of Groundedness. Identify any information in the response not present in the documents and provide assessment according to the criterion. \nSTEP 2: Score based on the rating rubric. Give a brief rationale to explain your evaluation considering Groundedness."
            }
          ]
        },
        "promptType": "define",
        "hasOutputParser": true
      },
      "typeVersion": 1.7
    },
    {
      "id": "e867ed3e-df63-40e2-a3f6-0d19bbace13a",
      "name": "设置输出",
      "type": "n8n-nodes-base.evaluation",
      "position": [
        1020,
        480
      ],
      "parameters": {
        "outputs": {
          "values": [
            {
              "outputName": "output",
              "outputValue": "={{ $('Get Documents').item.json.output }}"
            },
            {
              "outputName": "documents",
              "outputValue": "={{ $('Get Documents').item.json.documents.map(doc => doc.text).join('\\n') }}"
            },
            {
              "outputName": "score",
              "outputValue": "={{ $json.output.rating }}"
            },
            {
              "outputName": "score_reason",
              "outputValue": "={{ $json.output.reason }}"
            }
          ]
        },
        "sheetName": {
          "__rl": true,
          "mode": "list",
          "value": 633230352,
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1YOnu2JJjlxd787AuYcg-wKbkjyjyZFgASYVV0jsij5Y/edit#gid=633230352",
          "cachedResultName": "RAG Document Groundedness"
        },
        "documentId": {
          "__rl": true,
          "mode": "list",
          "value": "1YOnu2JJjlxd787AuYcg-wKbkjyjyZFgASYVV0jsij5Y",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1YOnu2JJjlxd787AuYcg-wKbkjyjyZFgASYVV0jsij5Y/edit?usp=drivesdk",
          "cachedResultName": "96. Evaluations Test"
        }
      },
      "credentials": {
        "googleSheetsOAuth2Api": {
          "id": "XHvC7jIRR8A2TlUl",
          "name": "Google Sheets account"
        }
      },
      "typeVersion": 4.6
    },
    {
      "id": "7350d6c6-1a9d-4663-b509-de1c9d73588a",
      "name": "设置指标",
      "type": "n8n-nodes-base.evaluation",
      "position": [
        1220,
        480
      ],
      "parameters": {
        "metrics": {
          "assignments": [
            {
              "id": "e3f04944-ab59-4e2f-83f8-6efa36816671",
              "name": "score",
              "type": "number",
              "value": "={{ $json.output.rating }}"
            }
          ]
        },
        "operation": "setMetrics"
      },
      "typeVersion": 4.6
    },
    {
      "id": "e5313dae-6eff-4b71-83b1-0c89ff319662",
      "name": "便签",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -840,
        -500
      ],
      "parameters": {
        "color": 7,
        "width": 960,
        "height": 780,
        "content": "## 1. 准备好您的 RAG 向量存储"
      },
      "typeVersion": 1
    },
    {
      "id": "36da4904-2256-4d4a-9a1d-17ad3af939d4",
      "name": "便签1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -840,
        300
      ],
      "parameters": {
        "color": 7,
        "width": 960,
        "height": 900,
        "content": "## 2. 设置您的 AI 工作流以使用评估"
      },
      "typeVersion": 1
    },
    {
      "id": "d892b15f-126d-4fac-8458-167a9f4bf1b1",
      "name": "便签2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        160,
        300
      ],
      "parameters": {
        "color": 7,
        "width": 1260,
        "height": 640,
        "content": "## 3. 文档基础性:AI 响应是否基于检索到的文档?"
      },
      "typeVersion": 1
    },
    {
      "id": "c500b17e-0c27-4121-9f89-40531b83f48e",
      "name": "便签3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1280,
        -500
      ],
      "parameters": {
        "width": 380,
        "height": 880,
        "content": "## 试试看!"
      },
      "typeVersion": 1
    }
  ],
  "pinData": {},
  "connections": {
    "AI Agent": {
      "main": [
        [
          {
            "node": "Evaluation",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Evaluation": {
      "main": [
        [
          {
            "node": "Get Documents",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "No Operation, do nothing",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Remap Input": {
      "main": [
        [
          {
            "node": "AI Agent",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set Outputs": {
      "main": [
        [
          {
            "node": "Set Metrics",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Datasheet": {
      "main": [
        [
          {
            "node": "Simple Vector Store",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Documents": {
      "main": [
        [
          {
            "node": "Document Grounding",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Embeddings OpenAI": {
      "ai_embedding": [
        [
          {
            "node": "Simple Vector Store",
            "type": "ai_embedding",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "AI Agent",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Document Grounding": {
      "main": [
        [
          {
            "node": "Set Outputs",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Embeddings OpenAI1": {
      "ai_embedding": [
        [
          {
            "node": "Simple Vector Store1",
            "type": "ai_embedding",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI Chat Model1": {
      "ai_languageModel": [
        [
          {
            "node": "Document Grounding",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Default Data Loader": {
      "ai_document": [
        [
          {
            "node": "Simple Vector Store",
            "type": "ai_document",
            "index": 0
          }
        ]
      ]
    },
    "Simple Vector Store": {
      "main": [
        []
      ]
    },
    "Simple Vector Store1": {
      "ai_tool": [
        [
          {
            "node": "AI Agent",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "Structured Output Parser": {
      "ai_outputParser": [
        [
          {
            "node": "Document Grounding",
            "type": "ai_outputParser",
            "index": 0
          }
        ]
      ]
    },
    "When chat message received": {
      "main": [
        [
          {
            "node": "AI Agent",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "When fetching a dataset row": {
      "main": [
        [
          {
            "node": "Remap Input",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Recursive Character Text Splitter": {
      "ai_textSplitter": [
        [
          {
            "node": "Default Data Loader",
            "type": "ai_textSplitter",
            "index": 0
          }
        ]
      ]
    },
    "When clicking ‘Execute workflow’": {
      "main": [
        [
          {
            "node": "Get Datasheet",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
常见问题

如何使用这个工作流?

复制上方的 JSON 配置代码,在您的 n8n 实例中创建新工作流并选择「从 JSON 导入」,粘贴配置后根据需要修改凭证设置即可。

这个工作流适合什么场景?

这是一个高级难度的工作流,适用于Engineering、AI等场景。适合高级用户,包含 16+ 个节点的复杂工作流

需要付费吗?

本工作流完全免费,您可以直接导入使用。但请注意,工作流中使用的第三方服务(如 OpenAI API)可能需要您自行付费。

工作流信息
难度等级
高级
节点数量25
分类2
节点类型16
难度说明

适合高级用户,包含 16+ 个节点的复杂工作流

作者
Jimleuk

Jimleuk

@jimleuk

Freelance consultant based in the UK specialising in AI-powered automations. I work with select clients tackling their most challenging projects. For business enquiries, send me an email at hello@jimle.uk LinkedIn: https://www.linkedin.com/in/jimleuk/ X/Twitter: https://x.com/jimle_uk

外部链接
在 n8n.io 上查看 →

分享此工作流