deepseek---腾讯云API接入
deepseek关闭了接口充值,所以要用deepseek只能用第三方接口,另外就是第三方接口相对比较便宜。而且像腾讯云,先送100万Token,就还是用腾讯云接口来开发。
开发接口文档:
https://cloud.tencent.com/document/product/1772/115969
我用的是PHP开发,没有示例,但是有cURL请求示例:
curl https://api.lkeap.cloud.tencent.com/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-xxxxxxxxxxx" \
-d '{
"model": "deepseek-r1",
"messages": [
{
"role": "user",
"content": "你好"
}
],
"stream": false
}'
这就很简单了:
<?php $url = 'https://api.lkeap.cloud.tencent.com/v1/chat/completions'; $key = 'xxxxxxxxxxx'; $question = '你是谁?'; // 准备请求数据 $data = [ 'model' => 'deepseek-r1', 'messages' => [ [ 'role' => 'user', 'content' => $question ] ], 'stream' => false, 'max_tokens' => 2048, 'temperature' => 0.7, 'top_p' => 0.7, 'top_k' => 50, 'frequency_penalty' => 0.5, 'n' => 1, 'response_format' => [ 'type' => 'text' ] ]; // 准备 cURL 请求 $ch = curl_init($url); curl_setopt_array($ch, [ CURLOPT_POST => true, CURLOPT_POSTFIELDS => json_encode($data), CURLOPT_RETURNTRANSFER => true, CURLOPT_HTTPHEADER => [ 'Content-Type: application/json', 'Authorization: Bearer ' . $key ], CURLOPT_WRITEFUNCTION => function($ch, $data) { echo $data; return strlen($data); } ]); // 发送请求并处理响应 $handle = curl_exec($ch); var_dump($handle); if (curl_errno($ch)) { var_dump(curl_error($ch)); }
就完事儿了。
略微出手优化一下代码:
<?php class DeepSeekChat { private $apiUrl = 'https://api.lkeap.cloud.tencent.com/v1/chat/completions'; private $apiKey; // 从环境变量获取:ml-citation{ref="1,3" data="citationList"} private $logFile = 'conversation.log'; public function __construct() { $this->apiKey = getenv('DEEPSEEK_API_KEY'); if (!file_exists($this->logFile)) { touch($this->logFile); } } public function sendMessage($message, $history = []) { $messages = array_merge( [['role' => 'system', 'content' => '你是有道AI助手']], $history, [['role' => 'user', 'content' => $message]] ); $postData = [ 'model' => 'deepseek-r1', 'messages' => $messages, 'temperature' => 1, 'max_tokens' => 100, 'stream' => false ]; return $this->executeRequest($postData); } private function executeRequest($data) { $ch = curl_init(); curl_setopt_array($ch, [ CURLOPT_URL => $this->apiUrl, CURLOPT_HTTPHEADER => [ 'Content-Type: application/json', 'Authorization: Bearer ' . $this->apiKey ], CURLOPT_POST => true, CURLOPT_POSTFIELDS => json_encode($data), CURLOPT_RETURNTRANSFER => true, CURLOPT_SSL_VERIFYPEER => true // 生产环境强制验证:ml-citation{ref="3,5" data="citationList"} ]); $response = curl_exec($ch); $this->logConversation($data, $response); if (curl_errno($ch)) { throw new Exception('API请求失败: ' . curl_error($ch)); } return $response; // return $this->parseResponse($response); } private function parseResponse($response) { $result = json_decode($response, true); if (isset($result['choices']['message']['content'])) { return $result['choices']['message']['content']; } throw new Exception('API响应解析失败'); } private function logConversation($request, $response) { $logEntry = json_encode([ 'timestamp' => date('Y-m-d H:i:s'), 'request' => $request, 'response' => json_decode($response, true) ]) . PHP_EOL; file_put_contents($this->logFile, $logEntry, FILE_APPEND); } } echo '123456789'; // 使用示例 $chat = new DeepSeekChat(); try { $reply = $chat->sendMessage("PHP如何接入DeepSeek?"); echo "AI回复: " . $reply; } catch (Exception $e) { echo "错误: " . $e->getMessage(); }
本地测试运行不了,运营是需要:CURLOPT_SSL_VERIFYPEER = true 需要线上运行,但是设为false,就可以本地运行了。
打完收工!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~