From f797f6984cc7bcc488efab2d9de7add992f7df49 Mon Sep 17 00:00:00 2001 From: Jing Hua Date: Thu, 23 Mar 2023 00:58:26 +0800 Subject: [PATCH] feat: ShareGPT Fixes #1 --- public/locales/da/main.json | 6 +- public/locales/en/main.json | 6 +- public/locales/es/main.json | 6 +- public/locales/nb/main.json | 6 +- public/locales/sv/main.json | 6 +- public/locales/zh-CN/main.json | 6 +- public/locales/zh-HK/main.json | 6 +- public/locales/zh-TW/main.json | 6 +- src/api/api.ts | 16 ++++++ .../Chat/ChatContent/ChatContent.tsx | 8 ++- src/components/ShareGPT/ShareGPT.tsx | 57 +++++++++++++++++++ src/components/ShareGPT/index.ts | 1 + src/types/api.ts | 8 +++ 13 files changed, 128 insertions(+), 10 deletions(-) create mode 100644 src/components/ShareGPT/ShareGPT.tsx create mode 100644 src/components/ShareGPT/index.ts diff --git a/public/locales/da/main.json b/public/locales/da/main.json index dda4db0..6dd5af3 100644 --- a/public/locales/da/main.json +++ b/public/locales/da/main.json @@ -28,5 +28,9 @@ "promptLibrary": "Opgavebibliotek", "name": "Navn", "search": "Søg", - "morePrompts": "Du kan finde flere opgaver her: " + "morePrompts": "Du kan finde flere opgaver her: ", + "postOnShareGPT": { + "title": "Indlæg på ShareGPT", + "warning": "Vær opmærksom på, at ved at poste din samtale på ShareGPT, vil den blive offentligt tilgængelig og synlig for alle. Når den er postet, kan samtalen ikke skjules eller slettes og kan blive arkiveret eller delt af andre. Vi råder dig til at overveje nøje og undgå at dele følsomme eller private oplysninger på denne platform." + } } diff --git a/public/locales/en/main.json b/public/locales/en/main.json index 8a9b494..661b8f7 100644 --- a/public/locales/en/main.json +++ b/public/locales/en/main.json @@ -28,5 +28,9 @@ "promptLibrary": "Prompt Library", "name": "Name", "search": "Search", - "morePrompts": "You can find more prompts here: " + "morePrompts": "You can find more prompts here: ", + "postOnShareGPT": { + "title": "Post on ShareGPT", + "warning": "Please be aware that by posting your conversation on ShareGPT, it will become publicly accessible and viewable to anyone. Once posted, the conversation cannot be hidden or deleted, and may be archived or shared by others. We advise you to consider carefully and avoid sharing sensitive or private information on this platform." + } } diff --git a/public/locales/es/main.json b/public/locales/es/main.json index f858b76..a1a5f5a 100644 --- a/public/locales/es/main.json +++ b/public/locales/es/main.json @@ -28,5 +28,9 @@ "promptLibrary": "Librería de Prompts", "name": "Nombre", "search": "Buscar", - "morePrompts": "Puedes encontrar más prompts aquí: " + "morePrompts": "Puedes encontrar más prompts aquí: ", + "postOnShareGPT": { + "title": "Publicar en ShareGPT", + "warning": "Por favor, tenga en cuenta que al publicar su conversación en ShareGPT, esta será accesible y visible para cualquiera. Una vez publicada, la conversación no se podrá ocultar ni eliminar, y puede ser archivada o compartida por otros. Le aconsejamos que lo considere detenidamente y evite compartir información sensible o privada en esta plataforma." + } } diff --git a/public/locales/nb/main.json b/public/locales/nb/main.json index cc4d7a5..46c1780 100644 --- a/public/locales/nb/main.json +++ b/public/locales/nb/main.json @@ -28,5 +28,9 @@ "promptLibrary": "Oppgavebibliotek", "name": "Navn", "search": "Søk", - "morePrompts": "Du kan finne flere oppgaver her: " + "morePrompts": "Du kan finne flere oppgaver her: ", + "postOnShareGPT": { + "title": "Innlegg på ShareGPT", + "warning": "Vær oppmerksom på at ved å poste samtalen din på ShareGPT, vil den bli offentlig tilgjengelig og synlig for alle. Når den er postet, kan samtalen ikke skjules eller slettes, og den kan bli arkivert eller delt av andre. Vi anbefaler deg å tenke nøye gjennom og unngå å dele sensitiv eller privat informasjon på denne plattformen." + } } diff --git a/public/locales/sv/main.json b/public/locales/sv/main.json index a9e875c..06960d7 100644 --- a/public/locales/sv/main.json +++ b/public/locales/sv/main.json @@ -28,5 +28,9 @@ "promptLibrary": "Uppmaningsbibliotek", "name": "Namn", "search": "Sök", - "morePrompts": "Du kan hitta fler uppmaningar här: " + "morePrompts": "Du kan hitta fler uppmaningar här: ", + "postOnShareGPT": { + "title": "Inlägg på ShareGPT", + "warning": "Var medveten om att genom att posta din konversation på ShareGPT kommer den att bli offentligt tillgänglig och synlig för alla. När den väl är postad kan konversationen varken döljas eller raderas och kan arkiveras eller delas av andra. Vi rekommenderar dig att tänka noggrant igenom och undvika att dela känslig eller privat information på denna plattform." + } } diff --git a/public/locales/zh-CN/main.json b/public/locales/zh-CN/main.json index d94c36c..4aaf85d 100644 --- a/public/locales/zh-CN/main.json +++ b/public/locales/zh-CN/main.json @@ -28,5 +28,9 @@ "promptLibrary": "提示词资料库", "name": "名称", "search": "搜索", - "morePrompts": "更多提示词请点击:" + "morePrompts": "更多提示词请点击:", + "postOnShareGPT": { + "title": "发布至 ShareGPT", + "warning": "请注意,把您的对话发布到 ShareGPT 后,任何人都可以公开访问和查看。发布后,对话不能被隐藏或删除,且可能被其他人存档或分享。建议您慎重考虑,在这个平台上避免分享敏感或私密信息。" + } } diff --git a/public/locales/zh-HK/main.json b/public/locales/zh-HK/main.json index 1248a9f..8972d55 100644 --- a/public/locales/zh-HK/main.json +++ b/public/locales/zh-HK/main.json @@ -28,5 +28,9 @@ "promptLibrary": "Prompt 資料庫", "name": "名", "search": "檢索", - "morePrompts": "如果你想揾更多 prompt,撳呢度:" + "morePrompts": "如果你想揾更多 prompt,撳呢度:", + "postOnShareGPT": { + "title": "po 上 ShareGPT", + "warning": "請注意,你將呢個傾偈 po 上 ShareGPT 之後,佢會係公開嘅,所有人都可以見到你寫嘅嘢。一旦 po 咗,呢個傾偈將冇得被隱藏或刪除,亦都可能畀人存檔同分享。我哋建議你諗清楚,唔好喺嗰度分享敏感或私人資料。" + } } diff --git a/public/locales/zh-TW/main.json b/public/locales/zh-TW/main.json index 7bab6cc..899efc9 100644 --- a/public/locales/zh-TW/main.json +++ b/public/locales/zh-TW/main.json @@ -28,5 +28,9 @@ "promptLibrary": "提示詞資料庫", "name": "名稱", "search": "搜尋", - "morePrompts": "更多提示詞請點選:" + "morePrompts": "更多提示詞請點選:", + "postOnShareGPT": { + "title": "發佈至 ShareGPT", + "warning": "請注意,將您的對話發佈至 ShareGPT 後,任何人都可以公開訪問和查看。一旦發佈,對話將無法隱藏或刪除,並且可能被他人存檔或分享。我們建議您慎重考慮,並避免在此平台上分享敏感或私人信息。" + } } diff --git a/src/api/api.ts b/src/api/api.ts index bc33969..9fb32a7 100644 --- a/src/api/api.ts +++ b/src/api/api.ts @@ -1,3 +1,4 @@ +import { ShareGPTSubmitBodyInterface } from '@type/api'; import { ConfigInterface, MessageInterface } from '@type/chat'; export const getChatCompletion = async ( @@ -76,3 +77,18 @@ export const getChatCompletionStream = async ( const stream = response.body; return stream; }; + +export const submitShareGPT = async (body: ShareGPTSubmitBodyInterface) => { + const request = await fetch('https://sharegpt.com/api/conversations', { + body: JSON.stringify(body), + headers: { + 'Content-Type': 'application/json', + }, + method: 'POST', + }); + + const response = await request.json(); + const { id } = response; + const url = `https://shareg.pt/${id}`; + window.open(url, '_blank'); +}; diff --git a/src/components/Chat/ChatContent/ChatContent.tsx b/src/components/Chat/ChatContent/ChatContent.tsx index aafd070..5e194fe 100644 --- a/src/components/Chat/ChatContent/ChatContent.tsx +++ b/src/components/Chat/ChatContent/ChatContent.tsx @@ -10,6 +10,7 @@ import CrossIcon from '@icon/CrossIcon'; import useSubmit from '@hooks/useSubmit'; import DownloadChat from './DownloadChat'; +import ShareGPT from '@components/ShareGPT'; const ChatContent = () => { const inputRole = useStore((state) => state.inputRole); @@ -90,9 +91,12 @@ const ChatContent = () => { )} -
+
{useStore.getState().generating || ( - + <> + + + )}
diff --git a/src/components/ShareGPT/ShareGPT.tsx b/src/components/ShareGPT/ShareGPT.tsx new file mode 100644 index 0000000..8272fe0 --- /dev/null +++ b/src/components/ShareGPT/ShareGPT.tsx @@ -0,0 +1,57 @@ +import React, { useState } from 'react'; +import { useTranslation } from 'react-i18next'; +import useStore from '@store/store'; + +import PopupModal from '@components/PopupModal'; +import { submitShareGPT } from '@api/api'; +import { ShareGPTSubmitBodyInterface } from '@type/api'; + +const ShareGPT = React.memo(() => { + const { t } = useTranslation(); + const [isModalOpen, setIsModalOpen] = useState(false); + + const handleConfirm = async () => { + const chats = useStore.getState().chats; + const currentChatIndex = useStore.getState().currentChatIndex; + if (chats) { + const items: ShareGPTSubmitBodyInterface['items'] = chats[ + currentChatIndex + ].messages.map((message) => ({ + from: 'gpt', + value: `

${t(message.role)}

${message.content}`, + })); + try { + await submitShareGPT({ + avatarUrl: '', + items, + }); + setIsModalOpen(false); + } catch (e: unknown) { + console.log(e); + } + } + }; + + return ( + <> + + {isModalOpen && ( + + )} + + ); +}); + +export default ShareGPT; diff --git a/src/components/ShareGPT/index.ts b/src/components/ShareGPT/index.ts new file mode 100644 index 0000000..2e13447 --- /dev/null +++ b/src/components/ShareGPT/index.ts @@ -0,0 +1 @@ +export { default } from './ShareGPT'; diff --git a/src/types/api.ts b/src/types/api.ts index 54fa696..e32f238 100644 --- a/src/types/api.ts +++ b/src/types/api.ts @@ -16,3 +16,11 @@ export interface EventSourceDataChoices { finish_reason?: string; index: number; } + +export interface ShareGPTSubmitBodyInterface { + avatarUrl: string; + items: { + from: 'gpt' | 'human'; + value: string; + }[]; +}