openai-java icon indicating copy to clipboard operation
openai-java copied to clipboard

chat 对话3-4个轮次后报tokens超出4096个

Open babi-hh opened this issue 2 years ago • 3 comments

每当超过3-4次对话的时候, 就会报异常

请求参数

[{"content":"You are a helpful assistant.","role":"system"},{"content":"Who won the world series in 2020?","role":"user"},{"content":"The Los Angeles Dodgers won the 2020 World Series.","role":"assistant"},{"content":"Where was it played?","role":"user"},{"content":"The 2020 World Series was played at Globe Life Field in Arlington, Texas.","role":"assistant"},{"content":"How many people watched at the scene?","role":"user"},{"content":"Due to the COVID-19 pandemic, the 2020 World Series had a limited number of fans in attendance. The highest attendance during the series was 11,437 spectators for Game 1.","role":"assistant"},{"content":"ok, thank you very much","role":"user"}]

响应内容 com.theokanning.openai.OpenAiHttpException: This model's maximum context length is 4096 tokens. However, you requested 4146 tokens (146 in the messages, 4000 in the completion). Please reduce the length of the messages or completion.

依赖版本

<dependency>
    <groupId>com.theokanning.openai-gpt3-java</groupId>
    <artifactId>service</artifactId>
    <version>0.11.0</version>
</dependency>

java 代码片段

log.info("chat dialog receive, sessionId:{}, question: {}", sessionId, question);
        List<ChatMessage> messageContext = ChatDialogStorage.buildDialogueContext(question);
        ChatCompletionRequest chatCompletionRequest = ChatCompletionRequest
                .builder()
                .model(MODEL_TURBO)
                .messages(messageContext)
                .temperature(0.2D)
                .maxTokens(4000)
                .frequencyPenalty(0.5D)
                .presencePenalty(0D)
                .build();
        try {
            List<ChatCompletionChoice> choices = service.createChatCompletion(chatCompletionRequest).getChoices();

当我使用PostMan的时候一切正常!

babi-hh avatar Mar 03 '23 06:03 babi-hh

应该就是这代码的问题,用老版本的 openAiService 不存在这个问题

Xsir0 avatar Mar 04 '23 06:03 Xsir0

It is because you have set up the maxToken as 4000. remove this setting you should be fine

mzhu-ai avatar Mar 04 '23 07:03 mzhu-ai

the chatgpt max 4096 token

mqd273 avatar Mar 21 '23 06:03 mqd273