langgenius / dify

Dify is an open-source LLM app development platform. Dify's intuitive interface combines AI workflow, RAG pipeline, agent capabilities, model management, observability features and more, letting you quickly go from prototype to production.
https://dify.ai
Other
51.82k stars 7.51k forks source link

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

Closed StantonStetler closed 2 months ago

StantonStetler commented 2 months ago

Self Checks

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

dosubot[bot] commented 2 months ago

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.

crazywoola commented 2 months ago

Try to adjust the max_tokens to a smaller one.

StantonStetler commented 2 months ago

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 commented 2 months ago

Then I think the prompt is too long. \

Context = prompt + memory(2k) + max_tokens