Improve prompt exceed truncation logic & Try Raise exception when prompt exceed truncation for openai api#5066
Improve prompt exceed truncation logic & Try Raise exception when prompt exceed truncation for openai api#5066yhyu13 wants to merge 5 commits into
Conversation
|
I sometimes send user prompt exceeding truncation length via the opanai api. Previous method simply drop the user prompt, resulting the model generating random messages in return. Rather, send an excpetion to the client For example, the excpetion would be openai.InternalServerError Edit: Otherwise, openai client could only resolve to InternalServerError |
|
I found the openai extension does not use openai's exception classes, instead it throwing (e.g. OpenAIError, InvalidRequestError) custom ones that our openai client side won't resolve. All of them resolve to openai.InternalServerError. This might be confusin to users. We might need to refactor exception handling in the the openai extension to mimic the response of an openai api server. Edit: |
|
Just found out a typical out of context error from openai looks like below, I will adjust the truncation error similar to this later Invalid response object from API: '{"object":"error","message":"流式输出失败 {"error":{"message":"This model's maximum context length is 4097 tokens. However, you requested 4232 tokens (1232 in the messages, 3000 in the completion). Please reduce the length of the messages or completion.","type":"invalid_request_error","param":"messages","code":"context_length_exceeded"}}","code":50099}' (HTTP response code was 400) |
|
That can't be added. Long conversations in the UI would raise exceptions. It would be better to somehow truncate the user input instead of removing it if it's too long. That should be possible by changing a few lines in generate_chat_prompt. |
28076eb to
c31558f
Compare
@oobabooga Thanks for your advice, I add an conditional kwarg so that only opani api would raise excpetion on fail to truncate as that is what openai official api would do. Otherwise, the faild truncated prompt would continue for furher processes |
…dle user prompt truncation
…on logic to method
|
Closing in favor of #5439 |
Summery
When chat prompt exceed allowed length, try raise excpetion (in openai api) instead of removing any of the prompt messages.
Checklist: