使用JAVA对接Deepseek API实现首次访问和提问

一、标题

使用JAVA对接Deepseek API实现首次访问和

提问:我有50万能做什么小本生意,举例3个!

二、代码

复制代码
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * 王子威
 */
public class DeepSeekApiExample {
    public static void main(String[] args) {
        String apiKey = "USER_KEY"; // Replace with your actual API key
        String apiUrl = "https://api.deepseek.com/chat/completions";

        try {
            // Create the URL object
            URL url = new URL(apiUrl);

            // Open a connection
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();

            // Set the request method and headers
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Content-Type", "application/json");
            connection.setRequestProperty("Authorization", "Bearer " + apiKey);
            connection.setDoOutput(true);

            // Create the request body
            String requestBody = "{"
                    + "\"model\": \"deepseek-chat\","
                    + "\"messages\": ["
                    + "    {\"role\": \"user\", \"content\": \"我有50万能做什么小本生意,举例3个!\"}"
                    + "],"
                    + "\"stream\": false"
                    + "}";

            // Write the request body
            try(OutputStream os = connection.getOutputStream()) {
                byte[] input = requestBody.getBytes("utf-8");
                os.write(input, 0, input.length);
            }

            // Get the response code
            int responseCode = connection.getResponseCode();
            System.out.println("Response Code: " + responseCode);

            // Read the response
            try(BufferedReader br = new BufferedReader(
                    new InputStreamReader(connection.getInputStream(), "utf-8"))) {
                StringBuilder response = new StringBuilder();
                String responseLine;
                while ((responseLine = br.readLine()) != null) {
                    response.append(responseLine.trim());
                }
                System.out.println("Response: " + response.toString());
            }

            // Close the connection
            connection.disconnect();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

三、结果

执行结果返回Json

复制代码
{
  "id": "14afeac2-32a3-4007-ab4e-7845bb2229d0",
  "object": "chat.completion",
  "created": 1746697550,
  "model": "deepseek-chat",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "根据50万元的启动资金和当前市场趋势,以下是三个适合的小本生意建议,涵盖不同行业和运营模式,供参考:\n\n---\n\n### **1. 社区精品咖啡店(轻餐饮+社交空间)**\n  - **投入**:30-40万元(装修15万+设备10万+租金押金5万+备用金)  \n  - **模式**:  \n    - 选址在年轻人群聚集的社区或写字楼周边,主打高性价比现磨咖啡和轻食(如贝果、三明治)。  \n    - 差异化:提供“咖啡订阅卡”(月费模式)、周末主题沙龙(读书会/手冲体验)增加粘性。  \n    - 控制成本:采用二手设备、与本地烘焙坊合作供应简餐。  \n  - **优势**:餐饮刚需+社区经济红利,复购率高。  \n\n---\n\n### **2. 智能家居安装服务(科技+服务行业)**\n  - **投入**:20-30万元(代理费10万+工具5万+宣传5万+备用金)  \n  - **模式**:  \n    - 代理小米、华为等品牌的智能家居产品(如智能锁、灯光系统),提供“销售+安装+售后”一站式服务。  \n    - 目标客户:新装修家庭、中小型民宿业主。  \n    - 轻资产运营:无需门店,线上接单(抖音/小红书宣传),按项目收费。  \n  - **优势**:智能家居市场年增长超20%,技术服务溢价高。  \n\n---\n\n### **3. 宠物鲜食烘焙工坊(健康宠物经济)**\n  - **投入**:15-25万元(厨房改造5万+资质办理3万+原料库存5万+营销2万)  \n  - **模式**:  \n    - 生产无添加宠物零食(如鸡肉干、蔬菜饼干),通过私域社群和快团团分销。  \n    - 定制服务:为高端客户提供宠物生日蛋糕、营养配餐方案。  \n    - 合作渠道:与宠物医院、美容店分佣代销。  \n  - **优势**:宠物食品赛道需求稳定,毛利率可达50%以上。  \n\n---\n\n### **选择建议**:\n- **求稳**:选咖啡店(需考察选址和竞争)。  \n- **趋势型**:智能家居(依赖本地装修市场活跃度)。  \n- **兴趣驱动**:宠物烘焙(适合热爱动物、擅长社交电商者)。  \n\n**关键提醒**:无论选择哪个方向,建议预留20%资金作为周转,并先通过小范围试运营(如快闪店、样品测试)验证市场需求再扩大投入。"
      },
      "logprobs": null,
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 15,
    "completion_tokens": 567,
    "total_tokens": 582,
    "prompt_tokens_details": {
      "cached_tokens": 0
    },
    "prompt_cache_hit_tokens": 0,
    "prompt_cache_miss_tokens": 15
  },
  "system_fingerprint": "fp_8802369eaa_prod0425fp8"
}

四、扣费记录