api.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import { ACCESS_CODE_PREFIX } from "../constant";
  2. import { ChatMessage, ModelConfig, ModelType, useAccessStore } from "../store";
  3. import { ChatGPTApi } from "./platforms/openai";
  4. export const ROLES = ["system", "user", "assistant"] as const;
  5. export type MessageRole = (typeof ROLES)[number];
  6. export const Models = ["gpt-3.5-turbo", "gpt-4"] as const;
  7. export type ChatModel = ModelType;
  8. export interface RequestMessage {
  9. role: MessageRole;
  10. content: string;
  11. }
  12. export interface LLMConfig {
  13. model: string;
  14. temperature?: number;
  15. top_p?: number;
  16. stream?: boolean;
  17. presence_penalty?: number;
  18. frequency_penalty?: number;
  19. }
  20. export interface ChatOptions {
  21. messages: RequestMessage[];
  22. config: LLMConfig;
  23. onUpdate?: (message: string, chunk: string) => void;
  24. onFinish: (message: string) => void;
  25. onError?: (err: Error) => void;
  26. onController?: (controller: AbortController) => void;
  27. }
  28. export interface LLMUsage {
  29. used: number;
  30. total: number;
  31. }
  32. export abstract class LLMApi {
  33. abstract chat(options: ChatOptions): Promise<void>;
  34. abstract usage(): Promise<LLMUsage>;
  35. }
  36. export class ClientApi {
  37. public llm: LLMApi;
  38. constructor() {
  39. this.llm = new ChatGPTApi();
  40. }
  41. config() {}
  42. prompts() {}
  43. masks() {}
  44. async share(messages: ChatMessage[], avatarUrl: string | null = null) {
  45. const msgs = messages
  46. .map((m) => ({
  47. from: m.role === "user" ? "human" : "gpt",
  48. value: m.content,
  49. }))
  50. .concat([
  51. {
  52. from: "human",
  53. value:
  54. "Share from [ChatGPT Next Web]: https://github.com/Yidadaa/ChatGPT-Next-Web",
  55. },
  56. ]);
  57. // 敬告二开开发者们,为了开源大模型的发展,请不要修改上述消息,此消息用于后续数据清洗使用
  58. // Please do not modify this message
  59. console.log("[Share]", msgs);
  60. const res = await fetch("/sharegpt", {
  61. body: JSON.stringify({
  62. avatarUrl,
  63. items: msgs,
  64. }),
  65. headers: {
  66. "Content-Type": "application/json",
  67. },
  68. method: "POST",
  69. });
  70. const resJson = await res.json();
  71. console.log("[Share]", resJson);
  72. if (resJson.id) {
  73. return `https://shareg.pt/${resJson.id}`;
  74. }
  75. }
  76. }
  77. export const api = new ClientApi();
  78. export function getHeaders() {
  79. const accessStore = useAccessStore.getState();
  80. let headers: Record<string, string> = {
  81. "Content-Type": "application/json",
  82. "x-requested-with": "XMLHttpRequest",
  83. };
  84. const makeBearer = (token: string) => `Bearer ${token.trim()}`;
  85. const validString = (x: string) => x && x.length > 0;
  86. // use user's api key first
  87. if (validString(accessStore.token)) {
  88. headers.Authorization = makeBearer(accessStore.token);
  89. } else if (
  90. accessStore.enabledAccessControl() &&
  91. validString(accessStore.accessCode)
  92. ) {
  93. headers.Authorization = makeBearer(
  94. ACCESS_CODE_PREFIX + accessStore.accessCode,
  95. );
  96. }
  97. return headers;
  98. }