|
@@ -1,5 +1,5 @@
|
|
|
import { Component, OnInit } from '@angular/core';
|
|
|
-import { IonicModule } from '@ionic/angular';
|
|
|
+import { IonicModule, ToastController } from '@ionic/angular';
|
|
|
import { FormsModule } from '@angular/forms';
|
|
|
import { CommonModule } from '@angular/common';
|
|
|
import { TagInputComponent } from '../tag-input/tag-input.component';
|
|
@@ -28,7 +28,7 @@ export class TestPageComponent implements OnInit {
|
|
|
isGenerating: boolean = false; // 是否正在生成计划
|
|
|
messageList: any[] = []; // 存储消息列表
|
|
|
|
|
|
- constructor() {
|
|
|
+ constructor(private toastController: ToastController) {
|
|
|
addIcons({ personOutline, barbellOutline, alarmOutline, square });
|
|
|
}
|
|
|
|
|
@@ -63,7 +63,24 @@ export class TestPageComponent implements OnInit {
|
|
|
this.age = event.detail.value;
|
|
|
}
|
|
|
|
|
|
+ // 显示toast提示
|
|
|
+ async showToast(message: string) {
|
|
|
+ const toast = await this.toastController.create({
|
|
|
+ message: message,
|
|
|
+ duration: 2000,
|
|
|
+ position: 'top',
|
|
|
+ color: 'danger',
|
|
|
+ });
|
|
|
+ toast.present();
|
|
|
+ }
|
|
|
+
|
|
|
async generatePlan() {
|
|
|
+ // 检查身高和体重是否为空
|
|
|
+ if (!this.height || !this.weight) {
|
|
|
+ // 如果为空,显示提示信息
|
|
|
+ await this.showToast('身高或体重为空,无法生成个人健身计划');
|
|
|
+ return; // 退出函数,不继续生成健身计划
|
|
|
+ }
|
|
|
|
|
|
this.generatedPlan = ''; // 清空之前的结果
|
|
|
this.messageList = []; // 清空旧的消息列表,避免重复内容
|
|
@@ -81,80 +98,16 @@ export class TestPageComponent implements OnInit {
|
|
|
let prompt = `您作为一名专业的健身计划定制大师,请帮我根据以下情况制定健身计划(健身计划请给每天的运动标上序号)。关键词:${this.selectedTags.join(",")},目标描述:${this.goalDescription},运动偏好:${this.exercisePreference},
|
|
|
健身频率一周:${this.workoutFrequency},身高:${this.height}cm,体重:${this.weight}kg,年龄:${this.age}`;
|
|
|
|
|
|
- // let TOKEN = `r:E4KpGvTEto-131588121831732778669`;
|
|
|
- // localStorage.setItem("token", TOKEN);
|
|
|
-
|
|
|
let messageList = new FmodeChatCompletion([
|
|
|
{ role: "system", content: '' },
|
|
|
{ role: "user", content: prompt }
|
|
|
]);
|
|
|
messageList.sendCompletion().subscribe((message: any) => {
|
|
|
- // 打印消息体
|
|
|
console.log(message.content)
|
|
|
- // 赋值消息内容给组件内属性
|
|
|
this.generatedPlan = message.content
|
|
|
if (message?.complete) { // 判断message为完成状态,则设置isComplete为完成
|
|
|
this.isGenerating = false
|
|
|
}
|
|
|
})
|
|
|
- // let bodyJson = {
|
|
|
- // "token": `Bearer ${TOKEN}`,
|
|
|
- // "messages": messageList,
|
|
|
- // "model": "fmode-4.5-128k",
|
|
|
- // "temperature": 0.5,
|
|
|
- // "presence_penalty": 0,
|
|
|
- // "frequency_penalty": 0,
|
|
|
- // "top_p": 1,
|
|
|
- // "stream": true
|
|
|
- // };
|
|
|
-
|
|
|
- // let response = await fetch("https://test.fmode.cn/api/apig/aigc/gpt/v1/chat/completions", {
|
|
|
- // "headers": {
|
|
|
- // "accept": "text/event-stream",
|
|
|
- // "sec-fetch-dest": "empty",
|
|
|
- // "sec-fetch-mode": "cors",
|
|
|
- // "sec-fetch-site": "same-site"
|
|
|
- // },
|
|
|
- // "body": JSON.stringify(bodyJson),
|
|
|
- // "method": "POST",
|
|
|
- // "mode": "cors",
|
|
|
- // "credentials": "omit"
|
|
|
- // });
|
|
|
-
|
|
|
- // let reader = response.body?.getReader();
|
|
|
- // if (!reader) {
|
|
|
- // throw new Error("Failed to get the response reader.");
|
|
|
- // }
|
|
|
-
|
|
|
- // let buffer = "";
|
|
|
- // const decoder = new TextDecoder(); // 定义 TextDecoder
|
|
|
- // while (true) {
|
|
|
- // let { done, value } = await reader.read();
|
|
|
- // if (done) break;
|
|
|
-
|
|
|
- // let data = decoder.decode(value);
|
|
|
- // buffer += data; // 保留数据流中的内容
|
|
|
-
|
|
|
- // let messages = buffer.split("\n");
|
|
|
- // buffer = messages.pop() || ""; // 保留未处理的部分
|
|
|
-
|
|
|
- // messages.forEach(message => {
|
|
|
- // let dataText: string = message.split("data: ")[1];
|
|
|
- // if (dataText && dataText.startsWith("{")) {
|
|
|
- // try {
|
|
|
- // let json = JSON.parse(dataText);
|
|
|
- // let content = json?.choices?.[0]?.delta?.content;
|
|
|
- // if (content) {
|
|
|
- // console.log(content);
|
|
|
- // this.generatedPlan += content;
|
|
|
- // }
|
|
|
- // } catch (err) {
|
|
|
- // console.error('生成计划失败:', err);
|
|
|
- // }
|
|
|
- // }
|
|
|
- // });
|
|
|
- // }
|
|
|
-
|
|
|
- // this.isGenerating = false; // 生成结束后恢复按钮
|
|
|
}
|
|
|
}
|