Java集成deepSeek

       <!-- 使用Apache HttpClient deepSeek -->
        <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.5.13</version>
        </dependency>
package com.ruoyi.system.controller;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

public class TestController {

    private static final String API_URL = "https://api.deepseek.com/chat/completions";
    private static final String API_KEY = "替换为实际API Key"; // 替换为实际API Key

    public static void main(String[] args) {
        try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
            HttpPost httpPost = new HttpPost(API_URL);
            httpPost.setHeader("Authorization", "Bearer " + API_KEY);
            httpPost.setHeader("Content-Type", "application/json");
            // 构建请求体(支持流式输出:stream=true)
            String jsonBody = "{" + "\"model\": \"deepseek-chat\"," + "\"messages\": [" + "{\"role\": \"system\", \"content\": \"你是一个Java技术专家\"}," + "{\"role\": \"user\", \"content\": \"如何用Java实现快速排序?\"}" + "]," + "\"stream\": false" + "}";
            httpPost.setEntity(new StringEntity(jsonBody));
            try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
                HttpEntity entity = response.getEntity();
                if (entity != null) {
                    String result = EntityUtils.toString(entity);
                    System.out.println("响应结果:" + result); // 解析JSON获取content字段
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值