SchoolPhysicalExamination/application/app/controller/Deepseek.php

210 lines
7.0 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace app\app\controller;
use think\Db;
class Deepseek extends Base{
protected $msginformation_use_db_name = [
'1'=>'admin_editor_text_content',
'2'=>'admin_editor_text_like_up_log',
'3'=>'admin_notice_banner',
'4'=>'admin_business_cooperation'
];
protected $page_num = 10;
// 加 bcadd(,,20)
// 减 bcsub(,,20)
// 乘 bcmul(,,20)
// 除 bcdiv(,,20)
################################################################接口################################################################
################################################################接口################################################################
################################################################接口################################################################
public function test_index(){
return $this->fetch();
}
// 获取板块,及板块下类型标签
public function send_msg_deepseek(){
// dump('456');
// die;
try {
$data = input('post.');
// dump($data);
// dump(array_key_exists('xinxi', $data));
if(!array_key_exists('xinxi', $data)){
return $this->msg(10001);
}
if(!$this->verify_data_is_ok($data['xinxi'],'str')){
return $this->msg(10005);
}
// $data['msg'] = "";
// dump($data);
// die;
$return_data = $this->deepseek_only_onec_action($data);
// $return_data = $this->deepseek_ceshiyongjiekou($data);
return $return_data;
} catch (\Exception $e) {
// 捕获异常
$logContent["flie"] = $e->getFile();
$logContent["line"] = $e->getLine();
$logContent['all_content'] = "异常信息:\n";
$logContent['all_content'] = "接口send_msg_deepseek\n";
$logContent['all_content'] .= "消息: " . $e->getMessage() . "\n";
$logContent['all_content'] .= "代码: " . $e->getCode() . "\n";
$logContent['all_content'] .= "文件: " . $e->getFile() . "\n";
$logContent['all_content'] .= "行号: " . $e->getLine() . "\n";
$logContent['all_content'] .= "跟踪信息:\n" . $e->getTraceAsString() . "\n";
$this->record_api_log([], $logContent, null);
return $this->msg(99999);
}
}
public function deepseek_only_onec_action($xinxi){
// DeepSeek API密钥
// $apiKey = 'sk-28dd23215ef84772b64d77011419e271';
$apiKey = 'bd739e8c-91af-40bb-85d4-0804060b2390';
// DeepSeek API的端点
// $apiUrl = 'https://api.deepseek.com/v1/chat/completions';
$apiUrl = 'https://ark.cn-beijing.volces.com/api/v3/chat/completions';
// 准备请求数据
$data = [
"messages" => [
[
"content" => "你是一个有关身体健康的专家,能根据用户提供的身体数据给出对应的分析和建议",
"role" => "system"
],
[
"content" => $xinxi['xinxi'],
"role" => "user"
]
],
// "model" => "deepseek-chat",
"model" => "ep-20250304141108-x9d7l",
"frequency_penalty" => 0,
"max_tokens" => 2048,
"presence_penalty" => 0,
"response_format" => [
"type" => "text" //指定生成文本的格式。"type": 指定格式类型,例如"text"(纯文本)或"json"JSON格式
],
"stop" => null,
"stream" => false,
"stream_options" => null,
"temperature" => 1,
"top_p" => 1,
"tools" => null,
"tool_choice" => "none",
"logprobs" => false,
"top_logprobs" => null
];
// 初始化cURL会话
$ch = curl_init();
// 设置cURL选项
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 禁用证书验证
curl_setopt($ch, CURLOPT_URL, $apiUrl); // 设置API URL
curl_setopt($ch, CURLOPT_POST, true); // 使用POST方法
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); // 设置POST数据
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Authorization: Bearer ' . $apiKey, // 设置API密钥
'Content-Type: application/json', // 设置请求头为JSON
]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 返回响应而不是直接输出
// 执行cURL请求并获取响应
$response = curl_exec($ch);
// 检查是否有cURL错误
if (curl_errno($ch)) {
echo 'cURL请求失败: ' . curl_error($ch);
} else {
// 获取HTTP状态码
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($httpCode === 200) {
// 解析JSON响应
$result = json_decode($response, true);
// print_r($result); // 输出API响应
return $this->msg(0,$result['choices'][0]['message']['content']);
// dump($result);
} else {
return $this->msg(['code'=>$httpCode]);
// echo 'API请求失败HTTP状态码: ' . $httpCode;
// echo '响应内容: ' . $response;
}
}
// 关闭cURL会话
curl_close($ch);
}
public function deepseek_ceshiyongjiekou(){
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://api.deepseek.com/chat/completions',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS =>'{
"messages": [
{
"content": "You are a helpful assistant",
"role": "system"
},
{
"content": "Hi",
"role": "user"
}
],
"model": "deepseek-chat",
"frequency_penalty": 0,
"max_tokens": 2048,
"presence_penalty": 0,
"response_format": {
"type": "text"
},
"stop": null,
"stream": false,
"stream_options": null,
"temperature": 1,
"top_p": 1,
"tools": null,
"tool_choice": "none",
"logprobs": false,
"top_logprobs": null
}',
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json',
'Accept: application/json',
'Authorization: Bearer <TOKEN>'
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
}
}