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

Feature/limit fix: maximum context length limit #580 #581

Closed CooperJiang closed 3 months 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/https://github.com/transitive-bullshit/chatgpt-api/issues/542](Make maximum context length limit )

transitive-bullshit commented 3 months ago

This project is undergoing a major revamp; closing out old PRs as part of the prep process.

This was a good change, but the chatgpt package is pretty outdated at this point. I recommend that you use the openai package or the openai-fetch package.