瞭解您想用Java同時傳遞圖片和文字信息給DeepSeek API。雖然目前的搜索結果中缺乏直接使用DeepSeek多模態接口處理圖片的Java示例,但我可以基於通用的大模型多模態API調用原理,為您提供一個可行的實現方案。

以下是一個基於OkHttp庫的示例,展示瞭如何構建同時包含文本和圖像數據的請求。

核心概念:圖像傳遞方式

通常,向大模型傳遞圖像有兩種方式:

  • 圖像URL:提供公網可訪問的圖片鏈接
  • Base64編碼:將圖像文件轉換為Base64字符串直接嵌入請求

Java代碼示例

1. 依賴配置(Maven)

<dependencies>
    <dependency>
        <groupId>com.squareup.okhttp3</groupId>
        <artifactId>okhttp</artifactId>
        <version>4.11.0</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.15.2</version>
    </dependency>
</dependencies>

2. 完整可用的Java類

import okhttp3.*;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Base64;
import java.util.concurrent.TimeUnit;

public class DeepSeekMultimodalClient {
    private static final String API_URL = "https://api.deepseek.com/v1/chat/completions";
    private static final String API_KEY = "您的API密鑰"; // 請替換為實際密鑰
    
    private final OkHttpClient client;
    private final ObjectMapper mapper;
    
    public DeepSeekMultimodalClient() {
        this.client = new OkHttpClient.Builder()
                .connectTimeout(30, TimeUnit.SECONDS)
                .readTimeout(60, TimeUnit.SECONDS)
                .build();
        this.mapper = new ObjectMapper();
    }
    
    /**
     * 將圖像文件轉換為Base64字符串
     */
    private String imageToBase64(String imagePath) throws IOException {
        byte[] imageBytes = Files.readAllBytes(Paths.get(imagePath));
        return Base64.getEncoder().encodeToString(imageBytes);
    }
    
    /**
     * 構建多模態請求體(包含文本和圖像)
     */
    private String buildMultimodalRequest(String textMessage, String imagePath) throws IOException {
        ObjectNode requestBody = mapper.createObjectNode();
        
        // 設置模型參數 - 請根據DeepSeek實際支持的多模態模型名稱調整
        requestBody.put("model", "deepseek-multimodal"); // 模型名稱可能需要調整
        
        // 構建消息數組
        var messagesArray = mapper.createArrayNode();
        var messageObject = mapper.createObjectNode();
        
        // 設置角色
        messageObject.put("role", "user");
        
        // 構建內容數組(同時包含文本和圖像)
        var contentArray = mapper.createArrayNode();
        
        // 1. 文本部分
        var textContent = mapper.createObjectNode();
        textContent.put("type", "text");
        textContent.put("text", textMessage);
        contentArray.add(textContent);
        
        // 2. 圖像部分(Base64編碼)
        if (imagePath != null && !imagePath.isEmpty()) {
            try {
                String base64Image = imageToBase64(imagePath);
                var imageContent = mapper.createObjectNode();
                imageContent.put("type", "image_url");
                
                var imageUrlObject = mapper.createObjectNode();
                imageUrlObject.put("url", "data:image/jpeg;base64," + base64Image);
                imageContent.set("image_url", imageUrlObject);
                
                contentArray.add(imageContent);
            } catch (IOException e) {
                System.err.println("圖像處理失敗: " + e.getMessage());
                // 可根據需要決定是否拋出異常或繼續僅文本處理
            }
        }
        
        messageObject.set("content", contentArray);
        messagesArray.add(messageObject);
        
        requestBody.set("messages", messagesArray);
        requestBody.put("max_tokens", 2048);
        requestBody.put("temperature", 0.7);
        
        return requestBody.toString();
    }
    
    /**
     * 發送請求到DeepSeek API
     */
    public String sendRequest(String textMessage, String imagePath) {
        try {
            // 構建請求體
            String requestBody = buildMultimodalRequest(textMessage, imagePath);
            System.out.println("請求體: " + requestBody);
            
            RequestBody body = RequestBody.create(
                requestBody, 
                MediaType.parse("application/json")
            );
            
            // 構建請求
            Request request = new Request.Builder()
                    .url(API_URL)
                    .post(body)
                    .addHeader("Content-Type", "application/json")
                    .addHeader("Authorization", "Bearer " + API_KEY)
                    .addHeader("Accept", "application/json")
                    .build();
            
            // 發送請求
            try (Response response = client.newCall(request).execute()) {
                if (response.isSuccessful() && response.body() != null) {
                    String responseBody = response.body().string();
                    System.out.println("完整響應: " + responseBody);
                    
                    // 解析響應提取內容
                    ObjectNode jsonResponse = (ObjectNode) mapper.readTree(responseBody);
                    if (jsonResponse.has("choices") && jsonResponse.get("choices").size() > 0) {
                        String content = jsonResponse.get("choices")
                                .get(0)
                                .get("message")
                                .get("content")
                                .asText();
                        return content;
                    }
                } else {
                    String errorBody = response.body() != null ? response.body().string() : "無響應體";
                    throw new IOException("API請求失敗: " + response.code() + " - " + errorBody);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
            return "請求處理失敗: " + e.getMessage();
        }
        return null;
    }
    
    // 使用示例
    public static void main(String[] args) {
        DeepSeekMultimodalClient client = new DeepSeekMultimodalClient();
        
        // 示例1: 僅文本請求
        String textOnlyResponse = client.sendRequest("請描述這張圖片的內容", "");
        System.out.println("文本響應: " + textOnlyResponse);
        
        // 示例2: 文本+圖像請求
        String multimodalResponse = client.sendRequest(
            "請分析這張圖片並回答:圖片中主要是什麼內容?有什麼特點?", 
            "path/to/your/image.jpg" // 替換為實際圖像路徑
        );
        System.out.println("多模態響應: " + multimodalResponse);
    }
}

關鍵配置説明

1. 模型名稱

代碼中的 deepseek-multimodal是佔位符,請根據DeepSeek官方文檔使用正確的多模態模型名稱。

2. 圖像格式支持

確保使用支持的圖像格式(通常包括JPEG、PNG、WEBP等),並注意文件大小限制。

3. 錯誤處理

代碼包含了基本的錯誤處理,在實際應用中您可能需要:

  • •添加重試機制
  • •實現更細緻的異常分類處理
  • •添加日誌記錄和監控

備選方案:URL方式傳遞圖像

如果希望使用圖像URL而非Base64編碼,可以修改請求構建部分:

// 替換Base64部分為URL方式
var imageContent = mapper.createObjectNode();
imageContent.put("type", "image_url");

var imageUrlObject = mapper.createObjectNode();
imageUrlObject.put("url", "https://example.com/your-image.jpg"); // 公網可訪問的URL
imageContent.set("image_url", imageUrlObject);

重要提醒

  1. 1.驗證API支持:在使用前請確認DeepSeek API是否已開放多模態功能支持
  2. 2.查閲最新文檔:模型名稱、參數格式可能更新,請以官方文檔為準
  3. 3.安全實踐:妥善管理API密鑰,避免硬編碼在代碼中

這個示例提供了一個堅實的技術起點,您可以根據DeepSeek具體的API規範進行調整。如果需要更具體的幫助,請提供更多關於您的使用場景的細節。