snowby666 / poe-api-wrapper

👾 A Python API wrapper for Poe.com. With this, you will have free access to GPT-4, Claude, Llama, Gemini, Mistral and more! 🚀
https://pypi.org/project/poe-api-wrapper/
GNU General Public License v3.0
831 stars 98 forks source link

Unexpected message truncation and context handling issues #187

Closed jishux2 closed 5 days ago

jishux2 commented 2 weeks ago

在官网上使用Claude-3.5-sonnet,一次性应该可以发送最多4k tokens的信息。但是在第三方客户端(如NextChat)中使用api发送携带上下文的信息时,即使只有2~4条历史信息,也经常会被截断,最终在对话中呈现出来的上下文只有不到1k tokens。

虽然不知道是什么原因,但目前我使用的替代方案是,不再在poe_api_wrapper/openai/api.py中调用__progressive_summarize_text函数:

# rest_string = await helpers.__progressive_summarize_text(
#             rest_string, min(len(rest_string), tokensLimit) 
#         )

不过这样会导致在rest_string的末尾重复包含main_request,于是还要修改这行代码:

rest_string = await helpers.__stringify_messages(messages=messages[:-1])

这样可以传入更大的上下文,但是没法处理token限制。如果有更好的方法,请让我知道

Please check #188 for my changes. Hope it helps.

jishux2 commented 2 weeks ago

Translation by Claude-3.5-sonnet:

When using Claude-3.5-sonnet on the official website, it's possible to send messages containing 4k tokens at once. However, when using the API through third-party clients (such as NextChat) to send messages with context, even with only 2-4 historical messages, the content is frequently truncated. The final context presented in the conversation often contains less than 1k tokens.

Although the cause is unclear, my current workaround is to stop calling the __progressive_summarize_text function in poe_api_wrapper/openai/api.py:

# rest_string = await helpers.__progressive_summarize_text(
#             rest_string, min(len(rest_string), tokensLimit) 
#         )

However, this approach results in rest_string repeatedly including main_request at the end. To address this, I've also modified this line of code:

rest_string = await helpers.__stringify_messages(messages=messages[:-1])

This allows for larger context to be passed, but it doesn't handle token limits. If there's a better method, please let me know.

jishux2 commented 2 weeks ago

看样子问题在于__progressive_summarize_text函数中的这一句len(text) < max_length
It appears the issue lies in the __progressive_summarize_text function, specifically with this line: len(text) < max_length

它把文本长度和最大token值比较,而实际上应该都是用token计数进行比较。
This line compares the text length with the maximum token value, whereas it should actually be comparing token counts on both sides.

len(rest_string): 7894

tokensLimit: 4000

current_tokens: 2894