refactor: Introduce pre-consume quota and unify relay handlers
This commit introduces a major architectural refactoring to improve quota management, centralize logging, and streamline the relay handling logic. Key changes: - **Pre-consume Quota:** Implements a new mechanism to check and reserve user quota *before* making the request to the upstream provider. This ensures more accurate quota deduction and prevents users from exceeding their limits due to concurrent requests. - **Unified Relay Handlers:** Refactors the relay logic to use generic handlers (e.g., `ChatHandler`, `ImageHandler`) instead of provider-specific implementations. This significantly reduces code duplication and simplifies adding new channels. - **Centralized Logger:** A new dedicated `logger` package is introduced, and all system logging calls are migrated to use it, moving this responsibility out of the `common` package. - **Code Reorganization:** DTOs are generalized (e.g., `dalle.go` -> `openai_image.go`) and utility code is moved to more appropriate packages (e.g., `common/http.go` -> `service/http.go`) for better code structure.
This commit is contained in:
45
types/request_meta.go
Normal file
45
types/request_meta.go
Normal file
@@ -0,0 +1,45 @@
|
||||
package types
|
||||
|
||||
type FileType string
|
||||
|
||||
const (
|
||||
FileTypeImage FileType = "image" // Image file type
|
||||
FileTypeAudio FileType = "audio" // Audio file type
|
||||
FileTypeVideo FileType = "video" // Video file type
|
||||
FileTypeFile FileType = "file" // Generic file type
|
||||
)
|
||||
|
||||
type TokenType string
|
||||
|
||||
const (
|
||||
TokenTypeTextNumber TokenType = "text_number" // Text or number tokens
|
||||
TokenTypeTokenizer TokenType = "tokenizer" // Tokenizer tokens
|
||||
TokenTypeImage TokenType = "image" // Image tokens
|
||||
)
|
||||
|
||||
type TokenCountMeta struct {
|
||||
TokenType TokenType `json:"token_type,omitempty"` // Type of tokens used in the request
|
||||
CombineText string `json:"combine_text,omitempty"` // Combined text from all messages
|
||||
ToolsCount int `json:"tools_count,omitempty"` // Number of tools used
|
||||
NameCount int `json:"name_count,omitempty"` // Number of names in the request
|
||||
MessagesCount int `json:"messages_count,omitempty"` // Number of messages in the request
|
||||
Files []*FileMeta `json:"files,omitempty"` // List of files, each with type and content
|
||||
MaxTokens int `json:"max_tokens,omitempty"` // Maximum tokens allowed in the request
|
||||
|
||||
ImagePriceRatio float64 `json:"image_ratio,omitempty"` // Ratio for image size, if applicable
|
||||
//IsStreaming bool `json:"is_streaming,omitempty"` // Indicates if the request is streaming
|
||||
}
|
||||
|
||||
type FileMeta struct {
|
||||
FileType
|
||||
MimeType string
|
||||
Data string
|
||||
Detail string
|
||||
}
|
||||
|
||||
type RequestMeta struct {
|
||||
OriginalModelName string `json:"original_model_name"`
|
||||
UserUsingGroup string `json:"user_using_group"`
|
||||
PromptTokens int `json:"prompt_tokens"`
|
||||
PreConsumedQuota int `json:"pre_consumed_quota"`
|
||||
}
|
||||
Reference in New Issue
Block a user