模型部署与调用

目录

部署

ollama下载

模型版本选择 

​编辑 对照表

控制台执行

调用


部署

大模型部署我使用的是Ollama,点击跳转

接下来我将在本地使用ollama就行模型部署的演示

ollama下载

模型版本选择 

 对照表

大家可以根据自己的显卡配置选择对应的模型版本

控制台执行

执行部署 DeepSeek-R1 模型的命令后开始下载对应的模型

        到这里本地大模型就部署成功了,大家可以看到已经能够实现deepseek对话,但我们的目的是调用api对大模型进行使用

调用

import okhttp3.*;
import org.json.JSONObject;
import java.io.IOException;

public class DeepSeekR1ApiClient {
    private static final String API_URL = "https://api.deepseek.com/v1/chat/completions";
    private static final String API_KEY = "your_api_key_here";
    private static final MediaType JSON = MediaType.get("application/json; charset=utf-8");

    public static void main(String[] args) {
        OkHttpClient client = new OkHttpClient();
        
        // 构建请求体
        JSONObject requestBody = new JSONObject();
        requestBody.put("model", "deepseek-r1");  // 指定R1模型
        
        JSONObject message = new JSONObject();
        message.put("role", "user");
        message.put("content", "你好,DeepSeek R1!");
        
        requestBody.put("messages", new JSONObject[] {message});
        
        // 可选参数
        requestBody.put("temperature", 0.7);      // 控制随机性
        requestBody.put("max_tokens", 1024);      // 最大输出token数
        
        Request request = new Request.Builder()
                .url(API_URL)
                .addHeader("Authorization", "Bearer " + API_KEY)
                .post(RequestBody.create(requestBody.toString(), JSON))
                .build();
        
        try (Response response = client.newCall(request).execute()) {
            if (!response.isSuccessful()) {
                throw new IOException("请求失败: " + response.code() + " - " + response.message());
            }
            
            String responseBody = response.body().string();
            System.out.println("API响应: " + responseBody);
            
            // 解析响应
            JSONObject jsonResponse = new JSONObject(responseBody);
            String assistantReply = jsonResponse.getJSONArray("choices")
                    .getJSONObject(0)
                    .getJSONObject("message")
                    .getString("content");
            
            System.out.println("\nAI回复: " + assistantReply);
        } catch (IOException e) {
            System.err.println("请求发生错误: " + e.getMessage());
            e.printStackTrace();
        }
    }
}

当然除了上面的调用方式,还要多种不同的其他方式: 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值