Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

有多轮会话的例子吗 #33

Open
hellozhihua opened this issue Feb 5, 2025 · 2 comments
Open

有多轮会话的例子吗 #33

hellozhihua opened this issue Feb 5, 2025 · 2 comments

Comments

@hellozhihua
Copy link

No description provided.

@LnYo-Cly
Copy link
Owner

    @Test
    public void test_chatCompletions_history() throws Exception {
        List<ChatMessage> history = new ArrayList<>();

        ChatCompletion chatCompletion = ChatCompletion.builder()
                .model("gpt-4o-mini")
                .message(ChatMessage.withUser("鲁迅为什么打周树人"))
                .build();

        System.out.println("请求参数");
        System.out.println(chatCompletion);

        // 向历史中添加刚刚问过的消息
        history.add(chatCompletion.getMessages().get(chatCompletion.getMessages().size()-1));

        ChatCompletionResponse chatCompletionResponse = chatService.chatCompletion(chatCompletion);

        System.out.println("请求成功");
        System.out.println(chatCompletionResponse.getChoices().get(0).getMessage());
        // 将返回的消息添加到历史中
        history.add(chatCompletionResponse.getChoices().get(0).getMessage());


        // 开始第二次问答
        history.add(ChatMessage.withUser("我刚刚问了什么问题"));
        ChatCompletion chatCompletionWithHistory = ChatCompletion.builder()
                .model("gpt-4o-mini")
                .messages(history)
                .build();
        ChatCompletionResponse chatCompletionResponseWithHistory = chatService.chatCompletion(chatCompletionWithHistory);

        System.out.println("请求成功");
        System.out.println(chatCompletionResponseWithHistory);

    }

上述只是一个例子,你需要根据你的应用场景进行灵活变通修改。

@Kingyumu
Copy link

@test
public void test_chatCompletions_history() throws Exception {
List history = new ArrayList<>();

    ChatCompletion chatCompletion = ChatCompletion.builder()
            .model("gpt-4o-mini")
            .message(ChatMessage.withUser("鲁迅为什么打周树人"))
            .build();

    System.out.println("请求参数");
    System.out.println(chatCompletion);

    // 向历史中添加刚刚问过的消息
    history.add(chatCompletion.getMessages().get(chatCompletion.getMessages().size()-1));

    ChatCompletionResponse chatCompletionResponse = chatService.chatCompletion(chatCompletion);

    System.out.println("请求成功");
    System.out.println(chatCompletionResponse.getChoices().get(0).getMessage());
    // 将返回的消息添加到历史中
    history.add(chatCompletionResponse.getChoices().get(0).getMessage());


    // 开始第二次问答
    history.add(ChatMessage.withUser("我刚刚问了什么问题"));
    ChatCompletion chatCompletionWithHistory = ChatCompletion.builder()
            .model("gpt-4o-mini")
            .messages(history)
            .build();
    ChatCompletionResponse chatCompletionResponseWithHistory = chatService.chatCompletion(chatCompletionWithHistory);

    System.out.println("请求成功");
    System.out.println(chatCompletionResponseWithHistory);

}

上述只是一个例子,你需要根据你的应用场景进行灵活变通修改。

如果集成的是deepseek,添加上次的回答时,需要删除回答的推理,不然会报400

Image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants