How to set Knowledge Retrieval Top K to more than 10? #7483

Closed
opened 2026-02-21 18:20:50 -05:00 by yindo · 6 comments
Owner

Originally created by @toptalentsgpt on GitHub (Jan 1, 2025).

Self Checks

  • 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).
  • [FOR CHINESE USERS] 请务必使用英文提交 Issue,否则会被关闭。谢谢!:)
  • Please do not modify this template :) and fill in all the required fields.

1. Is this request related to a challenge you're experiencing? Tell me about your story.

How to set Knowledge Retrieval Top K to more than 10?

2. Additional context or comments

image

3. Can you help us with this feature?

  • I am interested in contributing to this feature.
Originally created by @toptalentsgpt on GitHub (Jan 1, 2025). ### Self Checks - [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] [FOR CHINESE USERS] 请务必使用英文提交 Issue,否则会被关闭。谢谢!:) - [X] Please do not modify this template :) and fill in all the required fields. ### 1. Is this request related to a challenge you're experiencing? Tell me about your story. How to set Knowledge Retrieval Top K to more than 10? ### 2. Additional context or comments ![image](https://github.com/user-attachments/assets/a9ec613f-f739-498d-b4fd-cc265c5e51b9) ### 3. Can you help us with this feature? - [ ] I am interested in contributing to this feature.
yindo added the 🙋‍♂️ question label 2026-02-21 18:20:50 -05:00
yindo closed this issue 2026-02-21 18:20:50 -05:00
Author
Owner

@dosubot[bot] commented on GitHub (Jan 1, 2025):

To set the Knowledge Retrieval Top K to more than 10 in Dify, you need to adjust the top_k parameter in the retrival_setting object of your API request. Here's how you can do it:

{
  "knowledge_id": "your-knowledge-id",
  "query": "your question",
  "retrival_setting": {
    "top_k": 15,  // Set this to the desired number greater than 10
    "score_threshold": 0.5
  }
}

Make sure to replace "your-knowledge-id" and "your question" with your actual knowledge ID and query. Adjust the top_k value to the number you require [1].

Additionally, I found a similar discussion titled "How to set the top_k for knowledge base search to be greater than 10?" which might provide further insights [2].

To continue talking to Dosu, mention @dosu.


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

@dosubot[bot] commented on GitHub (Jan 1, 2025): <!-- Answer --> To set the Knowledge Retrieval Top K to more than 10 in Dify, you need to adjust the `top_k` parameter in the `retrival_setting` object of your API request. Here's how you can do it: ```json { "knowledge_id": "your-knowledge-id", "query": "your question", "retrival_setting": { "top_k": 15, // Set this to the desired number greater than 10 "score_threshold": 0.5 } } ``` Make sure to replace `"your-knowledge-id"` and `"your question"` with your actual knowledge ID and query. Adjust the `top_k` value to the number you require <sup>[[1]](https://github.com/langgenius/dify-docs/blob/main/zh_CN/.gitbook/assets/%E5%A4%96%E9%83%A8%E7%9F%A5%E8%AF%86%E5%BA%93%20API.json)</sup>. Additionally, I found a similar discussion titled "[How to set the top_k for knowledge base search to be greater than 10?](https://github.com/langgenius/dify/discussions/10166)" which might provide further insights <sup>[[2]](https://github.com/langgenius/dify/discussions/10166)</sup>. <!-- Dosu Comment Footer --> *To continue talking to [Dosu](https://dosu.dev), mention @dosu.* --- <sup>Help Dosu learn! Give it feedback: [Great Response](https://app.dosu.dev/response-feedback/d0fdcf19-48a2-4346-b3ec-d5d1ca6d99d8?feedback_type=great_response) | [Irrelevant Answer](https://app.dosu.dev/response-feedback/d0fdcf19-48a2-4346-b3ec-d5d1ca6d99d8?feedback_type=irrelevant_answer) | [Incorrect Sources](https://app.dosu.dev/response-feedback/d0fdcf19-48a2-4346-b3ec-d5d1ca6d99d8?feedback_type=incorrect_sources) | [Too Verbose](https://app.dosu.dev/response-feedback/d0fdcf19-48a2-4346-b3ec-d5d1ca6d99d8?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/d0fdcf19-48a2-4346-b3ec-d5d1ca6d99d8?feedback_type=hallucination) | [Other](https://app.dosu.dev/response-feedback/d0fdcf19-48a2-4346-b3ec-d5d1ca6d99d8?feedback_type=other)</sup>
Author
Owner

@toptalentsgpt commented on GitHub (Jan 1, 2025):

how to set it in the .env ? @dosu

@toptalentsgpt commented on GitHub (Jan 1, 2025): how to set it in the .env ? @dosu
Author
Owner

@dosubot[bot] commented on GitHub (Jan 1, 2025):

To set the top_k value in the .env file for Dify, you would typically define an environment variable that the application reads to configure this setting. However, based on the available information, there isn't a specific environment variable mentioned for directly setting the top_k value in the .env file for knowledge retrieval.

If you want to override default settings using environment variables, you can check the docker/.env.example file in the Dify codebase, which lists environment variables that can be configured. You might need to add a custom environment variable and modify the application code to read this variable if it's not already supported [1].

If the application does not natively support setting top_k through an environment variable, you may need to adjust the code where the retrieval settings are initialized, such as in the web/app/components/datasets/settings/form/index.tsx file, and ensure it reads from an environment variable [2].

To continue talking to Dosu, mention @dosu.


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

@dosubot[bot] commented on GitHub (Jan 1, 2025): <!-- Answer --> To set the `top_k` value in the `.env` file for Dify, you would typically define an environment variable that the application reads to configure this setting. However, based on the available information, there isn't a specific environment variable mentioned for directly setting the `top_k` value in the `.env` file for knowledge retrieval. If you want to override default settings using environment variables, you can check the `docker/.env.example` file in the Dify codebase, which lists environment variables that can be configured. You might need to add a custom environment variable and modify the application code to read this variable if it's not already supported <sup>[[1]](https://github.com/langgenius/dify/blob/main/docker/.env.example)</sup>. If the application does not natively support setting `top_k` through an environment variable, you may need to adjust the code where the retrieval settings are initialized, such as in the `web/app/components/datasets/settings/form/index.tsx` file, and ensure it reads from an environment variable <sup>[[2]](https://github.com/langgenius/dify/blob/main/web/app/components/datasets/settings/form/index.tsx)</sup>. <!-- Dosu Comment Footer --> *To continue talking to [Dosu](https://dosu.dev), mention @dosu.* --- <sup>Help Dosu learn! Give it feedback: [Great Response](https://app.dosu.dev/response-feedback/255a7622-8dab-4d3d-b446-42d1ad114582?feedback_type=great_response) | [Irrelevant Answer](https://app.dosu.dev/response-feedback/255a7622-8dab-4d3d-b446-42d1ad114582?feedback_type=irrelevant_answer) | [Incorrect Sources](https://app.dosu.dev/response-feedback/255a7622-8dab-4d3d-b446-42d1ad114582?feedback_type=incorrect_sources) | [Too Verbose](https://app.dosu.dev/response-feedback/255a7622-8dab-4d3d-b446-42d1ad114582?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/255a7622-8dab-4d3d-b446-42d1ad114582?feedback_type=hallucination) | [Other](https://app.dosu.dev/response-feedback/255a7622-8dab-4d3d-b446-42d1ad114582?feedback_type=other)</sup>
Author
Owner

@toptalentsgpt commented on GitHub (Jan 2, 2025):

@crazywoola May I ask how to set the top K to 20 ? The maxmium now is 10. The reply by the bot is not working

@toptalentsgpt commented on GitHub (Jan 2, 2025): @crazywoola May I ask how to set the top K to 20 ? The maxmium now is 10. The reply by the bot is not working
Author
Owner

@Ali-Mohamed3376 commented on GitHub (Jan 5, 2025):

can any one tell me what is the knowledge_id i am trying to connect to my external api but i can not do that ?

can any one help ?

@Ali-Mohamed3376 commented on GitHub (Jan 5, 2025): can any one tell me what is the knowledge_id i am trying to connect to my external api but i can not do that ? can any one help ?
Author
Owner

@unmo commented on GitHub (May 12, 2025):

I have modified the env file to increase the TOP_K_MAX_VALUE to 100 as follows:

env

# The maximum number of top-k value for RAG.
TOP_K_MAX_VALUE=100

After this modification, the WebUI now correctly allows me to select up to 100 for the topk parameter. However, despite the selection, the actual number of chunks retrieved is still capped at 10.

Expected Behavior:

The number of retrieved chunks should align with the selected topk value, up to the specified maximum (100).

Observed Behavior:

Regardless of the topk selection, only 10 chunks are being retrieved.

Please let me know if further details or logs are needed. Thank you for your support.

Would you like me to adjust anything before you post it?

@unmo commented on GitHub (May 12, 2025): I have modified the env file to increase the TOP_K_MAX_VALUE to 100 as follows: env ```env file # The maximum number of top-k value for RAG. TOP_K_MAX_VALUE=100 ``` After this modification, the WebUI now correctly allows me to select up to 100 for the topk parameter. However, despite the selection, the actual number of chunks retrieved is still capped at 10. Expected Behavior: The number of retrieved chunks should align with the selected topk value, up to the specified maximum (100). Observed Behavior: Regardless of the topk selection, only 10 chunks are being retrieved. Please let me know if further details or logs are needed. Thank you for your support. Would you like me to adjust anything before you post it?
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#7483