dify icon indicating copy to clipboard operation
dify copied to clipboard

Agent chat does not support knowledge base (context) retrieval

Open wangyl96 opened this issue 1 year ago • 6 comments

Self Checks

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

Dify version

master

Cloud or Self Hosted

Self Hosted (Source)

Steps to reproduce

I uploaded the knowledge base when creating the Agent chat application, but I downloaded the source code and found that the Agent chat application was not retrieved from the knowledge base. Please assist in resolving this issue

✔️ Expected Behavior

I hope to be informed whether the open-source master code has not undergone knowledge base search under agent_chat, or if there are issues with my source code reading

❌ Actual Behavior

The open-source master code does not perform knowledge base retrieval under agent_chat

wangyl96 avatar Mar 27 '25 12:03 wangyl96

@wangyl96 Hello there! I'm here to help you with bug reports, answer questions, and guide you on becoming a contributor. Feel free to share your concerns, and I'll assist you while we wait for a human maintainer.

There are a couple of similar discussions that might be relevant to your issue:

  1. After update to 1.0.0, my agent can't query the knowledgebase - This discussion is still open and might provide insights into similar problems with knowledge base retrieval after updates [1].

  2. Why cannot find required text from knowledge base in Dify? - This is another open discussion that addresses issues with finding text in the knowledge base [2].

Regarding your issue, the open-source master code for the Agent chat application does support knowledge base retrieval. The RetrievalResourceConfigManager is responsible for validating and setting defaults for retrieval resources, which includes knowledge base retrieval [3]. If you're experiencing issues, it might be due to a problem in your specific source code or configuration rather than the absence of this feature in the master code. Make sure to follow the steps outlined in the Dify documentation to ensure proper setup, including creating a knowledge base and configuring chunk settings for document segmentation [4].

To continue talking to Dosu, mention @dosu.


Help Dosu learn! Give it feedback: Great Response | Irrelevant Answer | Incorrect Sources | Too Verbose | Hallucination | Bug Report | Other

dosubot[bot] avatar Mar 27 '25 12:03 dosubot[bot]

Below, I will provide you with the DIY source code for constructing prompt words for two applications: chat assistant and agent. You will find that only the chat assistant supports knowledge base retrieval, while the agent application does not have relevant code

wangyl96 avatar Mar 27 '25 12:03 wangyl96

Chat assistant application retrieves knowledge base code location:

api/core/app/apps/chat/app_runner.py:161

        if app_config.dataset and app_config.dataset.dataset_ids:
            hit_callback = DatasetIndexToolCallbackHandler(
                queue_manager,
                app_record.id,
                message.id,
                application_generate_entity.user_id,
                application_generate_entity.invoke_from,
            )

            dataset_retrieval = DatasetRetrieval(application_generate_entity)
            context = dataset_retrieval.retrieve(
                app_id=app_record.id,
                user_id=application_generate_entity.user_id,
                tenant_id=app_record.tenant_id,
                model_config=application_generate_entity.model_conf,
                config=app_config.dataset,
                query=query,
                invoke_from=application_generate_entity.invoke_from,
                show_retrieve_source=app_config.additional_features.show_retrieve_source,
                hit_callback=hit_callback,
                memory=memory,
                message_id=message.id,
                inputs=inputs,
            )

wangyl96 avatar Mar 27 '25 12:03 wangyl96

The agent application only retrieves relevant code from external knowledge bases:

        # fill in variable inputs from external data tools if exists
        external_data_tools = app_config.external_data_variables
        if external_data_tools:
            inputs = self.fill_in_inputs_from_external_data_tools(
                tenant_id=app_record.tenant_id,
                app_id=app_record.id,
                external_data_tools=external_data_tools,
                inputs=inputs,
                query=query,
            )

        # reorganize all inputs and template to prompt messages
        # Include: prompt template, inputs, query(optional), files(optional)
        #          memory(optional), external data, dataset context(optional)
        prompt_messages, _ = self.organize_prompt_messages(
            app_record=app_record,
            model_config=application_generate_entity.model_conf,
            prompt_template_entity=app_config.prompt_template,
            inputs=dict(inputs),
            files=list(files),
            query=query or "",
            memory=memory,
        )

        # check hosting moderation
        hosting_moderation_result = self.check_hosting_moderation(
            application_generate_entity=application_generate_entity,
            queue_manager=queue_manager,
            prompt_messages=prompt_messages,
        )

        if hosting_moderation_result:
            return

        agent_entity = app_config.agent
        assert agent_entity is not None

        # init model instance
        model_instance = ModelInstance(
            provider_model_bundle=application_generate_entity.model_conf.provider_model_bundle,
            model=application_generate_entity.model_conf.model,
        )
        prompt_message, _ = self.organize_prompt_messages(
            app_record=app_record,
            model_config=application_generate_entity.model_conf,
            prompt_template_entity=app_config.prompt_template,
            inputs=dict(inputs),
            files=list(files),
            query=query or "",
            memory=memory,
        )

wangyl96 avatar Mar 27 '25 12:03 wangyl96

Image

Is the code for calculating the length of this text correct Should be changed to length_function: Callable[[list[str]], list[int]] = lambda x: [len(y) for y in x]

wangyl96 avatar Mar 28 '25 07:03 wangyl96

@crazywoola

wangyl96 avatar Mar 28 '25 10:03 wangyl96

I think that you just need to set up a model that has better support for function calls, so that you can call up the knowledge base in the agent application.

glm-4-flash: Image

glm-4: Image

ACAne0320 avatar Apr 17 '25 02:04 ACAne0320

I encountered the same problem on the differentiate-1.8.0 version. Even though my description was more accurate, the Agent still did not reference the knowledge base at all, while the chat assistant was ok. model is same! issue: Agent chat does not support knowledge base (context) retrieval,chat assist can do it #25537 @crazywoola

HokerLIU avatar Sep 11 '25 09:09 HokerLIU