作者 zhl

GEO分析是否一致

... ... @@ -119,24 +119,23 @@ class GeoQuestionRes extends Command
$hit_data = array_merge($url, $title, $hit_data);
}
$hit = 0;
//todo::与预期结果是否复合
if(!empty($taskInfo['expect_result'])){
$str = "客户提出的问题:{$question},客户得到的回复:{$result['text']},客户需要预期:{$taskInfo['expect_result']},请分析得到的回复和预期是否一致,仅回复我是或者否";
$strResult = $geo_service->getChatResult($str, 'gpt-4o-mini');
if(isset($strResult['text']) && !empty($strResult['text'])){
switch ($strResult['text']){
case '是':
$is_match = 1;
$hit++;
break;
case '否':
$is_match = 2;
break;
default:
$is_match = 0;
break;
}
$is_match = 0;
$cosine = 0;
$similarity = [];
// TODO 有预期结果,分析答案和预期结果
if(FALSE == empty($taskInfo['expect_result'])){
$cosine_result = $geo_service->cosineSimilarity($taskInfo['expect_result'], $result['text']);
// 语义是否一致
if (FALSE == empty($cosine_result['judgement'])) {
$is_match = $cosine_result['judgement'] == '语义相近' ? 1 : 2;
$hit++;
}
// 余弦相似度
if (FALSE == empty($cosine_result['similarity']))
$cosine = intval($cosine_result['similarity'] * 10000) / 100;
// 语句拆解结果
if (FALSE == empty($cosine_result['split_results']))
$similarity = $cosine_result['split_results'];
}
$hit_keyword = $this->getKeywords($taskInfo['keywords'],$hit_data);
if (!empty($hit_keyword['keywords'])) {
... ... @@ -165,6 +164,8 @@ class GeoQuestionRes extends Command
'url_num'=>$url_num ?? [],
'is_match'=>$is_match ?? 0,
'label'=>$taskInfo['label'] ?? null,
'cosine' => $cosine,
'similarity' => json_encode($similarity, true),
'created_at'=>date('Y-m-d H:i:s'),
'updated_at'=>date('Y-m-d H:i:s'),
];
... ...
... ... @@ -33,6 +33,7 @@ class CreateProject extends Command
protected $description = '创建项目';
public function handle(){
dd(1);
return $this->sync();
}
... ... @@ -42,10 +43,12 @@ class CreateProject extends Command
* @throws \Exception
*/
public function sync($is_update = 0){
$company = '济南市莱芜凤城铝合金有限公司';
$mobile = '13806340552';
$plan = '标准版';
$cooperate_date = '2019-11-19';
$company = '山东临磨数控机床装备有限公司(自建站)';
$mobile = '18663004388';
$lead_name = '18663004388';
$plan = '商务版';
$cooperate_date = '2025-08-21';
// $channel = '{"user_id": "732", "zone_id": "1", "channel_id": "95"}';
$channel = '{"user_id": "1989", "zone_id": "4", "channel_id": "13"}';
$title = date('Ymd') . '-' . $company;
... ... @@ -53,7 +56,7 @@ class CreateProject extends Command
'project'=>[
'title' => $title,
'company' => $company,
'lead_name' => $mobile,
'lead_name' => $lead_name,
'mobile' => $mobile,
'mysql_id'=>Project::MYSQL_ID,
'serve_id'=>9,
... ... @@ -61,7 +64,7 @@ class CreateProject extends Command
'channel' => $channel,
'requirement' => '',
'cooperate_date' => $cooperate_date,
'from_order_id' => '',
'from_order_id' => uniqid(),
'type' => $is_update,
'is_upgrade'=>$is_update,
],
... ...
... ... @@ -9,6 +9,8 @@
namespace App\Services\Geo;
use Illuminate\Support\Facades\Http;
class GeoService
{
public $api_key = 'UkzZljFv83Z2qBi5YR1o3f2otAVWtug6';
... ... @@ -108,4 +110,31 @@ class GeoService
$data = http_post($url,json_encode($message,true),$header);
return $data;
}
/**
* 获取语句余弦相似度
* $text 会被拆解语句, 分析预警传$text
* @param string $standard 标准答案
* @param string $text 需要分析的语句
* @param string $embedding_model
* @param string $similarity_method
* @return \Illuminate\Http\Client\Response
*/
public function cosineSimilarity($standard, $text, $embedding_model = 'text-embedding-3-small', $similarity_method = 'cosine')
{
$url = 'http://knowledge_base.zabbix.waimaoq.com/v1/crud/split_similarity';
$header = [
'accept: application/json',
'Content-Type: application/json'
];
$param = [
'text1' => $standard,
'text2' => $text,
'embedding_model' => $embedding_model,
'similarity_method' => $similarity_method
];
$result = Http::post($url, $param);
return $result->json();
}
}
... ...