DeepSeek模型集成到java中使用(阿里云版)超简单版

DeepSeek开源后,AI的发展将会日新月异.以前局限于AI高门槛限制了很多开发者介入,但是开源后整个格局就变了. 广大开发者会将AI无孔不入的融入到社会的各个层面.这篇文章将通过阿里云的AI模型将DeepSeek集成到java项目中.

1:在阿里云创建自己的key

点进链接:阿里云百炼 (aliyun.com) 创建一下key.

2: 在java项目中引入jar依赖

复制代码
  <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dashscope-sdk-java</artifactId>
            <version>2.18.2</version>
        </dependency>

3: 创建对话DeepSeek的代码

java 复制代码
package com.example.ai_ds;// dashscope SDK的版本 >= 2.18.2
import java.util.Arrays;
import java.lang.System;
import com.alibaba.dashscope.aigc.generation.Generation;
import com.alibaba.dashscope.aigc.generation.GenerationParam;
import com.alibaba.dashscope.aigc.generation.GenerationResult;
import com.alibaba.dashscope.common.Message;
import com.alibaba.dashscope.common.Role;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;

public class DsTest {
    public static GenerationResult callWithMessage() throws ApiException, NoApiKeyException, InputRequiredException {
        Generation gen = new Generation();
        Message userMsg = Message.builder()
                .role(Role.USER.getValue())
                .content("你是谁?")
                .build();
        GenerationParam param = GenerationParam.builder()
              //注意!!!! 这个地方需要改成你自己的key
                .apiKey("这个是第一步你获取的阿里云key")
                .model("deepseek-r1")
                .messages(Arrays.asList(userMsg))
                .resultFormat(GenerationParam.ResultFormat.MESSAGE)
                .build();
        return gen.call(param);
    }
    public static void main(String[] args) {
        try {
            GenerationResult result = callWithMessage();
            System.out.println("思考过程:");
            System.out.println(result.getOutput().getChoices().get(0).getMessage().getReasoningContent());
            System.out.println("回复内容:");
            System.out.println(result.getOutput().getChoices().get(0).getMessage().getContent());
        } catch (ApiException | NoApiKeyException | InputRequiredException e) {
            // 使用日志框架记录异常信息
            System.err.println("An error occurred while calling the generation service: " + e.getMessage());
        }
        System.exit(0);
    }
}

4:接入完成完成.开始对话试一下

这个只是最简单的集成deepSeek进行对话.只适合聊天使用.如果需要定制训练AI.还需要进一步查看阿里云官方文档 和deepSeel官方文档.

deepSeek官方文档: 首次调用 API | DeepSeek API Docs

阿里云对接deepSeek文档: DeepSeek R1和DeepSeek V3 API_大模型服务平台百炼(Model Studio)-阿里云帮助中心 (aliyun.com)

相关推荐
DKPT17 分钟前
JVM直接内存和堆内存比例如何设置?
java·jvm·笔记·学习·spring
weixin_4365250735 分钟前
若依 - idea集成docker一键部署springboot项目(docker-compose)
java·1024程序员节
鼠鼠我捏,要死了捏36 分钟前
深入解析Java GC调优:从原理到实战
java·性能优化·gc调优
Live&&learn37 分钟前
Tomcat 10和Tomcat 9引入servlet的不同
java·servlet·tomcat
siriuuus37 分钟前
JVM 垃圾收集器相关知识总结
java·jvm
weixin_4365250743 分钟前
Windows - Maven 安装到 IDEA 配置全流程
java·maven·intellij-idea
启山智软44 分钟前
APS系统适合哪些行业或企业规模
java·商城开发
武子康1 小时前
DeepSeek-OCR 原理剖析|上下文光学压缩、Gundam 动态分辨率与并发预期 附代码
深度学习·aigc·deepseek
在等晚安么1 小时前
记录自己写项目的第三天,springbot+redis+rabbitma高并发项目
java·spring boot·redis·1024程序员节
OkGogooXSailboat1 小时前
flume的log4j日志无输出排查
java·flume·1024程序员节