Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

An error "context length exceeded" occurred during the conversation. #8360

Closed
5 tasks done
StantonStetler opened this issue Sep 13, 2024 · 4 comments
Closed
5 tasks done
Labels
🐞 bug Something isn't working

Comments

@StantonStetler
Copy link

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

Dify version

0.8.0

Cloud or Self Hosted

Self Hosted (Source)

Steps to reproduce

Create agent
Configure knowledge base
Debug dialogue chat on input box page
Error appears at the top of the page
企业微信截图_17262105991870
企业微信截图_17262107076651

✔️ Expected Behavior

Normal conversation

❌ Actual Behavior

Error occurred and no response

Copy link

dosubot bot commented Sep 13, 2024

Hello, @StantonStetler! I'm Dosu, a bot here to help you with bug reports, questions, and contributions. I'll assist you while we wait for a human maintainer.

I found some similar issues and discussions that might help resolve the "context length exceeded" error in your self-hosted Dify version 0.8.0:

  1. Max Token Limits Incorrect? - This discussion addresses issues with max token limits and might provide insights into adjusting your settings [1].
  2. Error in Dify Framework with Qwen Model: Session Exceeds Input Length Limit - This open issue suggests reducing the prefix prompt, shrinking the max token, or switching to a model with a larger token limit [2].
  3. bug: max_token limit exceeded when change llm - This open issue recommends adjusting the max_tokens parameter to a maximum of 4096 tokens to resolve similar errors [3].

To resolve the "context length exceeded" error, you can lower the value of "Max token" in the parameter settings of the Prompt Eng. Reducing the "Max token" value will help manage the context length within the acceptable range. For example, setting max_tokens=500 ensures that only the first 500 tokens of the output are considered, discarding any excess [4][5].

Additionally, the AppRunner class in Dify's codebase provides methods to handle token calculations and adjustments. The get_pre_calculate_rest_tokens method calculates the remaining tokens available for the prompt by considering the model's context size and the max_tokens parameter. If the prompt tokens plus max_tokens exceed the model's context size, an InvokeBadRequestError is raised, indicating that the prompt or max_tokens needs to be reduced. The recalc_llm_max_tokens method recalculates the max_tokens if the sum of prompt tokens and max_tokens exceeds the model's token limit, ensuring it stays within the acceptable range [6].

By adjusting these settings, you should be able to avoid the "context length exceeded" error and ensure a smoother conversation flow.

To continue talking to Dosu, mention @dosu.

@dosubot dosubot bot added the 🐞 bug Something isn't working label Sep 13, 2024
@crazywoola
Copy link
Member

Try to adjust the max_tokens to a smaller one.

@StantonStetler
Copy link
Author

Try to adjust the max_tokens to a smaller one.

The current max_token is the default 512, which is not very large, and in some cases, the length exceeds the limit. May I ask if this is due to the code? If so, which class should be modified?

@crazywoola
Copy link
Member

Then I think the prompt is too long. \

Context = prompt + memory(2k) + max_tokens

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐞 bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants