java springboot 调用 deepseek 并实例化

申请api key,有很多文章,不多说,直接上代码

复制代码
@RestController
@RequestMapping("/dsk")
public class DeepSeek {
    static String apiKey="sk-63************5f";
//    * deepseek-chat 模型已全面升级为 DeepSeek-V3,接口不变。 通过指定 model='deepseek-chat' 即可调用 DeepSeek-V3。
//    * deepseek-reasoner 是 DeepSeek 最新推出的推理模型 DeepSeek-R1。通过指定 model='deepseek-reasoner',即可调用 DeepSeek-R1。


    @GetMapping("t2")
    public String test2(){
        String base_url="https://api.deepseek.com/chat/completions";
        DeepSeekRequest content=new DeepSeekRequest();
        content.setModel("deepseek-chat");
        content.setStream(false);
        List<Message> lst=new ArrayList<Message>();
        lst.add(new Message("system","男生"));
        lst.add(new Message("user","中午吃什么!"));
        content.setMessages(lst);

        String body= HttpRequest.post(base_url).header("Authorization","Bearer "+apiKey).header("Accept", "application/json")
                .header("Content-Type","application/json").body(JSONUtil.toJsonStr(content),"application/json")
                .execute().body();
        //System.out.println(body);
        DeepSeekResponse deepSeekResponse = JSONUtil.toBean(body, DeepSeekResponse.class);
        if(null != deepSeekResponse){
            if(null !=deepSeekResponse.getError()){
                return JSONUtil.toJsonStr(deepSeekResponse.getError().getMessage());
            }
        }
        return JSONUtil.toJsonStr(deepSeekResponse.getChoices());
    }
}
复制代码
复制代码
/**
 * 模型生成的 completion 的选择列表
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Choice {
    /**
     * Possible values: [stop, length, content_filter, tool_calls, insufficient_system_resource]
     * 模型停止生成 token 的原因。
     * stop:模型自然停止生成,或遇到 stop 序列中列出的字符串。
     * length :输出长度达到了模型上下文长度限制,或达到了 max_tokens 的限制。
     * content_filter:输出内容因触发过滤策略而被过滤。
     * insufficient_system_resource:系统推理资源不足,生成被打断。
     */
    private String finish_reason;

    /**
     * 该 completion 在模型生成的 completion 的选择列表中的索引。
     */
    private Integer index;

    private List<Message> message;
}
View Code
复制代码
复制代码
@Data
@NoArgsConstructor
@AllArgsConstructor
public class DeepSeekRequest {
    /**
     * 消息列表,包含对话中的消息对象
     */
    private List<Message> messages;

    /**
     * 模型名称,指定要使用的模型  [deepseek-chat, deepseek-reasoner]
     */
    private String model;

    /**
     * 频率惩罚,用于减少重复内容的概率
     * 介于 -2.0 和 2.0 之间的数字。如果该值为正,那么新 token 会根据其在已有文本中的出现频率受到相应的惩罚,降低模型重复相同内容的可能性
     */
    private double frequency_penalty=0;

    /**
     * 最大生成的令牌数
     * 介于 1 到 8192 间的整数,限制一次请求中模型生成 completion 的最大 token 数。输入 token 和输出 token 的总长度受模型的上下文长度的限制。
     * 如未指定 max_tokens参数,默认使用 4096
     */
    private int max_tokens=4096;

    /**
     * 介于 -2.0 和 2.0 之间的数字。如果该值为正,那么新 token 会根据其是否已在已有文本中出现受到相应的惩罚,从而增加模型谈论新主题的可能性
     */
    private double presence_penalty=0;

    /**
     * 响应格式,指定返回的响应格式
     */
    private ResponseFormat response_format;

    /**
     * 停止序列,指定生成文本时的停止条件
     */
    private Object stop;

    /**
     * 是否流式返回结果
     * 如果设置为 True,将会以 SSE(server-sent events)的形式以流式发送消息增量。消息流以 data: [DONE] 结尾。
     */
    private boolean stream;

    /**
     * 流式选项,指定流式返回的选项
     * stream 为 true 时,才可设置此参数
     */
    private Object stream_options;

    /**
     * 温度,控制生成文本的随机性
     * 采样温度,介于 0 和 2 之间。更高的值,如 0.8,会使输出更随机,而更低的值,如 0.2,会使其更加集中和确定。 我们通常建议可以更改这个值或者更改 top_p,但不建议同时对两者进行修改。
     */
    private double temperature=1;

    /**
     * 核采样参数,控制生成文本的多样性
     * 作为调节采样温度的替代方案,模型会考虑前 top_p 概率的 token 的结果。所以 0.1 就意味着只有包括在最高 10% 概率中的 token 会被考虑。 我们通常建议修改这个值或者更改 temperature,但不建议同时对两者进行修改。
     */
    private double top_p=1;

    /**
     * 工具列表,指定可用的工具
     */
    private Object tools;

    /**
     * 工具选择,指定使用的工具
     */
    private String tool_choice;

    /**
     * 是否返回对数概率
     */
    private boolean logprobs;

    /**
     * 对数概率选项,指定返回的对数概率选项
     */
    private Object top_logprobs;
}
View Code
复制代码
复制代码
@Data
@NoArgsConstructor
@AllArgsConstructor
public class DeepSeekResponse {
    /**
     * 该对话的唯一标识符。
     */
    private String id;
    /**
     * API 模型名称,标识使用的模型版本。
     */
    private String model;

    /**
     * 响应创建时间,格式为 ISO 8601 标准的时间戳。
     */
    private Integer created;

    /**
     * This fingerprint represents the backend configuration that the model runs with.
     */
    private String system_fingerprint;

    private List<Choice> choices;
    private List<Usage> usages;

    private ErrorMessage error;
}
View Code
复制代码
复制代码
@Data
@NoArgsConstructor
@AllArgsConstructor
public class ErrorMessage {
    private String message;
    private String type;
    private String param;
    private String code;
}
View Code
复制代码
复制代码
 1 @Data
 2 @NoArgsConstructor
 3 @AllArgsConstructor
 4 public class Message {
 5     /**
 6      * 消息的角色,例如 "system" 或 "user"。
 7      */
 8     private String role;
 9 
10     /**
11      * 消息的具体内容。
12      */
13     private String content;
14 
15     /**
16      * 仅适用于 deepseek-reasoner 模型。内容为 assistant 消息中在最终答案之前的推理内容
17      */
18     //private String reasoning_content;
19 }
Message
复制代码
复制代码
 1 /**
 2  * 响应格式对象,指定返回的响应格式类型
 3  */
 4 @Data
 5 @AllArgsConstructor
 6 @NoArgsConstructor
 7 public class ResponseFormat {
 8     /**
 9      * 响应格式类型,例如 "text"
10      */
11     private String type;
12 }
format
复制代码
复制代码
 1 /**
 2  * 该对话补全请求的用量信息
 3  */
 4 @Data
 5 @NoArgsConstructor
 6 @AllArgsConstructor
 7 public class Usage {
 8     /**
 9      * 模型 completion 产生的 token 数。
10      */
11     private Integer completion_tokens;
12 
13     /**
14      * 用户 prompt 所包含的 token 数。该值等于 prompt_cache_hit_tokens + prompt_cache_miss_tokens
15      */
16     private Integer prompt_tokens;
17 
18     /**
19      * 用户 prompt 中,命中上下文缓存的 token 数。
20      */
21     private Integer prompt_cache_hit_tokens;
22 
23     /**
24      * 用户 prompt 中,未命中上下文缓存的 token 数。
25      */
26     private Integer prompt_cache_miss_tokens;
27 
28     /**
29      * 该请求中,所有 token 的数量(prompt + completion)
30      */
31     private Integer total_tokens;
32 }
Usage
复制代码

 

posted @   jamin  阅读(1203)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· 上周热点回顾(2.17-2.23)
· 如何使用 Uni-app 实现视频聊天(源码,支持安卓、iOS)
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
点击右上角即可分享
微信分享提示