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

116 lines
4.6 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 dailyCourseAnalysis($toDay, $dayDesc = '寻常日', string $prompt): string
{
if (empty($dayDesc)) {
$dayDesc = '寻常日';
}
$prompt = "今天是{$toDay}。请作为一位严厉但充满智慧的私塾先生或人生导师,根据以下用户日课数据进行分析:{$prompt}
请包含:
1. 【昨昔之憾】:分析昨日未完成之事。
2. 【今日当勉】:针对今日未完成任务的督促。
3. 【明日之期】:对明日任务的展望。
4. 【日课建议】:根据最近的任务安排推测最近工作是否繁重,是否需要注意身体,不可劳累过度。
5. 【近况小结】:根据已完成的任务总结我最近在忙什么。
提示:
如果某日的日课是空的,那么说明那天没有安排日程。
要求文风古雅要给出人生建议不使用Markdown纯文本输出。";
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;
}
}