|
@@ -0,0 +1,91 @@
|
|
|
+import { Component, OnInit } from '@angular/core';
|
|
|
+// 引用FmodeChatCompletion类
|
|
|
+import { TestRxjsChatCompletion,TestRxjsChatMessage } from '../../chat/class-rxjs-chat-completion';
|
|
|
+import Parse from "parse";
|
|
|
+@Component({
|
|
|
+ selector: 'app-agent-shige',
|
|
|
+ templateUrl: './agent-shige.page.html',
|
|
|
+ styleUrls: ['./agent-shige.page.scss'],
|
|
|
+})
|
|
|
+export class AgentShigePage implements OnInit {
|
|
|
+ messageList:Array<TestRxjsChatMessage> = []
|
|
|
+ shigeOptions:any ={
|
|
|
+ content:"",
|
|
|
+ type:"不限制",
|
|
|
+ theme:"不限制",
|
|
|
+ }
|
|
|
+ themeList = ['羁旅思乡诗','爱情闺怨诗','咏史怀古诗','咏物言志诗','送别怀人诗','边塞征战诗','山水田园诗']
|
|
|
+
|
|
|
+ constructor() {
|
|
|
+ }
|
|
|
+
|
|
|
+ ngOnInit() {
|
|
|
+ }
|
|
|
+ sendMessage(){
|
|
|
+ /*
|
|
|
+4. 风格:选择风格,例如“豪放”或“婉约”。
|
|
|
+5. 情感:描述诗中应表达的情感,例如“喜悦”、“忧愁”、“思念”等。
|
|
|
+
|
|
|
+ */
|
|
|
+ let GuhiPromoptTemplate = `
|
|
|
+你是一位古代诗人,擅长写作优美的诗词。请根据以下要求创作一首诗:
|
|
|
+1. 主题:${this.shigeOptions?.theme}。
|
|
|
+2. 格律:${this.shigeOptions?.type}。
|
|
|
+3. 关键词:${this.shigeOptions?.keywords}。
|
|
|
+4. 创意灵感:${this.shigeOptions?.content}。
|
|
|
+请根据以上要求创作一首优美的诗词。
|
|
|
+
|
|
|
+请开始创作,并按照以下格式返回
|
|
|
+题目:
|
|
|
+内容:
|
|
|
+简介:
|
|
|
+ `
|
|
|
+ this.messageList.push({
|
|
|
+ role:"user",
|
|
|
+ content: GuhiPromoptTemplate
|
|
|
+ })
|
|
|
+
|
|
|
+ // messageList在competion内部,已经赋予了完整的message
|
|
|
+ // 下方暴露出来的可订阅内容,主要是用于关键字过滤,或者其他开发逻辑的续写
|
|
|
+ let resultStr = ""
|
|
|
+ let testChatCompletion = new TestRxjsChatCompletion(this.messageList);
|
|
|
+ testChatCompletion.createCompletionByStream().subscribe({
|
|
|
+ next: ({ content, cumulativeContent, done }) => {
|
|
|
+ resultStr = cumulativeContent
|
|
|
+ console.log(`Content: ${content}`);
|
|
|
+ console.log(`Cumulative Content: ${cumulativeContent}`);
|
|
|
+ if (done) {
|
|
|
+ console.log('Stream completed');
|
|
|
+ }
|
|
|
+ },
|
|
|
+ error: err => console.error(err),
|
|
|
+ complete: () => {
|
|
|
+ // 诗歌创建完成:正则表达式,匹配诗歌json内容
|
|
|
+ console.log("原文",resultStr)
|
|
|
+
|
|
|
+ let pattern = /题目:\s*(.*)\s*内容:\s*(.*)\s*简介:\s*(.*)/;
|
|
|
+ let match = resultStr.match(pattern);
|
|
|
+
|
|
|
+ if (match) {
|
|
|
+ let gushi:any = {}
|
|
|
+ gushi.title = match[1];
|
|
|
+ gushi.content = match[2];
|
|
|
+ gushi.intro = match[3];
|
|
|
+ gushi.source = "AI创作";
|
|
|
+ console.log(`题目: ${gushi.title}`);
|
|
|
+ console.log(`内容: ${gushi.content}`);
|
|
|
+ console.log(`简介: ${gushi.intro}`);
|
|
|
+ console.log(gushi);
|
|
|
+ let Shige = Parse.Object.extend("Shige");
|
|
|
+ let sg = new Shige();
|
|
|
+ sg.set(gushi);
|
|
|
+ sg.save();
|
|
|
+ } else {
|
|
|
+ console.log("未能匹配到任何内容");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+}
|