ai接口测试

This commit is contained in:
tiansf 2025-02-24 15:28:48 +08:00
parent 4bba6518cd
commit 086c314c10
3 changed files with 379 additions and 0 deletions

View File

@ -0,0 +1,203 @@
<?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($data = ['msg'=>'']){
// dump('456');
// die;
try {
$data = input('post.');
if(!array_key_exists('msg', $data)){
return $this->msg(10001);
}
if(!$this->verify_data_is_ok($data['msg'],'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';
// DeepSeek API的端点
$apiUrl = 'https://api.deepseek.com/v1/chat/completions';
// 准备请求数据
$data = [
"messages" => [
[
"content" => "你是一个有关身体健康的专家,能根据用户提供的身体数据给出对应的分析和建议",
"role" => "system"
],
[
"content" => $xinxi['msg'],
"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
];
// 初始化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(10001);
// 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;
}
}

View File

@ -0,0 +1,165 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, initial-scale=1,minimum-scale=1, maximum-scale=1,user-scalable=no">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<meta http-equiv="Access-Control-Allow-Origin" content="*">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta name="format-detection" content="telephone=no, email=no">
<meta name="full-screen" content="true">
<meta name="screen-orientation" content="portrait">
<meta name="x5-fullscreen" content="true">
<meta name="360-fullscreen" content="true">
<title>deepseek测试页面</title>
<script src="/x_admin/js/jq.js"></script>
<style>
*{
padding: 0 0;
margin: 0 0;
}
.big_box{
width: 100vw;
height: 100vh;
position: absolute;
top: 0;
left: 0;
display: flex;
flex-direction: column;
}
.chat_box {
flex: 1;
/* overflow-y: auto; */
overflow-y: scroll;
padding: 10px;
border-bottom: 1px solid #ccc;
}
.input_box {
display: flex;
padding: 10px;
border-top: 1px solid #ccc;
}
.input_box input {
flex: 1;
padding: 10px;
border: 1px solid #ccc;
border-radius: 5px;
}
.input_box button {
margin-left: 10px;
padding: 10px 20px;
border: none;
background-color: #007bff;
color: white;
border-radius: 5px;
cursor: pointer;
}
.message {
margin: 10px 0;
display: flex;
}
.message.user {
justify-content: flex-end;
}
.message.bot {
justify-content: flex-start;
}
.message .content {
max-width: 70%;
padding: 10px;
border-radius: 5px;
background-color: #f1f0f0;
}
.message.user .content {
background-color: #dcf8c6;
}
.fugai{
display: none;
position: absolute;
width: 100vw;
height: 100vh;
text-align: center;
line-height: 100vh;
color: white;
font-size: 10vw;
font-weight: bold;
background-color: rgba(0, 0, 0, 0.5);
}
</style>
</head>
<body id="box_k">
<div class="big_box">
<div class="chat_box" id="chat_box">
<!-- 对话内容将在这里展示 -->
</div>
<div class="input_box">
<input type="text" id="message_input" placeholder="输入消息..." value="我是一个河南的25岁女性身高64.52cm体重68.00kg,请根据我的这个信息,从睡眠、饮食、运动三个方向给我一些健康建议。">
<button id="send_button">发送</button>
</div>
<div class='fugai'>思考中请稍等</div>
</div>
</body>
</html>
<script>
$(document).ready(function() {
var pd = true;
$('#send_button').click(function() {
$('.fugai').show()
if(pd == false){
return;
}
pd = false;
var message = $('#message_input').val();
if (message.trim() === '') {
return;
}
// 显示用户消息
$('#chat_box').append('<div class="message user"><div class="content">' + message + '</div></div>');
// document.getElementById('scroll_button').addEventListener('click', function() {
// var chatBox = document.getElementById('chat_box');
// chatBox.scrollTop = chatBox.scrollHeight;
// });
// 清空输入框
$('#message_input').val('');
$.ajax({
url:"https://tc.pcxbc.com/ai/send_msg_deepseek", //请求的url地址
dataType:"json", //返回格式为json
async:true,//请求是否异步默认为异步这也是ajax重要特性
data:{"msg":message}, //参数值
type:"POST", //请求方式
success:function(req){
$('.fugai').hide()
pd = true;
//请求成功时处理
if(req.code == 0){
$('#chat_box').append('<div class="message bot"><div class="content">' + req.msg + '</div></div>');
}
},
error:function(){
//请求出错处理
}});
// 模拟对方回复
// setTimeout(function() {
// var reply = '对方回复: ' + message;
// $('#chat_box').append('<div class="message bot"><div class="content">' + reply + '</div></div>');
// }, 1000);
});
// // 按下回车键发送消息
// $('#message_input').keypress(function(e) {
// console.log(e)
// // if (e.which == 13) {
// // $('#send_button').click();
// // }
// });
});
</script>

View File

@ -43,6 +43,17 @@ Route::any('/device_api_1', 'admin/device/device_request_api');
Route::any('/see_device_msg', 'admin/device/see_device_msg');
// // ################################################################AI接口处理################################################################
// // ################################################################AI接口处理################################################################
// 调用deepseek接口
Route::any('/ai/send_msg_deepseek', 'app/deepseek/send_msg_deepseek');
// ai测试页面
Route::any('/ai/test_index', 'app/deepseek/test_index');
// Route::any('/testedition/get_all_record_data_group', 'testapp/pagingcontrast/get_all_record_data_group');
// // ################################################################管理后台################################################################
// // ################################################################管理后台################################################################