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

95 lines
3.5 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 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;
}
// 解惑
public function genHelpPrompt($toDay, $dayDesc = '寻常日', $question): string
{
if (empty($question)) {
$question = '人生很迷茫';
}
$prompt = "今天是{$toDay}{$dayDesc}。用户心中有惑:“{$question}”。请以隐世智者或禅师的口吻结合时令节气为其解惑。包含【机锋】与【指引】。要求不使用Markdown纯文本输出。";
return $prompt;
}
}