a5d3a94c97
- 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.
95 lines
2.4 KiB
Go
95 lines
2.4 KiB
Go
package glm
|
|
|
|
import (
|
|
"fmt"
|
|
)
|
|
|
|
// Error represents a GLM SDK error
|
|
type Error struct {
|
|
Code string
|
|
Message string
|
|
Cause error
|
|
}
|
|
|
|
// Error implements the error interface
|
|
func (e *Error) Error() string {
|
|
if e.Cause != nil {
|
|
return fmt.Sprintf("glm: %s: %s: %v", e.Code, e.Message, e.Cause)
|
|
}
|
|
return fmt.Sprintf("glm: %s: %s", e.Code, e.Message)
|
|
}
|
|
|
|
// Unwrap returns the underlying cause of the error
|
|
func (e *Error) Unwrap() error {
|
|
return e.Cause
|
|
}
|
|
|
|
// NewError creates a new GLM error
|
|
func NewError(code, message string, cause error) *Error {
|
|
return &Error{
|
|
Code: code,
|
|
Message: message,
|
|
Cause: cause,
|
|
}
|
|
}
|
|
|
|
// Error codes
|
|
const (
|
|
ErrCodeInvalidConfig = "INVALID_CONFIG"
|
|
ErrCodeHTTPError = "HTTP_ERROR"
|
|
ErrCodeInvalidRequest = "INVALID_REQUEST"
|
|
ErrCodeAuthentication = "AUTHENTICATION_ERROR"
|
|
ErrCodeRateLimit = "RATE_LIMIT_ERROR"
|
|
ErrCodeServerError = "SERVER_ERROR"
|
|
ErrCodeInvalidResponse = "INVALID_RESPONSE"
|
|
ErrCodeChatCompletion = "CHAT_COMPLETION_ERROR"
|
|
ErrCodeModelNotFound = "MODEL_NOT_FOUND"
|
|
ErrCodeTokenLimit = "TOKEN_LIMIT_ERROR"
|
|
ErrCodeStreamError = "STREAM_ERROR"
|
|
)
|
|
|
|
// Error constructors
|
|
func ErrInvalidConfig(message string) *Error {
|
|
return NewError(ErrCodeInvalidConfig, message, nil)
|
|
}
|
|
|
|
func ErrHTTPError(statusCode int, message string) *Error {
|
|
return NewError(ErrCodeHTTPError, fmt.Sprintf("HTTP %d: %s", statusCode, message), nil)
|
|
}
|
|
|
|
func ErrInvalidRequest(message string) *Error {
|
|
return NewError(ErrCodeInvalidRequest, message, nil)
|
|
}
|
|
|
|
func ErrAuthentication(message string) *Error {
|
|
return NewError(ErrCodeAuthentication, message, nil)
|
|
}
|
|
|
|
func ErrRateLimit(message string) *Error {
|
|
return NewError(ErrCodeRateLimit, message, nil)
|
|
}
|
|
|
|
func ErrServerError(message string) *Error {
|
|
return NewError(ErrCodeServerError, message, nil)
|
|
}
|
|
|
|
func ErrInvalidResponse(message string) *Error {
|
|
return NewError(ErrCodeInvalidResponse, message, nil)
|
|
}
|
|
|
|
func ErrChatCompletion(message string, cause error) *Error {
|
|
return NewError(ErrCodeChatCompletion, message, cause)
|
|
}
|
|
|
|
func ErrModelNotFound(model string) *Error {
|
|
return NewError(ErrCodeModelNotFound, fmt.Sprintf("model '%s' not found", model), nil)
|
|
}
|
|
|
|
func ErrTokenLimit(message string) *Error {
|
|
return NewError(ErrCodeTokenLimit, message, nil)
|
|
}
|
|
|
|
func ErrStreamError(message string, cause error) *Error {
|
|
return NewError(ErrCodeStreamError, message, cause)
|
|
}
|