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,92 @@
|
||||
package glm
|
||||
|
||||
// ChatCompletionRequest represents a request for chat completion
|
||||
type ChatCompletionRequest struct {
|
||||
Model string `json:"model" validate:"required"`
|
||||
Messages []Message `json:"messages" validate:"required,min=1"`
|
||||
Thinking *Thinking `json:"thinking,omitempty"`
|
||||
MaxTokens int `json:"max_tokens,omitempty" validate:"min=1,max=32768"`
|
||||
TopP float64 `json:"top_p,omitempty" validate:"min=0,max=1"`
|
||||
FrequencyPenalty float64 `json:"frequency_penalty,omitempty" validate:"min=-2,max=2"`
|
||||
PresencePenalty float64 `json:"presence_penalty,omitempty" validate:"min=-2,max=2"`
|
||||
Temperature float64 `json:"temperature,omitempty" validate:"min=0,max=2"`
|
||||
Stop []string `json:"stop,omitempty"`
|
||||
Stream bool `json:"stream,omitempty"`
|
||||
}
|
||||
|
||||
// Message represents a message in a chat conversation
|
||||
type Message struct {
|
||||
Role MessageRole `json:"role" validate:"required,oneof=system user assistant"`
|
||||
Content string `json:"content" validate:"required"`
|
||||
}
|
||||
|
||||
// MessageRole defines the role of a message
|
||||
type MessageRole string
|
||||
|
||||
const (
|
||||
MessageRoleSystem MessageRole = "system"
|
||||
MessageRoleUser MessageRole = "user"
|
||||
MessageRoleAssistant MessageRole = "assistant"
|
||||
)
|
||||
|
||||
// Thinking represents the thinking configuration for GLM models
|
||||
type Thinking struct {
|
||||
Type string `json:"type" validate:"required,oneof=true false"`
|
||||
}
|
||||
|
||||
// ChatCompletionResponse represents the response from a chat completion request
|
||||
type ChatCompletionResponse struct {
|
||||
ID string `json:"id"`
|
||||
Object string `json:"object"`
|
||||
Created int64 `json:"created"`
|
||||
Model string `json:"model"`
|
||||
Choices []Choice `json:"choices"`
|
||||
Usage Usage `json:"usage"`
|
||||
SystemFingerprint string `json:"system_fingerprint,omitempty"`
|
||||
}
|
||||
|
||||
// Choice represents a single choice in the chat completion response
|
||||
type Choice struct {
|
||||
Index int `json:"index"`
|
||||
Message Message `json:"message"`
|
||||
FinishReason string `json:"finish_reason"`
|
||||
}
|
||||
|
||||
// Usage represents token usage information
|
||||
type Usage struct {
|
||||
PromptTokens int `json:"prompt_tokens"`
|
||||
CompletionTokens int `json:"completion_tokens"`
|
||||
TotalTokens int `json:"total_tokens"`
|
||||
}
|
||||
|
||||
// StreamChatCompletionResponse represents a streaming response chunk
|
||||
type StreamChatCompletionResponse struct {
|
||||
ID string `json:"id"`
|
||||
Object string `json:"object"`
|
||||
Created int64 `json:"created"`
|
||||
Model string `json:"model"`
|
||||
Choices []StreamChoice `json:"choices"`
|
||||
Usage *Usage `json:"usage,omitempty"`
|
||||
SystemFingerprint string `json:"system_fingerprint,omitempty"`
|
||||
}
|
||||
|
||||
// StreamChoice represents a single choice in a streaming response
|
||||
type StreamChoice struct {
|
||||
Index int `json:"index"`
|
||||
Delta Message `json:"delta"`
|
||||
FinishReason *string `json:"finish_reason,omitempty"`
|
||||
}
|
||||
|
||||
// Model represents information about available models
|
||||
type Model struct {
|
||||
ID string `json:"id"`
|
||||
Object string `json:"object"`
|
||||
Created int64 `json:"created"`
|
||||
OwnedBy string `json:"owned_by"`
|
||||
}
|
||||
|
||||
// ModelsResponse represents the response from listing available models
|
||||
type ModelsResponse struct {
|
||||
Object string `json:"object"`
|
||||
Data []Model `json:"data"`
|
||||
}
|
||||
Reference in New Issue
Block a user