12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- import { Component, OnInit } from '@angular/core';
- import {IonHeader, IonToolbar, IonTitle,IonInput, IonContent,IonButton, IonIcon } from '@ionic/angular/standalone';
- import { addIcons } from 'ionicons';
- import { chevronBack,heart } from 'ionicons/icons';
- import { Router } from '@angular/router';
- import { FmodeChatCompletion } from 'fmode-ng';
- import { CommonModule } from '@angular/common'; // 添加 CommonModule
- import { FormsModule } from '@angular/forms'; // 添加 FormsModule
- @Component({
- selector: 'app-page-companion',
- templateUrl: './page-companion.component.html',
- styleUrls: ['./page-companion.component.scss'],
- standalone: true,
- imports:[IonHeader, CommonModule, FormsModule, IonToolbar, IonTitle, IonContent,IonButton,IonInput, IonIcon]
- })
- export class PageCompanionComponent implements OnInit {
- userInput: string = ''; // 用户输入内容
- messages: { role: string, content: string }[] = []; // 消息列表
-
- // 用户输入变化
- onInputChange(ev: any) {
- this.userInput = ev.detail.value;
- }
- // 发送消息
- sendMessage() {
- if (!this.userInput.trim()) return; // 确保输入不为空
- // 添加用户消息到消息列表
- this.messages.push({ role: 'user', content: this.userInput });
- // 调用 AI 接口
- const promptTemplate = `
- 请你先分析用户的口述,如果是明显需要心理咨询帮助的意思,那么请你作为专业的心理咨询师,根据用户的描述提供情感疏导和建议;如果用户只是简单的日常聊天,比如吐槽、唠嗑,那么请你作为一个朋友的形象,根据的用户的消息提供友好并有情绪价值的回复。
- 以下是用户的口述:${this.userInput}
- `;
- const completion = new FmodeChatCompletion([
- { role: 'system', content: '' },
- { role: 'user', content: promptTemplate }
- ]);
- let aiResponse = '';
- completion.sendCompletion().subscribe((message: any) => {
-
- // 将生成的消息内容累加到 aiResponse 中
- aiResponse += message.content; // 假设 message.content 是 AI 生成的内容
- // 添加 AI 消息到消息列表
- this.messages.push({ role: 'ai', content:aiResponse });
- // 清空用户输入框
- this.userInput = '';
- });
- }
- constructor(private router: Router) {
- addIcons({chevronBack,heart});
- }
- goTab1(){
- this.router.navigate(['tabs/tab1']);
- }
- ngOnInit() {}
- }
|