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)

相关推荐
zhyhgx11 分钟前
【Spring】Spring配置文件
java·服务器·spring boot·后端·spring·配置文件
程序员JerrySUN13 分钟前
每天设计者模式-1:基础面试题
java·linux·运维·服务器·开发语言·python·docker
阿小木的愤怒20 分钟前
Spring监听器Listener
java·spring·监听器·spring监听器
johnrui36 分钟前
java8Optional 使用
java·开发语言
m0_748235951 小时前
SpringBoot:解决前后端请求跨域问题(详细教程)
java·spring boot·后端
LUCIAZZZ1 小时前
简单说一下什么是RPC
java·网络·网络协议·计算机网络·spring cloud·rpc
嘵奇1 小时前
最新版IDEA下载安装教程
java·intellij-idea
s_fox_2 小时前
Nginx Embedded Variables 嵌入式变量解析(4)
java·网络·nginx
Jelena157795857922 小时前
使用Java爬虫获取1688 item_get_company 接口的公司档案信息
java·开发语言·爬虫
数据小小爬虫2 小时前
Jsoup解析商品详情时,如何确保数据准确性?
java·爬虫