|
@@ -0,0 +1,132 @@
|
|
|
+import { Component, OnInit } from '@angular/core';
|
|
|
+import { EditTagComponent } from '../edit-tag/edit-tag.component';
|
|
|
+import {IonButton, IonItem, IonList, IonTextarea } from '@ionic/angular/standalone';
|
|
|
+import { TestChatCompletion } from './class-chat-completion';
|
|
|
+// import {FmodeChatCompletion}from "fmode-ng"
|
|
|
+
|
|
|
+@Component({
|
|
|
+ selector: 'app-page-exercise-analysis',
|
|
|
+ templateUrl: './page-exercise-analysis.component.html',
|
|
|
+ styleUrls: ['./page-exercise-analysis.component.scss'],
|
|
|
+ standalone: true,
|
|
|
+ imports:[EditTagComponent,IonList,IonItem,IonTextarea,IonButton]
|
|
|
+})
|
|
|
+export class PageExerciseAnalysisComponent implements OnInit {
|
|
|
+
|
|
|
+// 运动关键词
|
|
|
+exerciseTags:Array<string>=[]
|
|
|
+setTagsValue(ev:any){
|
|
|
+ this.exerciseTags=ev;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+// 运动描述
|
|
|
+exerciseDesc:string=""
|
|
|
+
|
|
|
+userInput(ev:any){
|
|
|
+ this.exerciseDesc=ev.detail.value
|
|
|
+}
|
|
|
+
|
|
|
+// 运动结果
|
|
|
+exerciseResult:string=""
|
|
|
+
|
|
|
+// 个性化训练计划推荐
|
|
|
+async analysisExercise(){
|
|
|
+ console.log(this.exerciseTags);
|
|
|
+ console.log(this.exerciseDesc);
|
|
|
+ console.log("开始");
|
|
|
+ let prompt=`您是一名专业的个性化运动推荐专家,请帮我生成一个个性化运动计划,关键词:${this.exerciseTags.join(",")},运动描述:${this.exerciseDesc}`
|
|
|
+ let TOKEN='r:E4KpGvTEto-188798522991732011467'
|
|
|
+ localStorage.setItem("token",TOKEN)
|
|
|
+ let messageList:any=[
|
|
|
+ {
|
|
|
+ role:"system" , content:`${new Date().toLocaleDateString()}`
|
|
|
+ },
|
|
|
+ {
|
|
|
+ role:"user",content:prompt
|
|
|
+ }
|
|
|
+]
|
|
|
+
|
|
|
+//创建并发起一条新的消息补全
|
|
|
+let completion=new TestChatCompletion(messageList)
|
|
|
+//持续更新事件推送的消息体内容绑定至消息变量
|
|
|
+completion.createCompletionByStream()
|
|
|
+
|
|
|
+setInterval(()=>{
|
|
|
+ console.log(messageList)
|
|
|
+ this.exerciseDesc=messageList[messageList.length-1]?.content
|
|
|
+
|
|
|
+},1000)
|
|
|
+// 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",
|
|
|
+// },
|
|
|
+// "body": JSON.stringify(bodyJson),
|
|
|
+// "method": "POST",
|
|
|
+// "mode": "cors",
|
|
|
+// "credentials": "omit"
|
|
|
+// });
|
|
|
+
|
|
|
+// if(response && response.body){
|
|
|
+// let reader = response.body.getReader();
|
|
|
+// if (!reader) {
|
|
|
+// throw new Error("Failed to get the response reader.");
|
|
|
+// }
|
|
|
+
|
|
|
+// let decoder = new TextDecoder();
|
|
|
+// let buffer = "";
|
|
|
+
|
|
|
+// while (true) {
|
|
|
+// let { done, value } = await reader.read();
|
|
|
+// if (done) {
|
|
|
+// break;
|
|
|
+// }
|
|
|
+
|
|
|
+// let data= decoder.decode(value);
|
|
|
+
|
|
|
+// /**
|
|
|
+// * data: {"id":"chatcmpl-AXpzLoeWsP9iG5sfnbV5dRtc34sgS","object":"chat.completion.chunk","created":1732628031,"model":"gpt-4o-mini-2024-07-18","system_fingerprint":"fp_3de1288069","choices":[{"index":0,"delta":{"content":"。"},"logprobs":null,"finish_reason":null}],"usage":null}
|
|
|
+
|
|
|
+// data: {"id":"chatcmpl-AXpzLoeWsP9iG5sfnbV5dRtc34sgS","object":"chat.completion.chunk","created":1732628031,"model":"gpt-4o-mini-2024-07-18","system_fingerprint":"fp_3de1288069","choices":[{"index":0,"delta":{},"logprobs":null,"finish_reason":"stop"}],"usage":null}
|
|
|
+
|
|
|
+// data: {"id":"chatcmpl-AXpzLoeWsP9iG5sfnbV5dRtc34sgS","object":"chat.completion.chunk","created":1732628031,"model":"gpt-4o-mini-2024-07-18","system_fingerprint":"fp_3de1288069","choices":[],"usage":{"prompt_tokens":116,"completion_tokens":665,"total_tokens":781,"prompt_tokens_details":{"cached_tokens":0,"audio_tokens":0},"completion_tokens_details":{"reasoning_tokens":0,"audio_tokens":0,"accepted_prediction_tokens":0,"rejected_prediction_tokens":0}}}
|
|
|
+
|
|
|
+// data: [DONE]
|
|
|
+// */
|
|
|
+
|
|
|
+// let messages=data.split("\n");
|
|
|
+// messages.forEach(message=>{
|
|
|
+// console.log(message);
|
|
|
+// let dataStr:string =message.split("data:")[1]
|
|
|
+// if(dataStr&&dataStr.startsWith("{")){
|
|
|
+// try{
|
|
|
+// let json=JSON.parse(dataStr);
|
|
|
+// let content=json.choices[0].delta.content
|
|
|
+// console.log(content);
|
|
|
+// this.exerciseResult=this.exerciseResult+content
|
|
|
+
|
|
|
+// }catch(err){}
|
|
|
+// }
|
|
|
+
|
|
|
+// })
|
|
|
+// console.log(data);
|
|
|
+
|
|
|
+// }
|
|
|
+// }
|
|
|
+}
|
|
|
+ constructor() { }
|
|
|
+
|
|
|
+ ngOnInit() {}
|
|
|
+
|
|
|
+}
|