Files
nl-jqrl-api/app/Service/common/ai/PromptService.php
2025-12-01 12:12:30 +08:00

122 lines
4.9 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\Service\common\ai;
class PromptService
{
/**
* 格式化药品信息
*/
public function formatDrugsInfo(array $drugs): string
{
$drugList = [];
foreach ($drugs as $drug) {
$drugInfo = "{index_id: {$drug['index_id']}, 药品ID: {$drug['drug_id']}, '名称: {$drug['drug_name']}', 单价: '{$drug['price']}'}";
// $drugInfo = "index_id: {$drug['index_id']}, 药品ID: {$drug['drug_id']}, '名称: {$drug['drug_name']}'";
// $drugInfo = "ID:{$drug['drug_id']},名称:{$drug['drug_name']}";
if (!empty($drug['indications']?? '')) {
$drugInfo .= ", 适应症: {$drug['indications']}";
}
if (!empty($drug['contraindications']?? '')) {
$drugInfo .= ", 禁忌: {$drug['contraindications']}";
}
$drugList[] = $drugInfo;
}
// return implode("\n", $drugList);
return '{'. implode(",", $drugList). '}';
}
/**
* 格式化患者信息
*/
public function formatPatientInfo(array $userInfo): string
{
$info = [];
$info[] = "年龄:" . ($userInfo['age'] ?? '未知');
$info[] = "性别:" . ($userInfo['gender'] ?? '未知');
$info[] = "诊断:" . ($userInfo['diagnosis'] ?? '未提供');
if (!empty($userInfo['medical_history'])) {
$info[] = "患病历史:" . (is_array($userInfo['medical_history']) ?
implode('、', $userInfo['medical_history']) : $userInfo['medical_history']);
}
if (!empty($userInfo['treatment_history'])) {
$info[] = "就诊历史:" . (is_array($userInfo['treatment_history']) ?
implode('、', $userInfo['treatment_history']) : $userInfo['treatment_history']);
}
if (!empty($userInfo['symptoms'])) {
$info[] = "症状:" . (is_array($userInfo['symptoms']) ?
implode('、', $userInfo['symptoms']) : $userInfo['symptoms']);
}
return implode("\n", $info);
}
public function genPrescriptionPrompt(array $userInfo, array $drugs): string
{
$patientInfo = $this->formatPatientInfo($userInfo);
$drugsInfo = $this->formatDrugsInfo($drugs);
$drugsInfo = base64_encode($drugsInfo);
$prompt = "你是一名专业的医生,请根据以下患者信息和可用药品,开具合理的处方:
## 患者信息:
{$patientInfo}
## 可用药品列表base64_encode后的
{$drugsInfo}
## 请按照以下JSON格式返回处方建议
{
\"ai_diagnosis\": \"AI诊断描述\",
\"ai_advice\": \"AI医嘱\",
\"medication_advice\": [
{
// \"index_id\": \"药品ID\",
\"drug_id\": \"药品ID\",
\"drug_name\": \"药品名称\",
\"single_dosage\": \"单次用量\",
}
],
\"frequency_description\": \"用药频次说明\",
\"treatment_period\": \"治疗周期说明\",
\"frequency\": \"每天次数(数字格式)\",
\"days\": \"用药天数(数字格式)\"
}
## 要求:
1. 诊断要基于患者年龄、病史和当前症状
2. 用药建议要合理,考虑药品适应症和患者情况,只能选择我给出的药品列表中的药品
3. 单次用量采用数字类型单位都是g
4. 频次要合理3,就是一天三次的意思)
5. 用药天数要根据病情严重程度确定";
return $prompt;
}
// 求签提示词封装
public function genSignPrompt($toDay, $dayDesc = '寻常日'): string
{
if (empty($dayDesc)) {
$dayDesc = '寻常日';
}
// toDay是日期yyyy-mm-dd
// dayDesc是节气默认寻常日
$prompt = "今天是{$toDay}{$dayDesc}。请模仿隐世高人,为我求签。包含【签文】(七言绝句)、【解曰】(禅意指点)。";
return $prompt;
}
// 求签提示词封装
public function genSignPrompt2($toDay, $dayDesc = '寻常日'): string
{
// toDay是日期yyyy-mm-dd
// dayDesc是节气默认寻常日
$prompt = "今天是{$toDay}{$dayDesc}。请模仿老中医根据这个季节和临近的节气给出现代人的养生方。包含饮食、起居。要用一段或几段描述。切记不要使用md格式请使用纯文本格式。并且语气要有仙风道骨的感觉";
return $prompt;
}
}