Skip to content

Commit

Permalink
新增retry功能和缓存write内容
Browse files Browse the repository at this point in the history
  • Loading branch information
cujinsen committed Mar 6, 2024
1 parent 2c82b99 commit 78b814c
Show file tree
Hide file tree
Showing 5 changed files with 160 additions and 28 deletions.
4 changes: 3 additions & 1 deletion src/locales/zhHans.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ export default {
write: {
settings: "参数设置",
write: "续写",
screenshot: "截图",
clear: "清空",
title: "论文标题",
step1: "生成提纲",
step2: "生成论文",
Expand All @@ -90,7 +92,7 @@ export default {
prompt2: "论文提纲标准格式:I.引言。II. 正文。III. 结论。正文又细分为若干具体的小节,用罗马数字分段。",
prompt3: "根据论文主题对下列段落进行详细的撰写,完成一篇高度凝练且全面的论文,论文的题目是:",
prompt4: "段落标题",


},

Expand Down
19 changes: 17 additions & 2 deletions src/views/demos/chat/chatStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export const useChatStore = defineStore({
this.chatHistory.history= his.history;
this.chatHistory.ai =his.ai;
this.chatHistory.me = his.me;

},
setnowchat(nowchatid: string) {
this.nowchat = nowchatid;
Expand All @@ -108,6 +108,21 @@ export const useChatStore = defineStore({
removeLatestMessage() {
this.chatHistory.history.pop();
},
removeOtherMessage(timestamp: number) {
const history = this.chatHistory.history;
for (let i = history.length - 1; i >= 0; i--) {
if (history[i].timestamp >= timestamp) {
history.splice(i, 1);
} else {
// 由于历史记录是按时间排序的,
// 一旦遇到不满足条件的记录就可以停止循环
break;
}
}
console.log(history);
},


changeLatestMessage(msg: string) {
let newmsg = this.chatHistory.history[this.chatHistory.history.length - 1];

Expand All @@ -120,7 +135,7 @@ export const useChatStore = defineStore({
return this.chatHistory.history[this.chatHistory.history.length - 1];
},
deleteMessage(id: string) {

let msg = this.chatHistory.history.find((item) => item.id === id);
//获取 msg的 index
let index = this.chatHistory.history.indexOf(msg);
Expand Down
118 changes: 117 additions & 1 deletion src/views/demos/chat/components/Message.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import ChatLabelai from "./ChatLabelai.vue";
import ChatLabeluser from "./ChatLabeluser.vue";
import { useChatStore } from "../chatStore";
import clipboard from "@/utils/clipboardUtils";
import {ChatListStore} from "@/views/demos/chat/chatlistStore";
import type {ChatMessage, Message, User} from "@/views/demos/chat/chatTypes";
import * as ai00Type from "@/ai00sdk/ai00Type";
const ChatStore = useChatStore();
const props = defineProps({
// Message to display
Expand Down Expand Up @@ -43,6 +46,119 @@ const copyText = (event: Event) => {
function deleteMessage(id: string) {
ChatStore.deleteMessage(id);
}
const chatStore = useChatStore();
const ChatList = ChatListStore();
const aiMessage = ref("");

const createMessage = (user: User, text: string) => {
const message: Message = {
id: "_" + Math.random().toString(36).substring(2, 11),
user: user,
text: text,
timestamp: new Date().getTime(),
};
return message;
};
const sendMessage = () => {
// 判断是否为空

if (chatStore.isChatting) {
return;
}
// 发送User Message
chatStore.removeOtherMessage(
props.message.timestamp
);

// 请求AI回答
console.log(chatStore.getLatestMessage().text);
sendChatMessage(chatStore.getLatestMessage().text);
};
const sendChatMessage = async (content: string) => {
try {
chatStore.setChatting(true);
const messlist = chatStore.chatHistory.history;
const contlist: any[] = [
{
role: "user",
content: "Hi!",
},
];

// const contlist =

for (let index = 0; index < messlist.length - 1; index++) {
const element = messlist[index];
let chatm: ChatMessage = {
role: element.user.id == 1 ? "user" : "assistant",
content: element.text,
};
contlist.push(chatm);
}

/*定义要传送给 window.Ai00Api.oai_chat_completions 的 body参数 ,
结构为 ai00Type.OaiChatCompletionsType
*/
if (chatStore.SamplerType == "Nucleus") {
const body: ai00Type.OaiChatCompletionsType = {
messages: contlist,
max_tokens: chatStore.Max_Tokens,
temperature: chatStore.Temperature,
top_p: chatStore.TOP_P,
presence_penalty: chatStore.Presence,
frequency_penalty: chatStore.Frequency,
penalty_decay: Math.exp(-0.69314718055994 / Number(chatStore.Penalty)),
stop: [
"\n" + chatStore.chatHistory.me.name + ":",
chatStore.chatHistory.me.name + ":",
],
stream: true,
names: {
user: chatStore.chatHistory.me.name,
assistant: chatStore.chatHistory.ai.name,
},
};
window.Ai00Api.oai_chat_completions(body, async (res: string) => {
chatStore.changeLatestMessage(res);
});
} else if (chatStore.SamplerType == "Mirostat") {
const body: ai00Type.OaiChatCompletionsType = {
messages: contlist,
max_tokens: chatStore.Max_Tokens,
tau: chatStore.tau,
rate:chatStore.rate,
stop: [
"\n" + chatStore.chatHistory.me.name + ":",
chatStore.chatHistory.me.name + ":",
],
stream: true,
names: {
user: chatStore.chatHistory.me.name,
assistant: chatStore.chatHistory.ai.name,
},
};
window.Ai00Api.oai_chat_completions(body, async (res: string) => {
chatStore.changeLatestMessage(res);
});

// 调用 window.Ai00Api.oai_chat_completions 函数,传入参数:
// body 参数数据结构是 /ai00sdk/ai00Type.ts 中定义 的 ai00Type.OaiChatCompletionsType
// console.log("111")
}

// 调用 window.Ai00Api.oai_chat_completions 函数,传入参数:
// body 参数数据结构是 /ai00sdk/ai00Type.ts 中定义 的 ai00Type.OaiChatCompletionsType
// console.log("111")
window.Ai00Api.oai_chat_completions(body, async (res: string) => {
chatStore.changeLatestMessage(res);
});
} catch (error: any) {
chatStore.setChatting(false);
} finally {
chatStore.setChatting(false);
}
};

</script>

<template>
Expand Down Expand Up @@ -78,7 +194,7 @@ function deleteMessage(id: string) {
<v-card-actions class="card-actions">
<div class="close" v-if="!isUserMessage">
<v-btn
@click=""
@click="sendMessage"
density="compact"
icon="mdi-reload"
size="small"
Expand Down
4 changes: 2 additions & 2 deletions src/views/demos/newchat/Store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { defineStore } from "pinia";
export const useTodoStore = defineStore({
id: "todo",
state: () => ({

text:"# Hello Editor",
apiKey: "",
chatHistory: [],
apiKeyDialog: false,
Expand All @@ -25,7 +25,7 @@ export const useTodoStore = defineStore({
Model:"",
AIimg:"",
SamplerType:"Nucleus",

}),

// persist: {
Expand Down
43 changes: 21 additions & 22 deletions src/views/demos/newchat/component/List.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@
-->
<script setup lang="ts">
import { useTodoStore } from "../Store";
import { MdEditor } from 'md-editor-v3';
import {MdEditor, ToolbarNames} from 'md-editor-v3';
import 'md-editor-v3/lib/style.css';
import * as ai00Type from "@/ai00sdk/ai00Type";
import Copy from "./Copy.vue";

const todoStore = useTodoStore();
const text = ref('# Hello Editor');


const toolbar = ref([
'bold',
'underline',
Expand All @@ -37,11 +36,11 @@ const toolbar = ref([


const sendPrompt = async () => {
const prompt = text.value
const temp :string = text.value

const prompt = todoStore.text
const temp :string = todoStore.text
if(todoStore.SamplerType == 'Nucleus'){


const body: ai00Type.OaiCompletionsType = {
prompt: [prompt],
Expand All @@ -55,8 +54,8 @@ if(todoStore.SamplerType == 'Nucleus'){
stream: true,
}
window.Ai00Api.oai_completions(body, async (res: string) => {
text.value = temp + res
todoStore.text = temp + res

})
}else if(todoStore.SamplerType == 'Mirostat'){
const body: ai00Type.OaiCompletionsType = {
Expand All @@ -68,13 +67,13 @@ if(todoStore.SamplerType == 'Nucleus'){
stream: true,
}
window.Ai00Api.oai_completions(body, async (res: string) => {
text.value = temp + res
todoStore.text = temp + res

})

}



// 调用 window.Ai00Api.oai_chat_completions 函数,传入参数:
// body 参数数据结构是 /ai00sdk/ai00Type.ts 中定义 的 ai00Type.OaiChatCompletionsType
Expand Down Expand Up @@ -103,29 +102,29 @@ if (element) {
<v-card height="100%"
prepend-icon="mdi-file-document-edit">
<template v-slot:title>
{{ $t("write.write") }}
{{ $t("write.write") }}
<v-spacer></v-spacer>

<v-btn color="primary" @click="sendPrompt"> {{ $t("write.write") }} </v-btn>

<v-btn color="primary" @click="text=''" style="margin-left: 30px;">清空</v-btn>

<v-btn color="primary" @click="goClipboard" style="margin-left: 30px;">截图</v-btn>
<v-btn color="primary" @click="todoStore.text=''" style="margin-left: 30px;">{{ $t("write.clear") }}</v-btn>

<v-btn color="primary" @click="goClipboard" style="margin-left: 30px;">{{ $t("write.screenshot") }}</v-btn>
</template>
<v-card-text >
<MdEditor v-model="text"
<MdEditor v-model="todoStore.text"
:toolbars="toolbar"
class="editor"
:preview = false

/>
<div class="hidejietu" id="po">
<Copy :text="text+'\n\n'" />

<Copy :text="todoStore.text+'\n\n'" />
<p style="margin-top: 20px;">✅ Powered by AI00 for RWKV ( https://github.com/cgisky1980/ai00_rwkv_server ) </p>
</div>
</v-card-text>

</v-card>

</template>
Expand Down

0 comments on commit 78b814c

Please sign in to comment.