使用OpenAI和余弦相似度评估AI代理响应相关性
高级
这是一个Engineering, AI领域的自动化工作流,包含 20 个节点。主要使用 Set, Code, Evaluation, HttpRequest, Agent 等节点,结合人工智能技术实现智能自动化。 使用OpenAI和余弦相似度评估AI代理响应相关性
前置要求
- •可能需要目标 API 的认证凭证
- •OpenAI API Key
使用的节点 (20)
工作流预览
可视化展示节点连接关系,支持缩放和平移
导出工作流
复制以下 JSON 配置到 n8n 导入,即可使用此工作流
{
"meta": {
"instanceId": "408f9fb9940c3cb18ffdef0e0150fe342d6e655c3a9fac21f0f644e8bedabcd9",
"templateCredsSetupCompleted": true
},
"nodes": [
{
"id": "49b274c7-1ff2-43ba-acb9-349a8665b513",
"name": "OpenAI 聊天模型1",
"type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
"position": [
148,
180
],
"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": "77f0c130-085f-4fa5-b74e-66fd084949b7",
"name": "当获取数据集行时",
"type": "n8n-nodes-base.evaluationTrigger",
"position": [
-380,
-140
],
"parameters": {
"sheetName": {
"__rl": true,
"mode": "list",
"value": 2015506025,
"cachedResultUrl": "https://docs.google.com/spreadsheets/d/1YOnu2JJjlxd787AuYcg-wKbkjyjyZFgASYVV0jsij5Y/edit#gid=2015506025",
"cachedResultName": "Relevance"
},
"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": "63d8ff44-5c4b-4cf6-b5b2-1f22de0de495",
"name": "重新映射输入",
"type": "n8n-nodes-base.set",
"position": [
-160,
-140
],
"parameters": {
"options": {},
"assignments": {
"assignments": [
{
"id": "00924b90-278f-49f5-80f2-c297df0fcc97",
"name": "chatInput",
"type": "string",
"value": "={{ $json.input }}"
}
]
}
},
"typeVersion": 3.4
},
{
"id": "d8ac1b2b-f4ee-4af1-92f5-d194832c5c76",
"name": "评估",
"type": "n8n-nodes-base.evaluation",
"position": [
436,
-40
],
"parameters": {
"operation": "checkIfEvaluating"
},
"typeVersion": 4.6
},
{
"id": "1d8d9c97-0c90-4878-95fa-0dc6d3ad82c6",
"name": "设置输入字段",
"type": "n8n-nodes-base.set",
"position": [
656,
-140
],
"parameters": {
"options": {},
"assignments": {
"assignments": [
{
"id": "d58952c1-d346-4fbf-881e-d5c04b6781a5",
"name": "question",
"type": "string",
"value": "={{ $('When fetching a dataset row').first().json.input }}"
},
{
"id": "0f10a3d0-cf6e-4715-9ded-2cee54aa62ec",
"name": "answer",
"type": "string",
"value": "={{ $json.output }}"
}
]
}
},
"typeVersion": 3.4
},
{
"id": "d4ad7c5e-f1cf-463b-b9b3-a45ff5597199",
"name": "无操作,不执行任何操作",
"type": "n8n-nodes-base.noOp",
"position": [
656,
60
],
"parameters": {},
"typeVersion": 1
},
{
"id": "1aea189f-3e7d-45d2-9802-d44b1dce4797",
"name": "AI Agent",
"type": "@n8n/n8n-nodes-langchain.agent",
"position": [
60,
-40
],
"parameters": {
"options": {}
},
"typeVersion": 2
},
{
"id": "54596db1-4ba7-414c-88cf-250f528d6c21",
"name": "当收到聊天消息时",
"type": "@n8n/n8n-nodes-langchain.chatTrigger",
"position": [
-160,
60
],
"webhookId": "ba1fadeb-b566-469a-97b3-3159a99f1805",
"parameters": {
"options": {}
},
"typeVersion": 1.1
},
{
"id": "4184f202-48f5-4e58-a0e8-0227a23553f3",
"name": "更新输出",
"type": "n8n-nodes-base.evaluation",
"position": [
2132,
-140
],
"parameters": {
"outputs": {
"values": [
{
"outputName": "output",
"outputValue": "={{ $('Set Input Fields').first().json.answer }}"
},
{
"outputName": "score",
"outputValue": "={{ $json.score }}"
}
]
},
"sheetName": {
"__rl": true,
"mode": "list",
"value": 2015506025,
"cachedResultUrl": "https://docs.google.com/spreadsheets/d/1YOnu2JJjlxd787AuYcg-wKbkjyjyZFgASYVV0jsij5Y/edit#gid=2015506025",
"cachedResultName": "Relevance"
},
"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": "fe7b41d2-e382-4f59-8b0c-06361062581d",
"name": "更新指标",
"type": "n8n-nodes-base.evaluation",
"position": [
2352,
-140
],
"parameters": {
"metrics": {
"assignments": [
{
"id": "1fd7759c-f4ef-4eda-87ad-9d9563b63e99",
"name": "score",
"type": "number",
"value": "={{ $json.score }}"
}
]
},
"operation": "setMetrics"
},
"typeVersion": 4.6
},
{
"id": "04f714cd-99ce-45a9-bc43-351fb6ea14d4",
"name": "OpenAI 聊天模型",
"type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
"position": [
920,
80
],
"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": "d821b0ec-93c9-46e1-ab03-280aeb9b87b1",
"name": "结构化输出解析器",
"type": "@n8n/n8n-nodes-langchain.outputParserStructured",
"position": [
1040,
80
],
"parameters": {
"jsonSchemaExample": "{\n \"question\": \"Where was Albert Einstein born?\",\n \"noncommittal\": 0\n}"
},
"typeVersion": 1.2
},
{
"id": "be975ef4-c50b-4bf9-b871-cc8d479661eb",
"name": "答案相关性",
"type": "@n8n/n8n-nodes-langchain.chainLlm",
"position": [
900,
-140
],
"parameters": {
"text": "={{ $json.answer }}",
"batching": {},
"messages": {
"messageValues": [
{
"message": "=Generate a question for the given answer and Identify if answer is noncommittal. Give noncommittal as 1 if the answer is noncommittal and 0 if the answer is committal. A noncommittal answer is one that is evasive, vague, or ambiguous. For example, \"I don't know\" or \"I'm not sure\" are noncommittal answers\""
}
]
},
"promptType": "define",
"hasOutputParser": true
},
"typeVersion": 1.7
},
{
"id": "8e3df14a-7754-4257-8209-a24cfa30dbbc",
"name": "便签1",
"type": "n8n-nodes-base.stickyNote",
"position": [
-452,
-400
],
"parameters": {
"color": 7,
"width": 840,
"height": 720,
"content": "## 1. 设置您的 AI 工作流以使用评估"
},
"typeVersion": 1
},
{
"id": "eeeac9d0-39c6-4d17-8d93-9c60b6c612f8",
"name": "便签",
"type": "n8n-nodes-base.stickyNote",
"position": [
820,
-400
],
"parameters": {
"color": 7,
"width": 1200,
"height": 720,
"content": "## 2. 答案相关性:代理响应与用户问题的相关性如何?"
},
"typeVersion": 1
},
{
"id": "c86f66e2-9997-4ca6-9ec6-cfa185d404f2",
"name": "便签3",
"type": "n8n-nodes-base.stickyNote",
"position": [
-920,
-340
],
"parameters": {
"width": 420,
"height": 720,
"content": "## 试试看!"
},
"typeVersion": 1
},
{
"id": "3bdd3254-7cdf-455a-ae63-9639069993bc",
"name": "获取嵌入",
"type": "n8n-nodes-base.httpRequest",
"position": [
1460,
-140
],
"parameters": {
"url": "https://api.openai.com/v1/embeddings",
"method": "POST",
"options": {},
"sendBody": true,
"authentication": "predefinedCredentialType",
"bodyParameters": {
"parameters": [
{
"name": "input",
"value": "={{ $json.data }}"
},
{
"name": "model",
"value": "text-embedding-3-small"
},
{
"name": "encoding_format",
"value": "float"
}
]
},
"nodeCredentialType": "openAiApi"
},
"credentials": {
"openAiApi": {
"id": "8gccIjcuf3gvaoEr",
"name": "OpenAi account"
}
},
"typeVersion": 4.2
},
{
"id": "9e3bcdee-5f24-45e6-a85f-6dcfacb58d5d",
"name": "计算相似度分数",
"type": "n8n-nodes-base.code",
"position": [
1660,
-140
],
"parameters": {
"jsCode": "const [vectorsA, vectorsB] = $input.all().map(item => item.json.data[0].embedding);\nconst score = cosineSimilarity(vectorsA, vectorsB);\n\nreturn { json: { similarityScore: score } };\n\nfunction cosineSimilarity(a, b) { \n let dotProduct = normA = normB = 0;\n for (let i = 0; i < a.length; i++) {\n dotProduct += a[i] * b[i];\n normA += a[i] ** 2;\n normB += b[i] ** 2;\n }\n return dotProduct / (Math.sqrt(normA) * Math.sqrt(normB));\n}"
},
"typeVersion": 2
},
{
"id": "69c30814-9725-426d-9572-9f92bd81dea5",
"name": "问题转项目",
"type": "n8n-nodes-base.code",
"position": [
1260,
-140
],
"parameters": {
"jsCode": "return [\n { json: { data: $('Set Input Fields').first().json.question } },\n { json: { data: $input.first().json.output.question } }\n]"
},
"typeVersion": 2
},
{
"id": "83be23ff-6857-4265-b1d0-f4b682c66903",
"name": "计算相关性分数",
"type": "n8n-nodes-base.set",
"position": [
1860,
-140
],
"parameters": {
"options": {},
"assignments": {
"assignments": [
{
"id": "c5abdf27-cb9e-4969-b669-25c6fbd135f1",
"name": "score",
"type": "number",
"value": "={{ $json.similarityScore * Math.abs(!$('Answer Relevance').first().json.output.noncommittal) }}"
}
]
}
},
"typeVersion": 3.4
}
],
"pinData": {},
"connections": {
"AI Agent": {
"main": [
[
{
"node": "Evaluation",
"type": "main",
"index": 0
}
]
]
},
"Evaluation": {
"main": [
[
{
"node": "Set Input Fields",
"type": "main",
"index": 0
}
],
[
{
"node": "No Operation, do nothing",
"type": "main",
"index": 0
}
]
]
},
"Remap Input": {
"main": [
[
{
"node": "AI Agent",
"type": "main",
"index": 0
}
]
]
},
"Update Output": {
"main": [
[
{
"node": "Update Metrics",
"type": "main",
"index": 0
}
]
]
},
"Get Embeddings": {
"main": [
[
{
"node": "Calculate Similarity Score",
"type": "main",
"index": 0
}
]
]
},
"Answer Relevance": {
"main": [
[
{
"node": "Questions to Items",
"type": "main",
"index": 0
}
]
]
},
"Set Input Fields": {
"main": [
[
{
"node": "Answer Relevance",
"type": "main",
"index": 0
}
]
]
},
"OpenAI Chat Model": {
"ai_languageModel": [
[
{
"node": "Answer Relevance",
"type": "ai_languageModel",
"index": 0
}
]
]
},
"OpenAI Chat Model1": {
"ai_languageModel": [
[
{
"node": "AI Agent",
"type": "ai_languageModel",
"index": 0
}
]
]
},
"Questions to Items": {
"main": [
[
{
"node": "Get Embeddings",
"type": "main",
"index": 0
}
]
]
},
"Structured Output Parser": {
"ai_outputParser": [
[
{
"node": "Answer Relevance",
"type": "ai_outputParser",
"index": 0
}
]
]
},
"Calculate Relevance Score": {
"main": [
[
{
"node": "Update Output",
"type": "main",
"index": 0
}
]
]
},
"Calculate Similarity Score": {
"main": [
[
{
"node": "Calculate Relevance Score",
"type": "main",
"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
}
]
]
}
}
}常见问题
如何使用这个工作流?
复制上方的 JSON 配置代码,在您的 n8n 实例中创建新工作流并选择「从 JSON 导入」,粘贴配置后根据需要修改凭证设置即可。
这个工作流适合什么场景?
高级 - 工程, 人工智能
需要付费吗?
本工作流完全免费,您可以直接导入使用。但请注意,工作流中使用的第三方服务(如 OpenAI API)可能需要您自行付费。
相关工作流推荐
使用OpenAI和RAGAS方法评估AI代理响应正确性
使用OpenAI和RAGAS方法评估AI代理响应正确性
Set
Code
Merge
+12
27 节点Jimleuk
工程
使用OpenAI评估RAG响应准确性:文档基础性指标
使用OpenAI评估RAG响应准确性:文档基础性指标
Set
Evaluation
Http Request
+13
25 节点Jimleuk
工程
评估指标:答案相似度
评估指标:答案相似度
Set
Code
Merge
+10
21 节点Jimleuk
工程
评估指标:摘要
评估指标:摘要
Set
Webhook
Evaluation
+9
17 节点Jimleuk
工程
使用 n8n、Apify 和 OpenAI o3 自托管 AI 深度研究代理
使用 n8n、Apify 和 OpenAI o3 自托管 AI 深度研究代理
If
Set
Code
+20
87 节点Jimleuk
人工智能
OpenAI响应API适配器 - LLM与AI智能体工作流
面向LLM和AI智能体工作流的OpenAI响应API适配器
If
Code
Webhook
+6
19 节点Jimleuk
工程
工作流信息
难度等级
高级
节点数量20
分类2
节点类型12
作者
Jimleuk
@jimleukFreelance 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 查看 →
分享此工作流