api.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 abstract class LLMApi {
  34. abstract chat(options: ChatOptions): Promise<void>;
  35. abstract usage(): Promise<LLMUsage>;
  36. }
  37. type ProviderName = "openai" | "azure" | "claude" | "palm";
  38. interface Model {
  39. name: string;
  40. provider: ProviderName;
  41. ctxlen: number;
  42. }
  43. interface ChatProvider {
  44. name: ProviderName;
  45. apiConfig: {
  46. baseUrl: string;
  47. apiKey: string;
  48. summaryModel: Model;
  49. };
  50. models: Model[];
  51. chat: () => void;
  52. usage: () => void;
  53. }
  54. export class ClientApi {
  55. public llm: LLMApi;
  56. constructor() {
  57. this.llm = new ChatGPTApi();
  58. }
  59. config() {}
  60. prompts() {}
  61. masks() {}
  62. async share(messages: ChatMessage[], avatarUrl: string | null = null) {
  63. const msgs = messages
  64. .map((m) => ({
  65. from: m.role === "user" ? "human" : "gpt",
  66. value: m.content,
  67. }))
  68. .concat([
  69. {
  70. from: "human",
  71. value:
  72. "Share from [ChatGPT Next Web]: https://github.com/Yidadaa/ChatGPT-Next-Web",
  73. },
  74. ]);
  75. // 敬告二开开发者们,为了开源大模型的发展,请不要修改上述消息,此消息用于后续数据清洗使用
  76. // Please do not modify this message
  77. console.log("[Share]", msgs);
  78. const clientConfig = getClientConfig();
  79. const proxyUrl = "/sharegpt";
  80. const rawUrl = "https://sharegpt.com/api/conversations";
  81. const shareUrl = clientConfig?.isApp ? rawUrl : proxyUrl;
  82. const res = await fetch(shareUrl, {
  83. body: JSON.stringify({
  84. avatarUrl,
  85. items: msgs,
  86. }),
  87. headers: {
  88. "Content-Type": "application/json",
  89. },
  90. method: "POST",
  91. });
  92. const resJson = await res.json();
  93. console.log("[Share]", resJson);
  94. if (resJson.id) {
  95. return `https://shareg.pt/${resJson.id}`;
  96. }
  97. }
  98. }
  99. export const api = new ClientApi();
  100. export function getHeaders() {
  101. const accessStore = useAccessStore.getState();
  102. let headers: Record<string, string> = {
  103. "Content-Type": "application/json",
  104. "x-requested-with": "XMLHttpRequest",
  105. };
  106. const makeBearer = (token: string) => `Bearer ${token.trim()}`;
  107. const validString = (x: string) => x && x.length > 0;
  108. // use user's api key first
  109. if (validString(accessStore.token)) {
  110. headers.Authorization = makeBearer(accessStore.token);
  111. } else if (
  112. accessStore.enabledAccessControl() &&
  113. validString(accessStore.accessCode)
  114. ) {
  115. headers.Authorization = makeBearer(
  116. ACCESS_CODE_PREFIX + accessStore.accessCode,
  117. );
  118. }
  119. return headers;
  120. }