Spaces:
Runtime error
Runtime error
File size: 6,229 Bytes
41af422 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 |
import type { ObjectId } from 'mongodb'
import type { TextAuditServiceOptions, TextAuditServiceProvider } from 'src/utils/textAudit'
export enum Status {
Normal = 0,
Deleted = 1,
InversionDeleted = 2,
ResponseDeleted = 3,
PreVerify = 4,
AdminVerify = 5,
Disabled = 6,
}
export enum UserRole {
Admin = 0,
User = 1,
Guest = 2,
Support = 3,
Viewer = 4,
Contributor = 5,
Developer = 6,
Tester = 7,
Partner = 8,
}
export class UserInfo {
_id: ObjectId
name: string
email: string
password: string
status: Status
createTime: string
verifyTime?: string
avatar?: string
description?: string
updateTime?: string
config?: UserConfig
roles?: UserRole[]
constructor(email: string, password: string) {
this.name = email
this.email = email
this.password = password
this.status = Status.PreVerify
this.createTime = new Date().toLocaleString()
this.verifyTime = null
this.updateTime = new Date().toLocaleString()
this.roles = [UserRole.User]
}
}
export class UserConfig {
chatModel: CHATMODEL
}
// https://platform.openai.com/docs/models/overview
export type CHATMODEL = 'gpt-3.5-turbo' | 'gpt-3.5-turbo-0301' | 'gpt-4' | 'gpt-4-0314' | 'gpt-4-32k' | 'gpt-4-32k-0314' | 'text-davinci-002-render-sha' | 'text-davinci-002-render-sha-mobile' | 'gpt-4-mobile' | 'gpt-4-browsing'
export const CHATMODELS: CHATMODEL[] = [
'gpt-3.5-turbo',
'gpt-3.5-turbo-0301',
'gpt-4',
'gpt-4-0314',
'gpt-4-32k',
'gpt-4-32k-0314',
'text-davinci-002-render-sha',
'text-davinci-002-render-sha-mobile',
'gpt-4-mobile',
'gpt-4-browsing',
]
export const chatModelOptions = [
'gpt-3.5-turbo',
'gpt-3.5-turbo-0301',
'gpt-4',
'gpt-4-0314',
'gpt-4-32k',
'gpt-4-32k-0314',
'text-davinci-002-render-sha',
'text-davinci-002-render-sha-mobile',
'gpt-4-mobile',
'gpt-4-browsing',
].map((model: string) => {
let label = model
if (model === 'text-davinci-002-render-sha')
label = 'gpt-3.5-browsing'
if (model === 'text-davinci-002-render-sha-mobile')
label = 'gpt-3.5-mobile'
return {
label,
key: model,
value: model,
}
})
export class ChatRoom {
_id: ObjectId
roomId: number
userId: string
title: string
prompt: string
usingContext: boolean
status: Status = Status.Normal
constructor(userId: string, title: string, roomId: number) {
this.userId = userId
this.title = title
this.prompt = undefined
this.roomId = roomId
this.usingContext = true
}
}
export class ChatOptions {
parentMessageId?: string
messageId?: string
conversationId?: string
prompt_tokens?: number
completion_tokens?: number
total_tokens?: number
estimated?: boolean
constructor(parentMessageId?: string, messageId?: string, conversationId?: string) {
this.parentMessageId = parentMessageId
this.messageId = messageId
this.conversationId = conversationId
}
}
export class previousResponse {
response: string
options: ChatOptions
}
export class ChatInfo {
_id: ObjectId
roomId: number
uuid: number
dateTime: number
prompt: string
response?: string
status: Status = Status.Normal
options: ChatOptions
previousResponse?: previousResponse[]
constructor(roomId: number, uuid: number, prompt: string, options: ChatOptions) {
this.roomId = roomId
this.uuid = uuid
this.prompt = prompt
this.options = options
this.dateTime = new Date().getTime()
}
}
export class UsageResponse {
prompt_tokens: number
completion_tokens: number
total_tokens: number
estimated: boolean
}
export class ChatUsage {
_id: ObjectId
userId: ObjectId
roomId: number
chatId: ObjectId
messageId: string
promptTokens: number
completionTokens: number
totalTokens: number
estimated: boolean
dateTime: number
constructor(userId: ObjectId, roomId: number, chatId: ObjectId, messageId: string, usage: UsageResponse) {
this.userId = userId
this.roomId = roomId
this.chatId = chatId
this.messageId = messageId
if (usage) {
this.promptTokens = usage.prompt_tokens
this.completionTokens = usage.completion_tokens
this.totalTokens = usage.total_tokens
this.estimated = usage.estimated
}
this.dateTime = new Date().getTime()
}
}
export class Config {
constructor(
public _id: ObjectId,
public timeoutMs: number,
public apiKey?: string,
public apiDisableDebug?: boolean,
public accessToken?: string,
public apiBaseUrl?: string,
public apiModel?: APIMODEL,
public reverseProxy?: string,
public socksProxy?: string,
public socksAuth?: string,
public httpsProxy?: string,
public siteConfig?: SiteConfig,
public mailConfig?: MailConfig,
public auditConfig?: AuditConfig,
) { }
}
export class SiteConfig {
constructor(
public siteTitle?: string,
public loginEnabled?: boolean,
public loginSalt?: string,
public registerEnabled?: boolean,
public registerReview?: boolean,
public registerMails?: string,
public siteDomain?: string,
) { }
}
export class MailConfig {
constructor(
public smtpHost: string,
public smtpPort: number,
public smtpTsl: boolean,
public smtpUserName: string,
public smtpPassword: string,
) { }
}
export class AuditConfig {
constructor(
public enabled: boolean,
public provider: TextAuditServiceProvider,
public options: TextAuditServiceOptions,
public textType: TextAudioType,
public customizeEnabled: boolean,
public sensitiveWords: string,
) { }
}
export enum TextAudioType {
None = 0,
Request = 1 << 0, // 二进制 01
Response = 1 << 1, // 二进制 10
All = Request | Response, // 二进制 11
}
export class KeyConfig {
_id: ObjectId
key: string
keyModel: APIMODEL
chatModels: CHATMODEL[]
userRoles: UserRole[]
status: Status
remark: string
constructor(key: string, keyModel: APIMODEL, chatModels: CHATMODEL[], userRoles: UserRole[], remark: string) {
this.key = key
this.keyModel = keyModel
this.chatModels = chatModels
this.userRoles = userRoles
this.status = Status.Normal
this.remark = remark
}
}
export type APIMODEL = 'ChatGPTAPI' | 'ChatGPTUnofficialProxyAPI' |