Files
nl-jqrl-api/app/Service/common/ai/PromptService.php

181 lines
6.8 KiB
PHP
Raw Permalink 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 dailyCourseAnalysis($toDay, $dayDesc = '寻常日', string $prompt): string
{
if (empty($dayDesc)) {
$dayDesc = '寻常日';
}
$time = date('Y年m月d日');
// 吧time变成大写的 时间
$time = $this->dateToChinese($time);
// 根据$time查看今天是农历几月初几
$prompt = "今天是{$time}。请作为一位严厉但充满智慧的私塾先生或人生导师,根据以下用户日课数据进行分析:{$prompt}
请包含:
{$time}:写一段文言文来作为导语。
【昨昔之憾】:分析昨日未完成之事和可能的原因。
【今日当勉】:针对今日未完成任务的督促指导。
【明日之期】:对明日任务的展望。
【日课建议】:根据最近的任务安排推测最近工作是否繁重,是否需要注意身体,事业固然中药,万不可劳累过度。
【小结】:根据已完成的任务总结我最近在忙什么。
提示:
如果没有数据则是昨日没有安排日课那一天就不需要分析为什么没完成。
要求文风古雅不要出现大白话不使用Markdownmd格式纯文本输出。";
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;
}
// 将数字转换为中文大写
function numberToChinese($num) {
$chineseNumbers = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九'];
$chineseUnits = ['', '十', '百', '千'];
$result = '';
$numStr = (string)$num;
$len = strlen($numStr);
for ($i = 0; $i < $len; $i++) {
$digit = (int)$numStr[$i];
$unit = $len - $i - 1;
if ($digit !== 0) {
// 处理"一十"开头的特殊情况
if ($unit == 1 && $digit == 1 && $i == 0) {
$result .= '十';
} else {
$result .= $chineseNumbers[$digit] . $chineseUnits[$unit % 4];
}
} else {
// 避免连续的零,且最后一位为零时不显示
if ($i < $len - 1 && $numStr[$i + 1] != '0') {
$result .= '零';
}
}
}
return $result;
}
// 处理日期的大写转换
function dateToChinese($date) {
// 使用正则表达式提取年、月、日
preg_match('/(\d{4})年(\d{2})月(\d{2})日/', $date, $matches);
if (count($matches) === 4) {
$year = $matches[1];
$month = (int)$matches[2]; // 转换为整数去除前导零
$day = (int)$matches[3]; // 转换为整数去除前导零
// 年份单独处理(四位数字)
$yearChinese = '';
for ($i = 0; $i < 4; $i++) {
$yearChinese .= $this->numberToChinese($year[$i]);
}
// 月份和日期的特殊处理
$monthChinese = $this->numberToChinese($month);
$dayChinese = $this->numberToChinese($day);
return $yearChinese . '年' . $monthChinese . '月' . $dayChinese . '日';
}
return $date;
}
}