Add GLM SDK Implementation
- Introduced a new GLM SDK for interacting with the GLM (Zhipu AI) API, designed for chat completions and text generation tasks. - Implemented core components including Client, Service, and SDK layers following Clean Architecture principles. - Added configuration management, error handling, and structured logging for improved usability and maintainability. - Included comprehensive documentation and usage examples to facilitate integration and understanding of the SDK's functionality. - Enhanced the API with features such as chat completion, streaming responses, and model management, ensuring robust interaction with the GLM service.
This commit is contained in:
@@ -0,0 +1,138 @@
|
||||
package glm
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// Config represents the configuration for the GLM SDK
|
||||
type Config struct {
|
||||
// BaseURL is the base URL of the GLM API server
|
||||
BaseURL string
|
||||
|
||||
// APIKey is the authorization token for GLM API
|
||||
APIKey string
|
||||
|
||||
// Timeout is the timeout for HTTP requests
|
||||
Timeout time.Duration
|
||||
|
||||
// RetryAttempts is the number of retry attempts for failed requests
|
||||
RetryAttempts int
|
||||
|
||||
// RetryDelay is the delay between retry attempts
|
||||
RetryDelay time.Duration
|
||||
|
||||
// EnableLogging enables request/response logging
|
||||
EnableLogging bool
|
||||
|
||||
// UserAgent is the user agent string for HTTP requests
|
||||
UserAgent string
|
||||
|
||||
// DefaultModel is the default model to use if none is specified
|
||||
DefaultModel string
|
||||
|
||||
// DefaultMaxTokens is the default maximum number of tokens to generate
|
||||
DefaultMaxTokens int
|
||||
|
||||
// DefaultTopP is the default top_p value for text generation
|
||||
DefaultTopP float64
|
||||
|
||||
// DefaultTemperature is the default temperature for text generation
|
||||
DefaultTemperature float64
|
||||
|
||||
// DefaultFrequencyPenalty is the default frequency penalty
|
||||
DefaultFrequencyPenalty float64
|
||||
|
||||
// DefaultPresencePenalty is the default presence penalty
|
||||
DefaultPresencePenalty float64
|
||||
|
||||
// EnableStreaming enables streaming responses by default
|
||||
EnableStreaming bool
|
||||
|
||||
// EnableThinking enables thinking mode by default
|
||||
EnableThinking bool
|
||||
|
||||
// TranslationConfig is the translation configuration
|
||||
TranslationOptions TranslationOption
|
||||
}
|
||||
|
||||
// TranslationOption represents the translation options
|
||||
type TranslationOption struct {
|
||||
Model string
|
||||
MaxTokens int
|
||||
Temperature float64
|
||||
FrequencyPenalty float64
|
||||
PresencePenalty float64
|
||||
TopP float64
|
||||
EnableThinking bool
|
||||
}
|
||||
|
||||
// DefaultConfig returns a default configuration for the GLM SDK
|
||||
func DefaultConfig() *Config {
|
||||
return &Config{
|
||||
BaseURL: "https://api.z.ai",
|
||||
APIKey: "",
|
||||
Timeout: 300 * time.Second,
|
||||
RetryAttempts: 3,
|
||||
RetryDelay: 2 * time.Second,
|
||||
EnableLogging: true,
|
||||
UserAgent: "Opplens-GLMSDK/1.0",
|
||||
DefaultModel: "glm-4.5",
|
||||
DefaultMaxTokens: 256,
|
||||
DefaultTopP: 1.0,
|
||||
DefaultTemperature: 0.5,
|
||||
DefaultFrequencyPenalty: 0,
|
||||
DefaultPresencePenalty: 0,
|
||||
EnableStreaming: false,
|
||||
EnableThinking: false,
|
||||
TranslationOptions: TranslationOption{
|
||||
Model: "glm-4.5",
|
||||
MaxTokens: 256,
|
||||
Temperature: 0.5,
|
||||
FrequencyPenalty: 0,
|
||||
PresencePenalty: 0,
|
||||
TopP: 1.0,
|
||||
EnableThinking: false,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// Validate validates the configuration
|
||||
func (c *Config) Validate() error {
|
||||
if c.BaseURL == "" {
|
||||
return ErrInvalidConfig("BaseURL cannot be empty")
|
||||
}
|
||||
|
||||
if c.Timeout <= 0 {
|
||||
return ErrInvalidConfig("Timeout must be positive")
|
||||
}
|
||||
|
||||
if c.RetryAttempts < 0 {
|
||||
return ErrInvalidConfig("RetryAttempts cannot be negative")
|
||||
}
|
||||
|
||||
if c.RetryDelay < 0 {
|
||||
return ErrInvalidConfig("RetryDelay cannot be negative")
|
||||
}
|
||||
|
||||
if c.DefaultMaxTokens < 1 || c.DefaultMaxTokens > 32768 {
|
||||
return ErrInvalidConfig("DefaultMaxTokens must be between 1 and 32768")
|
||||
}
|
||||
|
||||
if c.DefaultTopP < 0 || c.DefaultTopP > 1 {
|
||||
return ErrInvalidConfig("DefaultTopP must be between 0 and 1")
|
||||
}
|
||||
|
||||
if c.DefaultTemperature < 0 || c.DefaultTemperature > 2 {
|
||||
return ErrInvalidConfig("DefaultTemperature must be between 0 and 2")
|
||||
}
|
||||
|
||||
if c.DefaultFrequencyPenalty < -2 || c.DefaultFrequencyPenalty > 2 {
|
||||
return ErrInvalidConfig("DefaultFrequencyPenalty must be between -2 and 2")
|
||||
}
|
||||
|
||||
if c.DefaultPresencePenalty < -2 || c.DefaultPresencePenalty > 2 {
|
||||
return ErrInvalidConfig("DefaultPresencePenalty must be between -2 and 2")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user