欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python通過(guò)curl實(shí)現(xiàn)訪問(wèn)deepseek的API

 更新時(shí)間:2025年05月14日 10:34:38   作者:qq_1418269732  
這篇文章主要為大家詳細(xì)介紹了python如何通過(guò)curl實(shí)現(xiàn)訪問(wèn)deepseek的API,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下

API申請(qǐng)和充值

下面是deepeek的API網(wǎng)站

https://platform.deepseek.com/

進(jìn)去先注冊(cè),是不是手機(jī)賬號(hào)密碼都不重要,都一樣,完事充值打米,主要是打米后左側(cè)API Keys里面創(chuàng)建一個(gè)API Keys,注意自己手抄一個(gè)Key,那個(gè)你自己完了也打不開(kāi)而是一堆******,記不住只能重新生成

本地curl訪問(wèn)代碼腳本

部分參數(shù)意義和選項(xiàng)在代碼里標(biāo)注了

import subprocess
import json
import os

def call_deepseek_api(prompt,
    api_key="sk-0d83************f3a3461486",
    model="deepseek-chat",
    temperature=0.7,
    max_tokens=1000
    ):
    """
    使用cURL調(diào)用DeepSeek API
    
    參數(shù):
    - prompt: 提示文本
    - api_key: DeepSeek API密鑰,如果未提供則從環(huán)境變量獲取
    - model: 要使用的模型名稱(chēng)
        通過(guò)指定 model='deepseek-chat' 即可調(diào)用 DeepSeek-V3。
        通過(guò)指定 model='deepseek-reasoner',即可調(diào)用 DeepSeek-R1。
    - temperature: 控制隨機(jī)性的溫度參數(shù)(隨機(jī)性,越低越選擇概率高的答案,最高1,最低0,0.7時(shí)均衡,0.2時(shí)死板,1時(shí)靈活)
    - max_tokens: 生成的最大token數(shù)(計(jì)費(fèi)是通過(guò)token,模型的分詞器(Tokenizer)決定,粗略的說(shuō),1 個(gè)單詞 ≈ 1.3 個(gè) Token,1 個(gè)漢字 ≈ 1~1.5 個(gè) Token,單次費(fèi)用=輸入token*0.0001+輸出token*0.0003    
    返回:
    - API響應(yīng)的JSON解析結(jié)果
    """
    # 如果未提供API密鑰,則從環(huán)境變量獲取
    if api_key is None:
        api_key = os.environ.get("DEEPSEEK_API_KEY")
    
    if not api_key:
        raise ValueError("需要提供DeepSeek API密鑰")
    
    # 構(gòu)建API請(qǐng)求的JSON數(shù)據(jù)
    request_data = {
        "model": model,
        "messages": [{"role": "user", "content": prompt}],
        "temperature": temperature,
        "max_tokens": max_tokens,
        "stream":False
    }
    
    # 構(gòu)建cURL命令
    #這里可能存在一個(gè)問(wèn)題,訪問(wèn)地址可能是"https://api.deepseek.com/chat/completions"
    curl_cmd = [
        "curl",
        "-X", "POST",
        "https://api.deepseek.com/v1/chat/completions",
        "-H", f"Authorization: Bearer {api_key}",
        "-H", "Content-Type: application/json",
        "-d", json.dumps(request_data)
    ]
    
    try:
        # 執(zhí)行cURL命令
        result = subprocess.run(
            curl_cmd,
            capture_output=True,
            text=True,
            encoding='utf-8', 
            check=True
        )
        
        # 解析JSON響應(yīng)
        response = json.loads(result.stdout)
        return response
    
    except subprocess.CalledProcessError as e:
        print(f"API請(qǐng)求失敗: {e.stderr}")
        raise
    except json.JSONDecodeError:
        print(f"無(wú)法解析API響應(yīng): {result.stdout}")
        raise

# 使用示例
if __name__ == "__main__":
    # 方式1: 通過(guò)環(huán)境變量設(shè)置API密鑰
    # os.environ["DEEPSEEK_API_KEY"] = "your_api_key_here"
    
    # 方式2: 直接在函數(shù)調(diào)用中提供API密鑰
    api_key = "sk-0d8*******f3a3461486"
    
    # 調(diào)用API
    try:
        response = call_deepseek_api(
            prompt="你好,請(qǐng)介紹一下你自己",
            api_key=api_key
        )
        
        # 打印API返回的內(nèi)容
        if "choices" in response and len(response["choices"]) > 0:
            message = response["choices"][0]["message"]["content"]
            print("API響應(yīng):")
            print(message)
        else:
            print("API返回格式異常:", response)
    
    except Exception as e:
        print(f"發(fā)生錯(cuò)誤: {e}")

這里是上面代碼嘗試跑起來(lái)的結(jié)果

到此這篇關(guān)于python通過(guò)curl實(shí)現(xiàn)訪問(wèn)deepseek的API的文章就介紹到這了,更多相關(guān)python訪問(wèn)deepseek API內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論