From 99c14d1bacab46666df495d097572a59ec900485 Mon Sep 17 00:00:00 2001 From: Tristan Yang Date: Wed, 15 Feb 2023 09:54:06 +0800 Subject: [PATCH] feat: gpt demo --- src/app/services/message.ts | 11 +++++- src/routes/gptDemo.tsx | 67 +++++++++++++++++++++++++++++++++++++ src/routes/index.tsx | 2 ++ 3 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 src/routes/gptDemo.tsx diff --git a/src/app/services/message.ts b/src/app/services/message.ts index 09e8646b..0429eb8a 100644 --- a/src/app/services/message.ts +++ b/src/app/services/message.ts @@ -147,6 +147,14 @@ export const messageApi = createApi({ } } }), + + replyWithChatGPT: builder.mutation<{ message: string, prompt: string }, string>({ + query: (prompt) => ({ + url: `https://official.voce.chat/chatgpt/complete`, + method: "POST", + body: { prompt, api_key: "test" } + }) + }), replyMessage: builder.mutation< number, { from_uid: number, reply_mid: number; content: string; type: ContentTypeKey, properties?: {} } @@ -209,5 +217,6 @@ export const { useReplyMessageMutation, useLazyDeleteMessageQuery, useReadMessageMutation, - useCreateArchiveMutation + useCreateArchiveMutation, + useReplyWithChatGPTMutation } = messageApi; diff --git a/src/routes/gptDemo.tsx b/src/routes/gptDemo.tsx new file mode 100644 index 00000000..abc665b9 --- /dev/null +++ b/src/routes/gptDemo.tsx @@ -0,0 +1,67 @@ +import { useState, useEffect, FormEvent } from 'react'; +import { JellyTriangle } from '@uiball/loaders'; + +import { useReplyWithChatGPTMutation } from '../app/services/message'; +import Textarea from '../common/component/styled/Textarea'; +import Button from '../common/component/styled/Button'; +import clsx from 'clsx'; + +// type Props = {} +type Conversation = { + type: "prompt" | "reply", content: string +}[] +const GPTDemo = () => { + // const promptRef = useRef(); + // const [prompt, setPrompt] = useState(""); + const [talk, setTalk] = useState([]); + const [feedTheGPT, { data, isSuccess, isLoading }] = useReplyWithChatGPTMutation(); + const handleSend = (evt: FormEvent) => { + evt.preventDefault(); + const form = evt.currentTarget; + // 检查格式 + if (!form?.checkValidity()) { + form?.reportValidity(); + return; + } + const content = new FormData(form).get("prompt") as string; + feedTheGPT(content); + setTalk(prevs => { + return [...prevs, { type: "prompt", content }]; + }); + form.reset(); + }; + useEffect(() => { + if (isSuccess && data) { + const { message } = data; + setTalk(prevs => { + return [...prevs, { type: "reply", content: message }]; + }); + } + }, [isSuccess, data]); + + return ( +
+
+ {talk.map(({ type, content }) => { + return
+ {type}: +
{content}
+
; + })} +
+ {isLoading && + } +
+
+