The retriever resources returned by the chatbot created using workflows and knowledge base has an incorrect "position" field. #4636

Closed
opened 2026-02-21 18:07:20 -05:00 by yindo · 1 comment
Owner

Originally created by @DayoWang on GitHub (Jul 16, 2024).

Originally assigned to: @JohnJyong on GitHub.

Self Checks

  • This is only for bug report, if you would like to ask a question, please head to Discussions.
  • I have searched for existing issues search for existing issues, including closed ones.
  • I confirm that I am using English to submit this report (我已阅读并同意 Language Policy).
  • 请务必使用英文提交 Issue,否则会被关闭。谢谢!:)
  • Please do not modify this template :) and fill in all the required fields.

Dify version

0.6.11

Cloud or Self Hosted

Self Hosted (Docker)

Steps to reproduce

The retriever resources returned by the chatbot created using workflows and knowledge base has an incorrect "position" field. In the same dataset, the returned position fields are all 1.

企业微信20240717-093523@2x 企业微信20240717-093621@2x

✔️ Expected Behavior

In the same dataset, the returned position field is sequential.

Actual Behavior

field. In the same dataset, the returned position fields are all 1.

Originally created by @DayoWang on GitHub (Jul 16, 2024). Originally assigned to: @JohnJyong on GitHub. ### Self Checks - [X] This is only for bug report, if you would like to ask a question, please head to [Discussions](https://github.com/langgenius/dify/discussions/categories/general). - [X] I have searched for existing issues [search for existing issues](https://github.com/langgenius/dify/issues), including closed ones. - [X] I confirm that I am using English to submit this report (我已阅读并同意 [Language Policy](https://github.com/langgenius/dify/issues/1542)). - [X] 请务必使用英文提交 Issue,否则会被关闭。谢谢!:) - [X] Please do not modify this template :) and fill in all the required fields. ### Dify version 0.6.11 ### Cloud or Self Hosted Self Hosted (Docker) ### Steps to reproduce The retriever resources returned by the chatbot created using workflows and knowledge base has an incorrect "position" field. In the same dataset, the returned position fields are all 1. <img width="1388" alt="企业微信20240717-093523@2x" src="https://github.com/user-attachments/assets/558b62ae-c053-4a2b-b5e4-1d9ad90d086c"> <img width="1394" alt="企业微信20240717-093621@2x" src="https://github.com/user-attachments/assets/5821e8f8-edb1-4397-ad20-24331057ea9b"> ### ✔️ Expected Behavior In the same dataset, the returned position field is sequential. ### ❌ Actual Behavior field. In the same dataset, the returned position fields are all 1.
yindo added the 🌊 feat:workflow label 2026-02-21 18:07:20 -05:00
yindo closed this issue 2026-02-21 18:07:20 -05:00
Author
Owner

@dosubot[bot] commented on GitHub (Jul 16, 2024):

The "position" field in the returned JSON structure from the chatbot is always set to 1 because it represents the position of the retriever resource within the list of retrieved segments. The current implementation does not include logic to increment this field, which is why it defaults to 1 for each retriever resource [1][2].

However, there are configuration options that could affect the behavior of the "position" field. Specifically, the self.return_resource flag and the self.retriever_from attribute influence the inclusion and content of the "position" field. The resource_number is used to set the "position" field, and it is incremented for each segment [3].

To address this issue, you may need to ensure that the resource_number is correctly incremented in your implementation. Here is a relevant code snippet that demonstrates how the resource_number should be incremented:

if self.return_resource:
    context_list = []
    resource_number = 1
    for segment in sorted_segments:
        context = {}
        document = Document.query.filter(Document.id == segment.document_id,
                                         Document.enabled == True,
                                         Document.archived == False,
                                         ).first()
        if dataset and document:
            source = {
                'position': resource_number,
                'dataset_id': dataset.id,
                'dataset_name': dataset.name,
                'document_id': document.id,
                'document_name': document.name,
                'data_source_type': document.data_source_type,
                'segment_id': segment.id,
                'retriever_from': self.retriever_from,
                'score': document_score_list.get(segment.index_node_id, None)
            }
            if self.retriever_from == 'dev':
                source['hit_count'] = segment.hit_count
                source['word_count'] = segment.word_count
                source['segment_position'] = segment.position
                source['index_node_hash'] = segment.index_node_hash
            if segment.answer:
                source['content'] = f'question:{segment.content} \nanswer:{segment.answer}'
            else:
                source['content'] = segment.content
            context_list.append(source)
        resource_number += 1

    for hit_callback in self.hit_callbacks:
        hit_callback.return_retriever_resource_info(context_list)

This code ensures that the resource_number is incremented for each segment, resulting in sequential "position" fields.

To continue talking to Dosu, mention @dosu.

@dosubot[bot] commented on GitHub (Jul 16, 2024): <!-- Answer --> The "position" field in the returned JSON structure from the chatbot is always set to 1 because it represents the position of the retriever resource within the list of retrieved segments. The current implementation does not include logic to increment this field, which is why it defaults to 1 for each retriever resource <sup>[[1]](https://github.com/langgenius/dify/blob/main/web/app/components/develop/template/template_advanced_chat.zh.mdx)</sup><sup>[[2]](https://github.com/langgenius/dify/blob/main/web/app/components/develop/template/template_chat.en.mdx)</sup>. However, there are configuration options that could affect the behavior of the "position" field. Specifically, the `self.return_resource` flag and the `self.retriever_from` attribute influence the inclusion and content of the "position" field. The `resource_number` is used to set the "position" field, and it is incremented for each segment <sup>[[3]](https://github.com/langgenius/dify/blob/main/api/core/tools/tool/dataset_retriever/dataset_retriever_tool.py)</sup>. To address this issue, you may need to ensure that the `resource_number` is correctly incremented in your implementation. Here is a relevant code snippet that demonstrates how the `resource_number` should be incremented: ```python if self.return_resource: context_list = [] resource_number = 1 for segment in sorted_segments: context = {} document = Document.query.filter(Document.id == segment.document_id, Document.enabled == True, Document.archived == False, ).first() if dataset and document: source = { 'position': resource_number, 'dataset_id': dataset.id, 'dataset_name': dataset.name, 'document_id': document.id, 'document_name': document.name, 'data_source_type': document.data_source_type, 'segment_id': segment.id, 'retriever_from': self.retriever_from, 'score': document_score_list.get(segment.index_node_id, None) } if self.retriever_from == 'dev': source['hit_count'] = segment.hit_count source['word_count'] = segment.word_count source['segment_position'] = segment.position source['index_node_hash'] = segment.index_node_hash if segment.answer: source['content'] = f'question:{segment.content} \nanswer:{segment.answer}' else: source['content'] = segment.content context_list.append(source) resource_number += 1 for hit_callback in self.hit_callbacks: hit_callback.return_retriever_resource_info(context_list) ``` This code ensures that the `resource_number` is incremented for each segment, resulting in sequential "position" fields. <!-- Dosu Comment Footer --> *To continue talking to [Dosu](https://dosu.dev), mention @dosu.*
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#4636