日韩精品欧美激情国产一区_中文无码精品一区二区三区在线_岛国毛片AV在线无码不卡_亞洲歐美日韓精品在線_使劲操好爽好粗视频在线播放_日韩一区欧美二区_八戒八戒网影院在线观看神马_亚洲怡红院在线色网_av无码不卡亚洲电影_国产麻豆媒体MDX

php及java調(diào)用deepseek api實(shí)例

時(shí)間:2025-03-01 23:47:32 類型:PHP
字號(hào):    

php調(diào)用deepseek api實(shí)例如下:

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>

    <style>
        .box{width: 800px; margin: 15px auto; box-shadow: 0 0 5px #ccc; padding: 15px;}
        .title { color: #333; font-size: 24px; }
        .content { line-height: 1.6; margin: 20px 0; }
        pre { background: #f5f5f5; padding: 15px; }
    </style>
</head>
<body>
    <div class="box">
        <form action="?action=search">
            <textarea name="content" id="" cols="30" rows="10"></textarea>
            <button>搜索</button>
        </form>
        <div>
            <?php
            require 'vendor/autoload.php';
            $parsedown = new Parsedown();
            if(isset($_GET['content'])){
                $begin = time();
                $content = trim($_GET['content']);
                $markdownText = getDeepSeekRes($content);
                if($markdownText){
                    $html = $parsedown->text($markdownText);
                    $length = strlen($html); // 獲取響應(yīng)長(zhǎng)度
                    for ($i = 0; $i < $length; $i++) {
                        echo $html[$i]; // 逐字輸出
                        flush(); // 刷新輸出緩沖區(qū),確保立即顯示輸出
                        usleep(100000); // 延時(shí)100毫秒,可根據(jù)需要調(diào)整速度
                    }
                }
                $end = time();
                echo "<hr>共耗時(shí):" . ($end - $begin) . "秒";
            };
            /**
             * @param $content
             * @param $model : deepseek-chat=> DeepSeek-V3
             *                 deepseek-reasoner => DeepSeek-R1
             * @return mixed
             */
            function getDeepSeekRes($content,$model='deepseek-reasoner'){
                $url = "https://api.deepseek.com/chat/completions";
                $apiKey = "sk-api key";
                $arr = [
                    'messages' => [
                        [
                            'role'=>'system',
                            'content'=>'You are a helpful assistant'
                        ],
                        [
                            'role'=>'user',
                            'content'=>$content . "【返回格式要求】:
                    1. 標(biāo)題用<h2 class='title'>
                    2. 正文用<div class='content'>
                    3. 正文段落用<p>
                    4. 代碼塊用<pre><code>"
                        ]
                    ],
                    'model' => $model,
                    'stream'=>true,
                    "frequency_penalty"=>0,
                    "max_tokens"=>2048,
                    "presence_penalty"=>0,
                    "response_format"=>[
                        "type"=>"text"
                    ],
                    "stop"=>null,
                    "stream"=>false,
                    "stream_options"=>null,
                    "temperature"=>0.5,
                    "top_p"=>1,
                    "tools"=>null,
                    "tool_choice"=>"none",
                    "logprobs"=>false,
                    "top_logprobs"=>null
                ];
                $ch = curl_init($url);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                curl_setopt($ch, CURLOPT_HTTPHEADER, [
                    'Content-Type: application/json',
                    'Authorization: Bearer ' . $apiKey
                ]);
                curl_setopt($ch, CURLOPT_POST, 1);
                curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($arr));
                curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
                curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
                $response = curl_exec($ch);
                // 檢查是否有錯(cuò)誤
                if (curl_errno($ch)) {
                    echo 'cURL 錯(cuò)誤: ' . curl_error($ch);
                }
                curl_close($ch);

                $res = json_decode($response, true);
                return isset($res["choices"][0]['message']['content']) ? $res["choices"][0]['message']['content'] : "無返回值";
            }
            ?>
        </div>
    </div>
</body>
</html>

java調(diào)用deepseek api實(shí)例

 Map<String,Object> map = new HashMap<>();
            Map<String,Object> map2 = new HashMap<>();
            map2.put("role","system");
            map2.put("content","you are a helpful assistant");
            Map<String,Object> map3 = new HashMap<>();
            map3.put("role","user");
            map3.put("content","寫一首贊美自己人見人愛花見花開的散文");
            Map[] maps = {map2,map3};
            map.put("messages",maps);
            map.put("model","deepseek-chat");
            map.put("frequency_penalty",0);
            map.put("max_tokens",2024);
            map.put("presence_penalty",0);
            Map<String,Object> map4 = new HashMap<>();
            map4.put("type","text");
            map.put("response_format",map4);
            map.put("stop",null);
            map.put("stream",false);
            map.put("stream_options",null);
            map.put("temperature",1);
            map.put("top_p",1);
            map.put("tools",null);
            map.put("tool_choice","none");
            map.put("logprobs",false);
            map.put("top_logprobs",null);
            map.put("time_out",0);

            OkHttpClient client = new OkHttpClient().newBuilder()
                    .connectTimeout(1000, TimeUnit.SECONDS) // 設(shè)置連接超時(shí)為10秒
                    .readTimeout(300, TimeUnit.SECONDS)    // 設(shè)置讀取超時(shí)為30秒
                    .build();
            MediaType mediaType = MediaType.parse("application/json");
            RequestBody body = RequestBody.create(mediaType, JSON.toJSONString(map));
            Request request = new Request.Builder()
                    .url("https://api.deepseek.com/chat/completions")
                    .post(body)
                    .addHeader("Content-Type", "application/json")
                    .addHeader("Accept", "application/json")
                    .addHeader("Authorization", "Bearer ApiKey")
                    .build();
            try (Response response = client.newCall(request).execute()) {
                if (!response.isSuccessful()) {
                    throw new IOException("Unexpected code: " + response);
                }


//                // 提取AI回復(fù)內(nèi)容
                String responseBody = response.body().string();
                System.out.println(responseBody);

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


<