-
Notifications
You must be signed in to change notification settings - Fork 59.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1495 from Yidadaa/refactor-api
- Loading branch information
Showing
17 changed files
with
511 additions
and
594 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import { ACCESS_CODE_PREFIX } from "../constant"; | ||
import { ModelConfig, ModelType, useAccessStore } from "../store"; | ||
import { ChatGPTApi } from "./platforms/openai"; | ||
|
||
export const ROLES = ["system", "user", "assistant"] as const; | ||
export type MessageRole = (typeof ROLES)[number]; | ||
|
||
export const Models = ["gpt-3.5-turbo", "gpt-4"] as const; | ||
export type ChatModel = ModelType; | ||
|
||
export interface RequestMessage { | ||
role: MessageRole; | ||
content: string; | ||
} | ||
|
||
export interface LLMConfig { | ||
model: string; | ||
temperature?: number; | ||
top_p?: number; | ||
stream?: boolean; | ||
presence_penalty?: number; | ||
frequency_penalty?: number; | ||
} | ||
|
||
export interface ChatOptions { | ||
messages: RequestMessage[]; | ||
config: LLMConfig; | ||
|
||
onUpdate?: (message: string, chunk: string) => void; | ||
onFinish: (message: string) => void; | ||
onError?: (err: Error) => void; | ||
onController?: (controller: AbortController) => void; | ||
} | ||
|
||
export interface LLMUsage { | ||
used: number; | ||
total: number; | ||
} | ||
|
||
export abstract class LLMApi { | ||
abstract chat(options: ChatOptions): Promise<void>; | ||
abstract usage(): Promise<LLMUsage>; | ||
} | ||
|
||
export class ClientApi { | ||
public llm: LLMApi; | ||
|
||
constructor() { | ||
this.llm = new ChatGPTApi(); | ||
} | ||
|
||
config() {} | ||
|
||
prompts() {} | ||
|
||
masks() {} | ||
} | ||
|
||
export const api = new ClientApi(); | ||
|
||
export function getHeaders() { | ||
const accessStore = useAccessStore.getState(); | ||
let headers: Record<string, string> = { | ||
"Content-Type": "application/json", | ||
}; | ||
|
||
const makeBearer = (token: string) => `Bearer ${token.trim()}`; | ||
const validString = (x: string) => x && x.length > 0; | ||
|
||
// use user's api key first | ||
if (validString(accessStore.token)) { | ||
headers.Authorization = makeBearer(accessStore.token); | ||
} else if ( | ||
accessStore.enabledAccessControl() && | ||
validString(accessStore.accessCode) | ||
) { | ||
headers.Authorization = makeBearer( | ||
ACCESS_CODE_PREFIX + accessStore.accessCode, | ||
); | ||
} | ||
|
||
return headers; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// To store message streaming controller | ||
export const ChatControllerPool = { | ||
controllers: {} as Record<string, AbortController>, | ||
|
||
addController( | ||
sessionIndex: number, | ||
messageId: number, | ||
controller: AbortController, | ||
) { | ||
const key = this.key(sessionIndex, messageId); | ||
this.controllers[key] = controller; | ||
return key; | ||
}, | ||
|
||
stop(sessionIndex: number, messageId: number) { | ||
const key = this.key(sessionIndex, messageId); | ||
const controller = this.controllers[key]; | ||
controller?.abort(); | ||
}, | ||
|
||
stopAll() { | ||
Object.values(this.controllers).forEach((v) => v.abort()); | ||
}, | ||
|
||
hasPending() { | ||
return Object.values(this.controllers).length > 0; | ||
}, | ||
|
||
remove(sessionIndex: number, messageId: number) { | ||
const key = this.key(sessionIndex, messageId); | ||
delete this.controllers[key]; | ||
}, | ||
|
||
key(sessionIndex: number, messageIndex: number) { | ||
return `${sessionIndex},${messageIndex}`; | ||
}, | ||
}; |
Oops, something went wrong.