文章目录
本文参考视频教程:
五里墩茶社 : Dify聊天工作流 | 基于私有知识库和搜索引擎,构建高质量RAG聊天应用
https://www.bilibili.com/video/BV19E4m1R7c2/
一、创建 Workflow
创建app

添加节点

配置知识库

测试RAG节点

输出结果
json
{
"result": [
{
"metadata": {
"_source": "knowledge",
"dataset_id": "567f9f95-18e7-465a-a5ba-fcdf06363b2d",
"dataset_name": "中国AI出海现状1.md...",
"document_id": "e61d3406-01bc-4e8a-ad29-a5c6c001f131",
"document_name": "中国AI出海现状2.md",
"document_data_source_type": "upload_file",
"segment_id": "3607de39-fa16-4923-826f-5134407f6c6d",
"retriever_from": "workflow",
"score": 0.4948092089155974,
"segment_hit_count": 2,
"segment_word_count": 270,
"segment_position": 4,
"segment_index_node_hash": "fd799c4610bc119e339e3b8f5f73d96409927e2336f03fb67e6b975fbb08a3c5",
"position": 1
},
"title": "中国AI出海现状2.md",
"content": "谁在出海?\n首先抓住全球化机遇的公司,不出所料,是中国的科技巨头。华为、腾讯和阿里巴巴等公司在扩展海外时,重点放在云计算和 AI 基础设施上。2024 年 3 月,**腾讯云**与沙特阿拉伯领先的电信公司 Etihad Etisalat (Mobily) 合作,启动了"Go Saudi"项目,旨在推动沙特阿拉伯王国的数字化转型,作为其 2030 愿景战略的一部分。同年 5 月,**华为**在北非推出了 Galaxy AI,作为促进数字智能化转型的更大计划的一部分。此举是华为五年内计划在该地区投资 4.3 亿美元以加速智能化转型的一部分"
},
{
"metadata": {
"_source": "knowledge",
"dataset_id": "567f9f95-18e7-465a-a5ba-fcdf06363b2d",
"dataset_name": "中国AI出海现状1.md...",
"document_id": "cc09c21b-cd1f-4b7d-a28a-8aefd282b491",
"document_name": "中国AI出海现状1.md",
"document_data_source_type": "upload_file",
"segment_id": "e74112d6-8c97-4c92-a6e0-5da6fb025f15",
"retriever_from": "workflow",
"score": 0.3279104806854989,
"segment_hit_count": 2,
"segment_word_count": 7,
"segment_position": 1,
"segment_index_node_hash": "5b52ad98f45853445b35b524043506f3998622e7b43ad0afb2c809262673b2c4",
"position": 2
},
"title": "中国AI出海现状1.md",
"content": "为什么要出海?"
},
{
"metadata": {
"_source": "knowledge",
"dataset_id": "567f9f95-18e7-465a-a5ba-fcdf06363b2d",
"dataset_name": "中国AI出海现状1.md...",
"document_id": "cc09c21b-cd1f-4b7d-a28a-8aefd282b491",
"document_name": "中国AI出海现状1.md",
"document_data_source_type": "upload_file",
"segment_id": "171626c5-e0a0-4755-a8cb-f4e759ee2b25",
"retriever_from": "workflow",
"score": 0.3022106229406489,
"segment_hit_count": 2,
"segment_word_count": 25,
"segment_position": 6,
"segment_index_node_hash": "b46ec7d4aef2135434d16e4c36d9e819cc82436ca621f14219b75cfdaf7cbd66",
"position": 3
},
"title": "中国AI出海现状1.md",
"content": "在以上列举的情况下,出海似乎成为了企业的一条出路。"
}
]
}
配置 LLM

大模型提示词
/
输入引用

二、API 调用
创建 Key
每个 app 使用单独的 key 来请求

申请到的key,样式如:app-H7GziuCtNz7mbxcKJ4SI3tdA

chat-messages 请求示例
shell
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoiNTVkZGQ3YTQtMzlkMi00YzFjLTk1NDktZjhkMjQ3MDAzYjcxIiwiZXhwIjoxNzM3Njk5MDk0LCJpc3MiOiJTRUxGX0hPU1RFRCIsInN1YiI6IkNvbnNvbGUgQVBJIFBhc3Nwb3J0In0.sxUuvylrAvHGoEUhqOHLuOqi_TxmkLHqaduFp1leOK0
shell
curl -X POST 'http://127.0.0.1:5001/v1/chat-messages' \
--header 'Authorization: Bearer app-H7GziuCtNz7mbxcKJ4SI3tdA' \
--header 'Content-Type: application/json' \
--data-raw '{
"inputs": {},
"query": "请介绍AI出海",
"response_mode": "streaming",
"user": "eliz",
}'
报 json 错误,可以检查你的请求中,json 字典中最后是否有多余的逗号 ,
json
{
"code": "bad_request",
"message": "Failed to decode JSON object: Expecting property name enclosed in double quotes: line 6 column 1 (char 100)",
"status": 400
}
Python 请求
python
def test1():
url = 'http://127.0.0.1:5001/v1/chat-messages'
headers = {
'Authorization': 'Bearer app-H7GziuCtNz7mbxcKJ4SI3tdA',
'Content-Type': 'application/json'
}
param = {
"inputs": {},
"query": "请介绍AI出海",
"response_mode": "blocking",
"user": "eliz",
}
ret = requests.post(url, data=json.dumps(param), headers=headers)
# ret = requests.get(url, json=param, headers=headers)
print(ret)
print(ret.text)
# if ret.status_code != 200:return
jtext = ret.text.split('data:')[-1].strip()
rj = json.loads(jtext)
print(rj)
返回
python
{
'event': 'message',
'task_id': 'c45ab3b7-15b0-42c4-9ac6-1d8f53d9ecfb',
'id': 'e42c61e2-e6e7-474a-a873-1de15b2decc7',
'message_id': 'e42c61e2-e6e7-474a-a873-1de15b2decc7',
'conversation_id': '7b3bb5b1-20d5-4f42-a06e-8b5f112416a3',
'mode': 'advanced-chat',
'answer': '根据你提供的信息,AI出海指的是中国科技巨头,如华为和腾讯等,将自身在AI和云计算方面的技术和产品推广到海外市场的行为。\n\n**具体来说,AI出海包括以下几个方面:**\n\n* **基础设施建设:** 这些公司将重点放在云计算和AI基础设施的建设上,为其他企业和政府提供数字化转型所需的基础设施。\n* **技术输出:** 将自身在AI领域的技术,例如华为的Galaxy AI,输出到海外市场。\n* **战略合作:** 与当地企业进行合作,如腾讯云与沙特电信公司Mobily的合作,共同推动当地的数字化转型。\n* **投资布局:** 为了加速智能化转型,公司会在海外进行投资,例如华为计划在北非投资4.3亿美元。\n\n**为什么要出海?**\n\n根据你提供的信息,出海似乎是企业发展的一种战略选择,可能的原因包括:\n\n* **寻求新的增长机会:** 国内市场竞争激烈,出海可以帮助企业开拓新的市场,寻找新的增长点。\n* **响应全球化趋势:** 全球化是不可避免的趋势,出海可以帮助企业在全球范围内建立品牌影响力。\n* **技术优势:** 中国科技巨头在AI和云计算领域拥有一定的技术优势,出海可以将这些优势转化为商业价值。\n* **响应当地政策需求:** 某些海外市场可能存在数字化转型的需求,出海可以帮助企业抓住这些机会。\n\n**案例:**\n\n* **腾讯云"Go Saudi"项目:** 腾讯云与沙特阿拉伯电信公司合作,旨在推动沙特的数字化转型。\n* **华为在北非推出Galaxy AI:** 华为在北非推出AI产品,并计划投资4.3亿美元以加速当地的智能化转型。\n\n总而言之,AI出海是中国科技巨头在全球范围内扩展业务的重要战略,它不仅可以帮助企业获得新的增长机会,也能推动全球的数字化转型进程。\n',
'metadata': {
'retriever_resources': [{
'position': 1,
'dataset_id': '567f9f95-18e7-465a-a5ba-fcdf06363b2d',
'dataset_name': '中国AI出海现状1.md...',
'document_id': 'e61d3406-01bc-4e8a-ad29-a5c6c001f131',
'document_name': '中国AI出海现状2.md',
'data_source_type': 'upload_file',
'segment_id': '3607de39-fa16-4923-826f-5134407f6c6d',
'retriever_from': 'workflow',
'score': 0.547293785611343,
'hit_count': 20,
'word_count': 270,
'segment_position': 4,
'index_node_hash': 'fd799c4610bc119e339e3b8f5f73d96409927e2336f03fb67e6b975fbb08a3c5',
'content': '谁在出海?\n首先抓住全球化机遇的公司,不出所料,是中国的科技巨头。华为、腾讯和阿里巴巴等公司在扩展海外时,重点放在云计算和 AI 基础设施上。2024 年 3 月,**腾讯云**与沙特阿拉伯领先的电信公司 Etihad Etisalat (Mobily) 合作,启动了"Go Saudi"项目,旨在推动沙特阿拉伯王国的数字化转型,作为其 2030 愿景战略的一部分。同年 5 月,**华为**在北非推出了 Galaxy AI,作为促进数字智能化转型的更大计划的一部分。此举是华为五年内计划在该地区投资 4.3 亿美元以加速智能化转型的一部分',
'page': None
}, {
'position': 2,
'dataset_id': '567f9f95-18e7-465a-a5ba-fcdf06363b2d',
'dataset_name': '中国AI出海现状1.md...',
'document_id': 'cc09c21b-cd1f-4b7d-a28a-8aefd282b491',
'document_name': '中国AI出海现状1.md',
'data_source_type': 'upload_file',
'segment_id': 'e74112d6-8c97-4c92-a6e0-5da6fb025f15',
'retriever_from': 'workflow',
'score': 0.3979864901356652,
'hit_count': 21,
'word_count': 7,
'segment_position': 1,
'index_node_hash': '5b52ad98f45853445b35b524043506f3998622e7b43ad0afb2c809262673b2c4',
'content': '为什么要出海?',
'page': None
}, {
'position': 3,
'dataset_id': '567f9f95-18e7-465a-a5ba-fcdf06363b2d',
'dataset_name': '中国AI出海现状1.md...',
'document_id': 'cc09c21b-cd1f-4b7d-a28a-8aefd282b491',
'document_name': '中国AI出海现状1.md',
'data_source_type': 'upload_file',
'segment_id': '171626c5-e0a0-4755-a8cb-f4e759ee2b25',
'retriever_from': 'workflow',
'score': 0.33822810090097916,
'hit_count': 20,
'word_count': 25,
'segment_position': 6,
'index_node_hash': 'b46ec7d4aef2135434d16e4c36d9e819cc82436ca621f14219b75cfdaf7cbd66',
'content': '在以上列举的情况下,出海似乎成为了企业的一条出路。',
'page': None
}],
'usage': {
'prompt_tokens': 215,
'prompt_unit_price': '0.00',
'prompt_price_unit': '0.000001',
'prompt_price': '0E-7',
'completion_tokens': 439,
'completion_unit_price': '0.00',
'completion_price_unit': '0.000001',
'completion_price': '0E-7',
'total_tokens': 654,
'total_price': '0E-7',
'currency': 'USD',
'latency': 6.455977750010788
}
},
'created_at': 1737706022
}
三、跟踪 Trace

聊天页面
显示为 Public URL http://127.0.0.1:3000/chat/gI7PDCEoQLq1zQeV
2025-02-14(五)情人节快乐 --<-<-<@
愿天下有情人,终成眷属🌹