如何用Windows Power Shell直接调用Deep Seek

如何用Windows Power Shell直接调用Deep Seek

# 设置API密钥和端点 $apiKey = "your-api-key-here" $apiUrl = "https://api.deepseek.com/v1/chat/completions" # 设置请求头 $headers = @{ "Content-Type" = "application/json" "Authorization" = "Bearer $apiKey" } # 构造请求体 $body = @{ model = "deepseek-chat" messages = @( @{ role = "user" content = "你好,请介绍一下你自己" } ) temperature = 0.7 } | ConvertTo-Json try { # 发送POST请求 $response = Invoke-RestMethod -Uri $apiUrl -Method Post -Headers $headers -Body $body # 解析并显示响应 $reply = $response.choices.message.content Write-Host "回复:`n$reply" } catch { Write-Host "请求失败:$_" $errorDetails = $_.ErrorDetails.Message | ConvertFrom-Json Write-Host "错误信息:$($errorDetails.error.message)" }

注意事项:

替换 $apiKey 为你的实际API密钥

需要PowerShell 5.1或更高版本

确保你的网络可以访问DeepSeek API

根据API文档调整模型名称和参数

错误处理包含:

网络请求异常捕获

API错误信息解析

响应数据结构解析