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)

相关推荐
是小崔啊10 分钟前
tomcat源码02 - 理解Tomcat架构设计
java·tomcat
没有bug.的程序员27 分钟前
JAVA面试宝典 -《安全攻防:从 SQL 注入到 JWT 鉴权》
java·安全·面试
栈溢出了28 分钟前
MyBatis实现分页查询-苍穹外卖笔记
java·笔记·mybatis
morningcat201836 分钟前
java17 gc笔记
java·jvm·笔记
1 小时前
Unity开发中常用的洗牌算法
java·算法·unity·游戏引擎·游戏开发
Your易元1 小时前
设计模式-模板方法模式
java·设计模式·模板方法模式
risc1234562 小时前
Elasticsearch 线程池
java·大数据·elasticsearch
NE_STOP3 小时前
SpringBoot--如何整体读取多个配置属性及其相关操作
java·spring
apihz3 小时前
通用图片搜索-搜狗源免费API接口使用指南
android·java·python·php·音视频
风象南3 小时前
基于 SpringBoot 的 REST API 与 RPC 调用的统一封装
java·spring boot·后端