前提:

        使用RAG必须提交文件,分析系列1中对于提交文件进行了分析,提交文件后,后端返回文件相关数据给前端,前端基于这些数据进行后继的处理。后端返回数据如下:

{
    "id": "c50e5e9a-170f-4576-a911-36f5f11506fc", #文件唯一标识
    "user_id": "e6d4a214-8982-40ad-9bbc-77ee14534d58", #用户唯一标识

    #文件内容摘要
    "hash": "98bac8df1563eda073cc5bc73da30f661024e285a8dbc632221b3198cba2b2a7",
    #文件名

    "filename": "beijing_annual_report_2024.pdf",
    "data": {#文件内容。详细内容省略
        "content": "各位代表:\n现在,我代表北京市人民政府,……中国式现代化作出首都贡献!"
    },
    "meta": {#文件元数据
        "name": "beijing_annual_report_2024.pdf",
        "content_type": "application/pdf",
        "size": 329811,
        "data": {},
        "collection_name": "file-c50e5e9a-170f-4576-a911-36f5f11506fc"#向量库集合
    },
    "created_at": 1755485272,
    "updated_at": 1755485272
}

        一、创建对话

       创建对话逻辑在系列2中已经分析,在此仅对请求和应答报文做简单说明。报文与简单会话的区别是其中增加了files数据,内容为提交的文件数据及元数据,具体如下。

#请求报文如下

{
  "chat": {
    "id": "",
    "title": "新对话",
    "models": [
      "deepseek-r1:1.5b"
    ],
    "params": {},
    "history": {
      "messages": {
        "63f6e78b-94cc-436f-aac7-b02b2a4ee80e": {
          "id": "63f6e78b-94cc-436f-aac7-b02b2a4ee80e",
          "parentId": null,
          "childrenIds": [],
          "role": "user",
          "content": "请用对文档内容进行简单摘要,字数不超过100字。",
          "files": [#文件数据
            {
              "type": "file",
              "file": {
                "id": "c50e5e9a-170f-4576-a911-36f5f11506fc",
                "user_id": "e6d4a214-8982-40ad-9bbc-77ee14534d58",
                "hash": "98bac8df1563eda073cc5bc73da30f661024e285a8dbc632221b3198cba2b2a7",
                "filename": "beijing_annual_report_2024.pdf",
                "data": {
                  "content": "各位代表:\n现在,我代表北京府,……,中国式现代化作出首都贡献!"
                },
                "meta": {
                  "name": "beijing_annual_report_2024.pdf",
                  "content_type": "application/pdf",
                  "size": 329811,
                  "data": {},
                  "collection_name": "file-c50e5e9a-170f-4576-a911-36f5f11506fc"
                },
                "created_at": 1755485272,
                "updated_at": 1755485272
              },
              "id": "c50e5e9a-170f-4576-a911-36f5f11506fc",
              "url": "/api/v1/files/c50e5e9a-170f-4576-a911-36f5f11506fc",
              "name": "beijing_annual_report_2024.pdf",
              "collection_name": "file-c50e5e9a-170f-4576-a911-36f5f11506fc",
              "status": "uploaded",
              "size": 329811,
              "error": "",
              "itemId": "634420a9-4b4c-4b6f-84c5-949b53005a66"
            }
          ],
          "timestamp": 1755485603,
          "models": [
            "deepseek-r1:1.5b"
          ]
        }
      },
      "tags": [],
      "timestamp": 1755485603529
    },
    "folder_id": null
  }
}

#应答报文如下

{
    "id": "c372b7bd-8544-4d73-b35b-945e396dc3f0",
    "user_id": "e6d4a214-8982-40ad-9bbc-77ee14534d58",
    "title": "新对话",
    "chat": {
        "id": "",
        "title": "新对话",
        "models": [
            "deepseek-r1:1.5b"
        ],
        "params": {},
        "history": {
            "messages": {
                "63f6e78b-94cc-436f-aac7-b02b2a4ee80e": {
                    "id": "63f6e78b-94cc-436f-aac7-b02b2a4ee80e",
                    "parentId": null,
                    "childrenIds": [],
                    "role": "user",
                    "content": "请用对文档内容进行简单摘要,字数不超过100字。",
                    "files": [
                        {
                            "type": "file",
                            "file": {
                                "id": "c50e5e9a-170f-4576-a911-36f5f11506fc",
                                "user_id": "e6d4a214-8982-40ad-9bbc-77ee14534d58",
                                "hash": "98bac8df1563eda073cc5bc73da30f661024e285a8dbc632221b3198cba2b2a7",
                                "filename": "beijing_annual_report_2024.pdf",
                                "data": {
                                    "content": "各位代表:\n现在,我代表北京市人民政府,……着力推动高质量发展,为推进中国式现代化作出首都贡献!"
                                },
                                "meta": {
                                    "name": "beijing_annual_report_2024.pdf",
                                    "content_type": "application/pdf",
                                    "size": 329811,
                                    "data": {},
                                    "collection_name": "file-c50e5e9a-170f-4576-a911-36f5f11506fc"
                                },
                                "created_at": 1755485272,
                                "updated_at": 1755485272
                            },
                            "id": "c50e5e9a-170f-4576-a911-36f5f11506fc",
                            "url": "/api/v1/files/c50e5e9a-170f-4576-a911-36f5f11506fc",
                            "name": "beijing_annual_report_2024.pdf",
                            "collection_name": "file-c50e5e9a-170f-4576-a911-36f5f11506fc",
                            "status": "uploaded",
                            "size": 329811,
                            "error": "",
                            "itemId": "634420a9-4b4c-4b6f-84c5-949b53005a66"
                        }
                    ],
                    "timestamp": 1755485603,
                    "models": [
                        "deepseek-r1:1.5b"
                    ]
                }
            },
            "currentId": "63f6e78b-94cc-436f-aac7-b02b2a4ee80e"
        },
        "messages": [
            {
                "id": "63f6e78b-94cc-436f-aac7-b02b2a4ee80e",
                "parentId": null,
                "childrenIds": [],
                "role": "user",
                "content": "请用对文档内容进行简单摘要,字数不超过100字。",
                "files": [
                    {
                        "type": "file",
                        "file": {
                            "id": "c50e5e9a-170f-4576-a911-36f5f11506fc",
                            "user_id": "e6d4a214-8982-40ad-9bbc-77ee14534d58",
                            "hash": "98bac8df1563eda073cc5bc73da30f661024e285a8dbc632221b3198cba2b2a7",
                            "filename": "beijing_annual_report_2024.pdf",
                            "data": {
                                "content": "各位代表:\n现在,我代表北京市人民政府,……着力推动高质量发展,为推进中国式现代化作出首都贡献!"
                            },
                            "meta": {
                                "name": "beijing_annual_report_2024.pdf",
                                "content_type": "application/pdf",
                                "size": 329811,
                                "data": {},
                                "collection_name": "file-c50e5e9a-170f-4576-a911-36f5f11506fc"
                            },
                            "created_at": 1755485272,
                            "updated_at": 1755485272
                        },
                        "id": "c50e5e9a-170f-4576-a911-36f5f11506fc",
                        "url": "/api/v1/files/c50e5e9a-170f-4576-a911-36f5f11506fc",
                        "name": "beijing_annual_report_2024.pdf",
                        "collection_name": "file-c50e5e9a-170f-4576-a911-36f5f11506fc",
                        "status": "uploaded",
                        "size": 329811,
                        "error": "",
                        "itemId": "634420a9-4b4c-4b6f-84c5-949b53005a66"
                    }
                ],
                "timestamp": 1755485603,
                "models": [
                    "deepseek-r1:1.5b"
                ]
            }
        ],
        "tags": [],
        "timestamp": 1755485603529
    },
    "updated_at": 1755485605,
    "created_at": 1755485605,
    "share_id": null,
    "archived": false,
    "pinned": false,
    "meta": {},
    "folder_id": null
}

        二、更新会话

        更新会话逻辑不变,仅在请求和应答中多携带files数据,内容为提交的文件数据及元数据,具体如下。

#请求报文

{
  "chat": {
    "models": [
      "deepseek-r1:1.5b"
    ],
    "history": {
      "messages": {
        "63f6e78b-94cc-436f-aac7-b02b2a4ee80e": {
          "id": "63f6e78b-94cc-436f-aac7-b02b2a4ee80e",
          "parentId": null,
          "childrenIds": [
            "75899389-8089-4cc4-a381-a5d38d5e39b9"
          ],
          "role": "user",
          "content": "请用对文档内容进行简单摘要,字数不超过100字。",
          "files": [
            {
              "type": "file",
              "file": {
                "id": "c50e5e9a-170f-4576-a911-36f5f11506fc",
                "user_id": "e6d4a214-8982-40ad-9bbc-77ee14534d58",
                "hash": "98bac8df1563eda073cc5bc73da30f661024e285a8dbc632221b3198cba2b2a7",
                "filename": "beijing_annual_report_2024.pdf",
                "data": {
                  "content": "……"
                },
                "meta": {
                  "name": "beijing_annual_report_2024.pdf",
                  "content_type": "application/pdf",
                  "size": 329811,
                  "data": {},
                  "collection_name": "file-c50e5e9a-170f-4576-a911-36f5f11506fc"
                },
                "created_at": 1755485272,
                "updated_at": 1755485272
              },
              "id": "c50e5e9a-170f-4576-a911-36f5f11506fc",
              "url": "/api/v1/files/c50e5e9a-170f-4576-a911-36f5f11506fc",
              "name": "beijing_annual_report_2024.pdf",
              "collection_name": "file-c50e5e9a-170f-4576-a911-36f5f11506fc",
              "status": "uploaded",
              "size": 329811,
              "error": "",
              "itemId": "634420a9-4b4c-4b6f-84c5-949b53005a66"
            }
          ]
        }
      }
    }
  }
}

应答报文:

{
    "id": "c372b7bd-8544-4d73-b35b-945e396dc3f0",
    "user_id": "e6d4a214-8982-40ad-9bbc-77ee14534d58",
    "title": "新对话",
    "chat": {
        "id": "",
        "title": "新对话",
        "models": [
            "deepseek-r1:1.5b"
        ],
        "params": {},
        "history": {
            "messages": {
                "63f6e78b-94cc-436f-aac7-b02b2a4ee80e": {
                    "id": "63f6e78b-94cc-436f-aac7-b02b2a4ee80e",
                    "parentId": null,
                    "childrenIds": [
                        "75899389-8089-4cc4-a381-a5d38d5e39b9"
                    ],
                    "role": "user",
                    "content": "请用对文档内容进行简单摘要,字数不超过100字。",
                    "files": [
                        {
                            "type": "file",
                            "file": {
                                "id": "c50e5e9a-170f-4576-a911-36f5f11506fc",
                                "user_id": "e6d4a214-8982-40ad-9bbc-77ee14534d58",
                                "hash": "98bac8df1563eda073cc5bc73da30f661024e285a8dbc632221b3198cba2b2a7",
                                "filename": "beijing_annual_report_2024.pdf",
                                "data": {
                                    "content": "……"
                                },
                                "meta": {
                                    "name": "beijing_annual_report_2024.pdf",
                                    "content_type": "application/pdf",
                                    "size": 329811,
                                    "data": {},
                                    "collection_name": "file-c50e5e9a-170f-4576-a911-36f5f11506fc"
                                },
                                "created_at": 1755485272,
                                "updated_at": 1755485272
                            },
                            "id": "c50e5e9a-170f-4576-a911-36f5f11506fc",
                            "url": "/api/v1/files/c50e5e9a-170f-4576-a911-36f5f11506fc",
                            "name": "beijing_annual_report_2024.pdf",
                            "collection_name": "file-c50e5e9a-170f-4576-a911-36f5f11506fc",
                            "status": "uploaded",
                            "size": 329811,
                            "error": "",
                            "itemId": "634420a9-4b4c-4b6f-84c5-949b53005a66"
                        }
                    ],
                    "timestamp": 1755485603,
                    "models": [
                        "deepseek-r1:1.5b"
                    ]
                },
                "75899389-8089-4cc4-a381-a5d38d5e39b9": {
                    "parentId": "63f6e78b-94cc-436f-aac7-b02b2a4ee80e",
                    "id": "75899389-8089-4cc4-a381-a5d38d5e39b9",
                    "childrenIds": [],
                    "role": "assistant",
                    "content": "",
                    "model": "deepseek-r1:1.5b",
                    "modelName": "deepseek-r1:1.5b",
                    "modelIdx": 0,
                    "timestamp": 1755485603
                }
            },
            "currentId": "75899389-8089-4cc4-a381-a5d38d5e39b9"
        },
        "messages": [
            {
                "id": "63f6e78b-94cc-436f-aac7-b02b2a4ee80e",
                "parentId": null,
                "childrenIds": [
                    "75899389-8089-4cc4-a381-a5d38d5e39b9"
                ],
                "role": "user",
                "content": "请用对文档内容进行简单摘要,字数不超过100字。",
                "files": [
                    {
                        "type": "file",
                        "file": {
                            "id": "c50e5e9a-170f-4576-a911-36f5f11506fc",
                            "user_id": "e6d4a214-8982-40ad-9bbc-77ee14534d58",
                            "hash": "98bac8df1563eda073cc5bc73da30f661024e285a8dbc632221b3198cba2b2a7",
                            "filename": "beijing_annual_report_2024.pdf",
                            "data": {
                                "content": "……"
                            },
                            "meta": {
                                "name": "beijing_annual_report_2024.pdf",
                                "content_type": "application/pdf",
                                "size": 329811,
                                "data": {},
                                "collection_name": "file-c50e5e9a-170f-4576-a911-36f5f11506fc"
                            },
                            "created_at": 1755485272,
                            "updated_at": 1755485272
                        },
                        "id": "c50e5e9a-170f-4576-a911-36f5f11506fc",
                        "url": "/api/v1/files/c50e5e9a-170f-4576-a911-36f5f11506fc",
                        "name": "beijing_annual_report_2024.pdf",
                        "collection_name": "file-c50e5e9a-170f-4576-a911-36f5f11506fc",
                        "status": "uploaded",
                        "size": 329811,
                        "error": "",
                        "itemId": "634420a9-4b4c-4b6f-84c5-949b53005a66"
                    }
                ],
                "timestamp": 1755485603,
                "models": [
                    "deepseek-r1:1.5b"
                ]
            },
            {
                "parentId": "63f6e78b-94cc-436f-aac7-b02b2a4ee80e",
                "id": "75899389-8089-4cc4-a381-a5d38d5e39b9",
                "childrenIds": [],
                "role": "assistant",
                "content": "",
                "model": "deepseek-r1:1.5b",
                "modelName": "deepseek-r1:1.5b",
                "modelIdx": 0,
                "timestamp": 1755485603
            }
        ],
        "tags": [],
        "timestamp": 1755485603529,
        "files": [
            {
                "type": "file",
                "file": {
                    "id": "c50e5e9a-170f-4576-a911-36f5f11506fc",
                    "user_id": "e6d4a214-8982-40ad-9bbc-77ee14534d58",
                    "hash": "98bac8df1563eda073cc5bc73da30f661024e285a8dbc632221b3198cba2b2a7",
                    "filename": "beijing_annual_report_2024.pdf",
                    "data": {
                        "content": "……"
                    },
                    "meta": {
                        "name": "beijing_annual_report_2024.pdf",
                        "content_type": "application/pdf",
                        "size": 329811,
                        "data": {},
                        "collection_name": "file-c50e5e9a-170f-4576-a911-36f5f11506fc"
                    },
                    "created_at": 1755485272,
                    "updated_at": 1755485272
                },
                "id": "c50e5e9a-170f-4576-a911-36f5f11506fc",
                "url": "/api/v1/files/c50e5e9a-170f-4576-a911-36f5f11506fc",
                "name": "beijing_annual_report_2024.pdf",
                "collection_name": "file-c50e5e9a-170f-4576-a911-36f5f11506fc",
                "status": "uploaded",
                "size": 329811,
                "error": "",
                "itemId": "634420a9-4b4c-4b6f-84c5-949b53005a66"
            }
        ]
    },
    "updated_at": 1755485605,
    "created_at": 1755485605,
    "share_id": null,
    "archived": false,
    "pinned": false,
    "meta": {},
    "folder_id": null
}

        三、发起补足

        1)发起补足时,请求报文如下,同简单对话相比,其中增加了files数据:

{
  "stream": true,
  "model": "deepseek-r1:1.5b",
  "messages": [
    {
      "role": "user",
      "content": "请用对文档内容进行简单摘要,字数不超过100字。"
    }
  ],
  "params": {},
  "files": [
    {
      "type": "file",
      "file": {
        "id": "c50e5e9a-170f-4576-a911-36f5f11506fc",
        "user_id": "e6d4a214-8982-40ad-9bbc-77ee14534d58",
        "hash": "98bac8df1563eda073cc5bc73da30f661024e285a8dbc632221b3198cba2b2a7",
        "filename": "beijing_annual_report_2024.pdf",
        "data": {
          "content": "各位代表:\n现在,……着力推动高质量发展,国式现代化作出首都贡献!"
        },
        "meta": {
          "name": "beijing_annual_report_2024.pdf",
          "content_type": "application/pdf",
          "size": 329811,
          "data": {},
          "collection_name": "file-c50e5e9a-170f-4576-a911-36f5f11506fc"
        },
        "created_at": 1755485272,
        "updated_at": 1755485272
      },
      "id": "c50e5e9a-170f-4576-a911-36f5f11506fc",
      "url": "/api/v1/files/c50e5e9a-170f-4576-a911-36f5f11506fc",
      "name": "beijing_annual_report_2024.pdf",
      "collection_name": "file-c50e5e9a-170f-4576-a911-36f5f11506fc",
      "status": "uploaded",
      "size": 329811,
      "error": "",
      "itemId": "634420a9-4b4c-4b6f-84c5-949b53005a66"
    }
  ],
  "tool_servers": [],
  "features": {
    "image_generation": false,
    "code_interpreter": false,
    "web_search": false,
    "memory": false
  },
  "variables": {
    "{{USER_NAME}}": "acaluis",
    "{{USER_LOCATION}}": "Unknown",
    "{{CURRENT_DATETIME}}": "2025-08-18 10:53:23",
    "{{CURRENT_DATE}}": "2025-08-18",
    "{{CURRENT_TIME}}": "10:53:23",
    "{{CURRENT_WEEKDAY}}": "Monday",
    "{{CURRENT_TIMEZONE}}": "Etc/GMT-8",
    "{{USER_LANGUAGE}}": "zh-CN"
  },
  "model_item": {
    "id": "deepseek-r1:1.5b",
    "name": "deepseek-r1:1.5b",
    "object": "model",
    "created": 1755485297,
    "owned_by": "ollama",
    "ollama": {
      "name": "deepseek-r1:1.5b",
      "model": "deepseek-r1:1.5b",
      "modified_at": "2025-08-17T04:50:08.766430912Z",
      "size": 1117322768,
      "digest": "e0979632db5a88d1a53884cb2a941772d10ff5d055aabaa6801c4e36f3a6c2d7",
      "details": {
        "parent_model": "",
        "format": "gguf",
        "family": "qwen2",
        "families": [
          "qwen2"
        ],
        "parameter_size": "1.8B",
        "quantization_level": "Q4_K_M"
      },
      "connection_type": "local",
      "urls": [
        0
      ]
    },
    "connection_type": "local",
    "tags": [],
    "actions": [],
    "filters": []
  },
  "session_id": "VT1Fh0unzSKZhblRAAAZ",
  "chat_id": "c372b7bd-8544-4d73-b35b-945e396dc3f0",
  "id": "75899389-8089-4cc4-a381-a5d38d5e39b9",
  "background_tasks": {
    "title_generation": true,
    "tags_generation": true,
    "follow_up_generation": true
  }
}

       2)主要流程如下图所示:

       3)代码分析

       查找相关代码,在process_chat_payload中,代码如下:

async def process_chat_payload(request, form_data, user, metadata, model):

    ……   

    try:

        #调用chat_completion_files_handler从向量库检索上下文。
        form_data, flags = await chat_completion_files_handler(request, form_data, user)
        sources.extend(flags.get("sources", []))

  '''以下是检索向量库得到的内容列表。

            soucrces内容如下:

            [
              {
  "source": { #文档来源
    "type": "file",
    "file": {
      "id": "4fe037da-0c09-42c2-a80b-6ada32529140",
      "user_id": "e6d4a214-8982-40ad-9bbc-77ee14534d58",
      "hash": "98bac8df1563eda073cc5bc73da30f661024e285a8dbc632221b3198cba2b2a7",
      "filename": "beijing_annual_report_2024.pdf",
      "data": {
        "content": "……"
      },
      "meta": {
        "name": "beijing_annual_report_2024.pdf",
        "content_type": "application/pdf",
        "size": 329811,
        "data": {},
        "collection_name": "file-4fe037da-0c09-42c2-a80b-6ada32529140"
      },
      "created_at": 1755089358,
      "updated_at": 1755089358
    },
    "id": "4fe037da-0c09-42c2-a80b-6ada32529140",
    "url": "/api/v1/files/4fe037da-0c09-42c2-a80b-6ada32529140",
    "name": "beijing_annual_report_2024.pdf",
    "collection_name": "file-4fe037da-0c09-42c2-a80b-6ada32529140",
    "status": "uploaded",
    "size": 329811,
    "error": "",
    "itemId": "161c8c85-bd0b-4aa7-8332-c94e0c3359fc"
  },
  "document": [#shangxiawen wendang 
    "坚决贯彻党的民族政策和宗教工作基……式现代化作出首都贡献!",
    "水无情人有情的人间大爱,展现了……人民团体",
    "环境质量、能源、水资源等指标……平衡、相得益\n彰。"
  ],
  "metadata": [#文档对应的元数据
    {
      "author": "sky",
      "comments": "",
      "company": "",
      "created_by": "e6d4a214-8982-40ad-9bbc-77ee14534d58",
      "creationdate": "2024-02-08T20:34:12+12:34",
      "creator": "WPS 文字",
      "embedding_config": "{\"engine\": \"\", \"model\": \"sentence-transformers/all-MiniLM-L6-v2\"}",
      "file_id": "4fe037da-0c09-42c2-a80b-6ada32529140",
      "hash": "98bac8df1563eda073cc5bc73da30f661024e285a8dbc632221b3198cba2b2a7",
      "keywords": "",
      "moddate": "2024-02-08T20:34:12+12:34",
      "name": "beijing_annual_report_2024.pdf",
      "page": 12,
      "page_label": "13",
      "producer": "",
      "source": "beijing_annual_report_2024.pdf",
      "sourcemodified": "D:20240208203412+12'34'",
      "start_index": 0,
      "subject": "",
      "title": "",
      "total_pages": 13,
      "trapped": "/False"
    },
    {
      "author": "sky",
      "comments": "",
      "company": "",
      "created_by": "e6d4a214-8982-40ad-9bbc-77ee14534d58",
      "creationdate": "2024-02-08T20:34:12+12:34",
      "creator": "WPS 文字",
      "embedding_config": "{\"engine\": \"\", \"model\": \"sentence-transformers/all-MiniLM-L6-v2\"}",
      "file_id": "4fe037da-0c09-42c2-a80b-6ada32529140",
      "hash": "98bac8df1563eda073cc5bc73da30f661024e285a8dbc632221b3198cba2b2a7",
      "keywords": "",
      "moddate": "2024-02-08T20:34:12+12:34",
      "name": "beijing_annual_report_2024.pdf",
      "page": 3,
      "page_label": "4",
      "producer": "",
      "source": "beijing_annual_report_2024.pdf",
      "sourcemodified": "D:20240208203412+12'34'",
      "start_index": 875,
      "subject": "",
      "title": "",
      "total_pages": 13,
      "trapped": "/False"
    },
    {
      "author": "sky",
      "comments": "",
      "company": "",
      "created_by": "e6d4a214-8982-40ad-9bbc-77ee14534d58",
      "creationdate": "2024-02-08T20:34:12+12:34",
      "creator": "WPS 文字",
      "embedding_config": "{\"engine\": \"\", \"model\": \"sentence-transformers/all-MiniLM-L6-v2\"}",
      "file_id": "4fe037da-0c09-42c2-a80b-6ada32529140",
      "hash": "98bac8df1563eda073cc5bc73da30f661024e285a8dbc632221b3198cba2b2a7",
      "keywords": "",
      "moddate": "2024-02-08T20:34:12+12:34",
      "name": "beijing_annual_report_2024.pdf",
      "page": 4,
      "page_label": "5",
      "producer": "",
      "source": "beijing_annual_report_2024.pdf",
      "sourcemodified": "D:20240208203412+12'34'",
      "start_index": 919,
      "subject": "",
      "title": "",
      "total_pages": 13,
      "trapped": "/False"
    }
  ],
  "distances": [#文档与问题距离
    0.618969357592362,
    0.604947276246265,
    0.602841067931122
  ]
 }
]

'''
    except Exception as e:
        log.exception(e)

    #把检索到的上下文插入到消息中
    if len(sources) > 0:
        context_string = ""
        citation_idx = {}
        for source in sources:
            if "document" in source:
                for doc_context, doc_meta in zip(
                    source["document"], source["metadata"]
                ):

                   #来源文件名
                    source_name = source.get("source", {}).get("name", None)
                    citation_id = (#引用id
                        doc_meta.get("source", None)
                        or source.get("source", {}).get("id", None)
                        or "N/A"
                    )
                    if citation_id not in citation_idx:#针对同一个source的引用索引一致
                        citation_idx[citation_id] = len(citation_idx) + 1
                    context_string += (
                        f'<source id="{citation_idx[citation_id]}"'
                        + (f' name="{source_name}"' if source_name else "")
                        + f">{doc_context}</source>\n"
                    )

  '''

            拼接后上下文内容 context_string 为:

<context>\n<source id=\"1\" name=\"beijing_annual_report_2024.pdf\">和协同配套。着力打造京津冀协同发展示范区、京张体育文化旅\n游带,推动燃料电池汽\n车示范城市群建设。唱好京津\"双城记\",抓好天津滨海-中关村科技园等合作园区建设。\n加强公共服务共建共享。巩固提升\"轨道上的京津\",建成城际铁路联络线一期,实\n现国铁109新线高速通车。强化重污染天气应急应对,实施北方防沙带等生态保护和修复工\n程。深化教育、医疗、养老等公共服务合作,在就业、社保、税务等领域推出更多办事流程\n统一的\"跨省通办\"政务服务事项,共同打造区域一流营商环境。\n(二)聚力建设国际科技创新中心,进一步壮大发展新动能新优势\n充分发挥教育、科技、人才优势,加快构建高效协同开放的创新体系,着力打造我国自</source>\n<source id=\"1\" name=\"beijing_annual_report_2024.pdf\">文物保护利用示范区创建成果。加强非遗传承人培养激励,让传统技艺更好融入现代生活。\n加大\"演艺之都\"建设力度。倾力打造文化精品力作,构建多集群协同发展的演艺空间\n格局,培育更多演艺新空间。擦亮\"大戏看北京\"文化名片,推进\"北京大视听\"品牌建设,\n让文艺演出更加精彩纷呈。支持传统文化创新发展,推动更多优秀演艺机构、精品剧目\"走\n出去\",在国际舞台上展现北京文化魅力。\n充分激发文化创新创造活力。统筹用好各类文化产业引导基金,推动文化改革发展、\n文化产业园区提质升级。举办首都市民系列文化活动1.6万场,推进\"书香京城\"\"博物馆\n之城\"建设。办好北京国际电影节、国际设计周和国际音乐节等文化交流活动。\n(七)突出加强城市精细化治理,打造更加宜居的城市生活</source>\n<source id=\"1\" name=\"beijing_annual_report_2024.pdf\">4万农村劳动力就业参保,加强残疾人就业保障,让更多劳动者技有所施、才有所用、劳有\n所得。持续增加城乡居民收入,扩大中等收入群体规模。\n深化健康北京建设。深入推进\"三医\"联动改革,推动公立医院高质量发展。建立9\n个紧密型城市医疗集团,提升基层诊疗能力和全科医师培养质量。强化儿科服务体系和人才\n队伍建设,所有二级以上综合医院开设儿科门诊。扎实推进国家医学中心建设,实施中医药\n振兴发展重大工程。开展180项检验结果、300项检查结果互认,优化再造就医流程,加快\n推进北京卫生健康信息平台建设。优化慢病\"长处方\"管理,将处方用药量放宽至3个月。\n完善生育支持政策,加快普惠托育体系建设,新增普惠托位1万个。广泛开展群众性体育活动,\n高水平筹办各项重大体育赛事。</source>\n</context>

'''

        context_string = context_string.strip()
        prompt = get_last_user_message(form_data["messages"])#用户原始问题

        if prompt is None:
            raise Exception("No user message found")
        if (
            request.app.state.config.RELEVANCE_THRESHOLD == 0
            and context_string.strip() == ""
        ):
            log.debug(
                f"With a 0 relevancy threshold for RAG, the context cannot be empty"
            )

        # Workaround for Ollama 2.0+ system prompt issue
        '''

            把RAG模板、上下文和用户问题组织起来,然后与用户问题拼接,最终内容示例如

            下:  "messages": [
    {
      "role": "user",
      "content": "### Task:\nRespond to the user query using the provided context, incorporating inline citations in the format [id] **only when the <source> tag includes an explicit id attribute** (e.g., <source id=\"1\">).\n\n### Guidelines:\n- If you don't know the answer, clearly state that.\n- If uncertain, ask the user for clarification.\n- Respond in the same language as the user's query.\n- If the context is unreadable or of poor quality, inform the user and provide the best possible answer.\n- If the answer isn't present in the context but you possess the knowledge, explain this to the user and provide the answer using your own understanding.\n- **Only include inline citations using [id] (e.g., [1], [2]) when the <source> tag includes an id attribute.**\n- Do not cite if the <source> tag does not contain an id attribute.\n- Do not use XML tags in your response.\n- Ensure citations are concise and directly related to the information provided.\n\n### Example of Citation:\nIf the user asks about a specific topic and the information is found in a source with a provided id attribute, the response should include the citation like in the following example:\n* \"According to the study, the proposed method increases efficiency by 20% [1].\"\n\n### Output:\nProvide a clear and direct response to the user's query, including inline citations in the format [id] only when the <source> tag with id attribute is present in the context.\n\n<context>\n<source id=\"1\" name=\"bjreport.pdf\">4万农村劳动力就业参保,加强残疾人就业保障,让更多劳动者有业可成、才有所用、劳有所得。持续增加城乡居民收入,扩大中等收入群体规模。\n深化健康北京建设。深入推进“三医”联动改革,推动公立医院高质量发展。建立9个紧密型城市医疗集团,提升基层诊疗能力和全科医师培养质量。强化儿科服务体系和人才队伍建设,所有二级以上综合医院开设儿科门诊。落实推进国家医学中心建设,实施中医药振兴发展重大工程。开展180项检验结果、300项检查结果互认,优化再造就医流程,加快\n推进北京卫生健康信息平台建设。优化慢性病“长处方”管理,将处方用药量放宽至3个月。\n完善生育支持政策,加快普惠托育体系建设,新增普惠托位1万个。广泛开展群众性体育活动,高水平筹办各项重大体育赛事。</source>\n<source id=\"1\" name=\"bjreport.pdf\">生活方式和治理方式全面变革。\n统筹推进数字化。大力建设数据基础制度先行区,开展数据资产入表、数据跨境便利化服务等综合改革试点,推动算力中心、数据训练基地、国家区块链创新应用试点节点等一批重大项目落地,新建5G基站1万个以上。探索完善数据交易规范,提升北京国际大数据交易所\n运营能力。启动高级别自动驾驶示范区建设4.0阶段任务,推动机场、火车站、城市道路清扫等重点应用场景有序开放。\n大力支持产业数字化。深入实施“新智造100”工程,全面开展制造业企业数字化转型,\n加快培育标志性“数字领航”企业。促进平台经济有序竞争、创新发展,推动先进数字技术\n向中小企业深度普及,构建开放共享、充满活力的创新生态。提升人工智能底层技术和基础\n底座自主可控能力,推动人工智能模型对标国际先进水平,加快在政务、医疗、教育、工业、\n生活服务等领域应用,保持人工智能研发应用领先水平。</source>\n<source id=\"1\" name=\"bjreport.pdf\">文物保护利用示范区创建成果。加强非遗传承人培养激励,让传统工艺更好融入现代生活。\n加大“演艺之都”建设力度。倾力打造文化精品力作,构建多集群协同发展的演艺空间\n格局,培育更多演艺新空间。擦亮“大戏看北京”文化名片,推进“北京大视听”品牌建设,\n让文艺演出更加精彩纷呈。支持传统文化创新发展,推动更多优秀演艺机构、精品剧目“走出去”,在国际舞台上展现北京文化魅力。\n充分激发文化创新创造活力。统筹用好各类文化产业引导基金,推动文化企业改革发展、\n文化产业园区提质升级。举办首都市民系列文化活动1.6万场,推进“书香京城”“博物馆\n之城”建设。办好北京国际电影节、国际设计周和国际音乐节等文化交流活动。\n(七)突出加强城市精细化治理,打造更加宜居的城市生活</source>\n</context>\n\n<user_query>\n请用100个字以内总结一下文档内容</user_query>\n\n请用100个字以内总结一下文档内容"
    }

‘'''


        if model.get("owned_by") == "ollama":
            form_data["messages"] = prepend_to_first_user_message_content(
                rag_template(
                    request.app.state.config.RAG_TEMPLATE, context_string, prompt
                ),
                form_data["messages"],
            )
        else:
            form_data["messages"] = add_or_update_system_message(
                rag_template(
                    request.app.state.config.RAG_TEMPLATE, context_string, prompt
                ),
                form_data["messages"],
            )

    # 组织sources列表
    sources = [
        source
        for source in sources
        if source.get("source", {}).get("name", "")
        or source.get("source", {}).get("id", "")
    ]

    if len(sources) > 0: #如果sources列表不为空,追加到events列表中
        events.append({"sources": sources})

    ……

        chat_completion_files_handler代码如下:

#RAG检索时的top_k,如果环境变量中未设置,则为3,request.app.state.config.TOP_K=3

RAG_TOP_K = PersistentConfig(
    "RAG_TOP_K", "rag.top_k", int(os.environ.get("RAG_TOP_K", "3"))
)

async def chat_completion_files_handler(
    request: Request, body: dict, user: UserModel
) -> tuple[dict, dict[str, list]]:
    sources = []

    if files := body.get("metadata", {}).get("files", None):
        queries = []
        try:

            '''

                generate_queries使用[多个查询的查询扩展]方法生成子查询。实际就是以用户

                输入问话查询大模型,获取针对用户输入的问题的几个子问题,子问题用于矢量库

                检索。response示例如下:

                 ----------------------------------------------------------------------------------------------------------

                 "choices":[
                      {

                        "role": "assistant",
                        "content": "<think>\n好吧,用户问北京2025年的就业情况。让我先看看之前的对话历史。上一次用户请求了一个100字以内的摘要,然后助理给出了详细的2024年的报告内容。现在用户转向2025年的情况,这可能意味着他们想要最新的数据。\n\n首先,我需要分析助理的2024年报告,看看有没有提到2025年的变化。报告中提到了深化健康建设、推进医疗体系改革等举措,这些都显示出未来的发展方向。接下来,助理还强调了数据基础设施、产业数字化转型和文化创新发展的进展,并展望了精细化治理和宜居城市建设。\n\n现在,用户问的是就业情况,这可能与健康和医疗相关,因为就业率通常会受到医疗条件的影响。然而,报告中并没有直接提到2025年的就业数字,可能需要估计或者参考其他数据源。\n\n考虑到用户的查询是关于就业的,我应该生成相关的搜索关键词。首先,结合\"北京\"和\"就业\",生成关键词:北京就业2025年、北京2025年就业率、北京医疗保障政策等。这些关键词可以满足用户的需求,但可能不够全面或精确。\n\n此外,考虑到报告中提到了深化健康建设、推进医疗体系改革等,这些都可能影响到就业情况。如果用户需要具体数据,可能会搜索\"北京2025年就业预测\"或者\"北京医疗保障政策影响\"。不过,现在数据不明确,可能需要建议用户提供更多背景信息或搜索特定领域。\n\n综上所述,我生成的两个关键词可能不够精准,但尽可能涵盖了就业和健康相关的点。如果有更多的上下文,可能会更准确。\n</think>\n\n{\n  \"queries\": [\"北京2025年就业预测\", \"北京医疗保障政策影响\"]\n}"
  },
              "done_reason": "stop",
              "done": true,
              "total_duration": 68227013645,
              "load_duration": 5589872419,
              "prompt_eval_count": 411,
              "prompt_eval_duration": 21458563342,
              "eval_count": 379,
               "eval_duration": 41175999804
            }  
         ]           
            ---------------------------------------------------------------------------------         

        '''
            queries_response = await generate_queries(
                request,
                {
                    "model": body["model"],
                    "messages": body["messages"],
                    "type": "retrieval",
                },
                user,
            )
            queries_response = queries_response["choices"][0]["message"]["content"]

            try:

               '''子查询问题为{\n \"queries\": [\"北京2025年就业预测\", \"北京医疗保障政策影响

               \"]\n},被"{"和"}"包围。以下代码得到[\"北京2025年就业预测\", \"北京医疗保障政策 

               影响\"]

               '''
                bracket_start = queries_response.find("{")
                bracket_end = queries_response.rfind("}") + 1

                if bracket_start == -1 or bracket_end == -1:
                    raise Exception("No JSON object found in the response")

                queries_response = queries_response[bracket_start:bracket_end]
                queries_response = json.loads(queries_response)
            except Exception as e:
                queries_response = {"queries": [queries_response]}

            queries = queries_response.get("queries", [])
        except:
            pass

        if len(queries) == 0:
            queries = [get_last_user_message(body["messages"])]

        try:
            # 异步检索向量库
            loop = asyncio.get_running_loop()
            with ThreadPoolExecutor() as executor:#基于前面的子查询,检索向量库
                sources = await loop.run_in_executor(
                    executor,
                    lambda: get_sources_from_files(
                        request=request,
                        files=files,
                        queries=queries,
                        embedding_function=lambda query, prefix: request.app.state.EMBEDDING_FUNCTION(
                            query, prefix=prefix, user=user
                        ),
                        k=request.app.state.config.TOP_K, #缺省为3
                        reranking_function=request.app.state.rf,
                        k_reranker=request.app.state.config.TOP_K_RERANKER,
                        r=request.app.state.config.RELEVANCE_THRESHOLD,
                        hybrid_bm25_weight=request.app.state.config.HYBRID_BM25_WEIGHT,
                        hybrid_search=request.app.state.config.ENABLE_RAG_HYBRID_SEARCH,
                        full_context=request.app.state.config.RAG_FULL_CONTEXT,
                    ),
                )
        except Exception as e:
            log.exception(e)

        log.debug(f"rag_contexts:sources: {sources}")

    

    return body, {"sources": sources}

        在chat_completion_handler(generate_chat_completion)中用process_chat_payload返回的用RAG模板+上下文+用户问题组织起来的问题调用ollama。

Logo

为武汉地区的开发者提供学习、交流和合作的平台。社区聚集了众多技术爱好者和专业人士,涵盖了多个领域,包括人工智能、大数据、云计算、区块链等。社区定期举办技术分享、培训和活动,为开发者提供更多的学习和交流机会。

更多推荐