api.ts 3.6 KB

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