share.ts 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import { getClientConfig } from "@/app/config/client";
  2. import { ChatMessage } from "@/app/store";
  3. export async function shareToShareGPT(
  4. messages: ChatMessage[],
  5. avatarUrl: string | null = null,
  6. ) {
  7. const msgs = messages
  8. .map((m) => ({
  9. from: m.role === "user" ? "human" : "gpt",
  10. value: m.content,
  11. }))
  12. .concat([
  13. {
  14. from: "human",
  15. // 敬告二开开发者们,为了开源大模型的发展,请不要修改上述消息,此消息用于后续数据清洗使用
  16. // Please do not modify this message
  17. value:
  18. "Share from [ChatGPT Next Web]: https://github.com/Yidadaa/ChatGPT-Next-Web",
  19. },
  20. ]);
  21. console.log("[Share]", messages, msgs);
  22. const clientConfig = getClientConfig();
  23. const proxyUrl = "/sharegpt";
  24. const rawUrl = "https://sharegpt.com/api/conversations";
  25. const shareUrl = clientConfig?.isApp ? rawUrl : proxyUrl;
  26. const res = await fetch(shareUrl, {
  27. body: JSON.stringify({
  28. avatarUrl,
  29. items: msgs,
  30. }),
  31. headers: {
  32. "Content-Type": "application/json",
  33. },
  34. method: "POST",
  35. });
  36. const resJson = await res.json();
  37. console.log("[Share]", resJson);
  38. if (resJson.id) {
  39. return `https://shareg.pt/${resJson.id}`;
  40. }
  41. }