ChatCompletionResponse.java 2.2 KB
Newer Older
小傅哥's avatar
小傅哥 已提交
1 2
package cn.bugstack.chatglm.model;

3 4 5 6
import com.alibaba.fastjson2.JSON;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
小傅哥's avatar
小傅哥 已提交
7
import lombok.Data;
8 9 10
import lombok.NoArgsConstructor;

import java.util.List;
小傅哥's avatar
小傅哥 已提交
11 12 13 14

/**
 * @author 小傅哥,微信:fustack
 * @description 返回结果
15
 * @github https://github.com/fuzhengwei/chatglm-sdk-java
小傅哥's avatar
小傅哥 已提交
16 17 18 19 20
 * @Copyright 公众号:bugstack虫洞栈 | 博客:https://bugstack.cn - 沉淀、分享、成长,让自己和他人都能有所收获!
 */
@Data
public class ChatCompletionResponse {

21
    // 旧版获得的数据方式
小傅哥's avatar
小傅哥 已提交
22 23 24
    private String data;
    private String meta;

25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
    // 24年1月发布的 GLM_3_5_TURBO、GLM_4 模型时新增
    private String id;
    private Long created;
    private String model;
    private List<Choice> choices;
    private Usage usage;

    // 封装 setChoices 对 data 属性赋值,兼容旧版使用方式
    public void setChoices(List<Choice> choices) {
        this.choices = choices;
        for (Choice choice : choices) {
            if ("stop".equals(choice.finishReason)) {
                continue;
            }
            if (null == this.data) {
                this.data = "";
            }
            this.data = this.data.concat(choice.getDelta().getContent());
        }
    }

    // 封装 setChoices 对 meta 属性赋值,兼容旧版使用方式
    public void setUsage(Usage usage) {
        this.usage = usage;
        if (null != usage) {
            this.meta = JSON.toJSONString(Meta.builder().usage(usage).build());
        }
    }

    @Data
    public static class Choice {
        private Long index;
        @JsonProperty("finish_reason")
        private String finishReason;
        private Delta delta;
    }

    @Data
    public static class Delta {
        private String role;
        private String content;
    }

小傅哥's avatar
小傅哥 已提交
68
    @Data
69 70 71
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
小傅哥's avatar
小傅哥 已提交
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
    public static class Meta {
        private String task_status;
        private Usage usage;
        private String task_id;
        private String request_id;
    }

    @Data
    public static class Usage {
        private int completion_tokens;
        private int prompt_tokens;
        private int total_tokens;
    }

}