transitive-bullshit / agentic

AI agent stdlib that works with any LLM and TypeScript AI SDK.
https://agentic.so
MIT License
16.22k stars 2.12k forks source link

fix: maximum context length limit #580

Closed CooperJiang closed 1 year ago

CooperJiang commented 1 year ago

There is a scenario where, assuming our model supports 4096 tokens, we set _maxModelTokens to 3096 and _maxResponseTokens to 1000. Before we check prompt && !isValidPrompt, we have already assigned a value to the message variable, which means that our context will definitely exceed the _maxModelTokens limit. If _maxModelTokens exceeds by a lot, for example, up to 3880, any subsequent response in the conversation that exceeds 400 tokens will result in a context error. Therefore, when we detect this, we need to remove the first item from the context, which is the second item in the systemMessage array, and calculate whether it still exceeds the limit. We need to recursively remove items until the context length is within our set safe limit, in order to ensure that no context errors occur.

[https://github.com/transitive-bullshit/chatgpt-api/issues/542](Make maximum context length limit )

https://github.com/transitive-bullshit/chatgpt-api/issues/542