page-test-completion.component.ts 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import { Component, OnInit } from '@angular/core';
  2. import { ChatMessage, TestFmodeChatCompletion } from '../test.chat.completion';
  3. import { IonicModule } from '@ionic/angular';
  4. import { CommonModule } from '@angular/common';
  5. import { FormsModule } from '@angular/forms';
  6. @Component({
  7. selector: 'app-page-test-completion',
  8. templateUrl: './page-test-completion.component.html',
  9. styleUrls: ['./page-test-completion.component.scss'],
  10. imports: [IonicModule, CommonModule, FormsModule],
  11. standalone: true,
  12. })
  13. export class PageTestCompletionComponent implements OnInit {
  14. userDescription: string = '';
  15. aiResponse: string = '';
  16. isLoading: boolean = false;
  17. constructor() { }
  18. ngOnInit() { return; }
  19. startConsult() {
  20. if (!this.userDescription.trim()) {
  21. console.warn('用户描述不能为空');
  22. return;
  23. }
  24. this.isLoading = true;
  25. this.aiResponse = ''; // 清空之前的响应
  26. // 导诊系统提示词
  27. const diagnosisPrompt = `你是一个专业的医疗导诊助手。
  28. 请根据患者描述的症状,进行以下分析:
  29. 1. 可能的疾病方向(列出2-3个最可能的)
  30. 2. 建议就诊的科室
  31. 3. 是否需要立即就医的紧急程度评估
  32. 4. 日常护理建议
  33. 请用专业但易懂的语言回答,避免直接诊断,而是提供就医指导。
  34. 结果要求:
  35. 1.首先,直接给出就诊的科室
  36. 2.给用户建议
  37. `;
  38. const messages: ChatMessage[] = [
  39. { role: 'system', content: `` },
  40. { role: 'user', content: diagnosisPrompt+`患者症状描述:${this.userDescription}` }
  41. ];
  42. localStorage.setItem("token",'r:9485439b35a9ce34bb95a9f3cfec2e6e'), // 替换为您的实际token
  43. TestFmodeChatCompletion(
  44. {
  45. messages
  46. },
  47. (chunk, isLast) => {
  48. if (isLast) {
  49. this.isLoading = false;
  50. console.log('\n对话完成');
  51. } else {
  52. this.aiResponse += chunk; // 累积AI返回的内容
  53. console.log(chunk); // 实时输出内容
  54. }
  55. },
  56. (error) => {
  57. this.isLoading = false;
  58. console.error('发生错误:', error.message);
  59. this.aiResponse = '获取导诊建议时出错,请稍后再试。';
  60. }
  61. );
  62. }
  63. }