154610e2a0
- Introduced the Ollama SDK, providing a comprehensive interface for interacting with Ollama AI models, including text generation, chat conversations, embeddings, and model management. - Implemented core components such as Client, Config, and various request/response entities to facilitate API interactions. - Added structured error handling with specific error types for improved clarity and debugging. - Included a README and usage guide to assist developers in integrating the SDK into their applications, ensuring a smooth onboarding experience. - Established a fluent API for chat interactions and streaming responses, enhancing usability and flexibility for developers.
92 lines
2.8 KiB
Go
92 lines
2.8 KiB
Go
package ollama
|
|
|
|
import (
|
|
"time"
|
|
)
|
|
|
|
// Config represents the configuration for the Ollama SDK
|
|
type Config struct {
|
|
// BaseURL is the base URL of the Ollama API server
|
|
BaseURL string `env:"OLLAMA_BASE_URL" default:"http://localhost:11434"`
|
|
|
|
// Timeout is the timeout for HTTP requests
|
|
Timeout time.Duration `env:"OLLAMA_TIMEOUT" default:"300s"`
|
|
|
|
// RetryAttempts is the number of retry attempts for failed requests
|
|
RetryAttempts int `env:"OLLAMA_RETRY_ATTEMPTS" default:"3"`
|
|
|
|
// RetryDelay is the delay between retry attempts
|
|
RetryDelay time.Duration `env:"OLLAMA_RETRY_DELAY" default:"2s"`
|
|
|
|
// EnableLogging enables request/response logging
|
|
EnableLogging bool `env:"OLLAMA_ENABLE_LOGGING" default:"true"`
|
|
|
|
// UserAgent is the user agent string for HTTP requests
|
|
UserAgent string `env:"OLLAMA_USER_AGENT" default:"TenderManagement-OllamaSDK/1.0"`
|
|
|
|
// DefaultModel is the default model to use if none is specified
|
|
DefaultModel string `env:"OLLAMA_DEFAULT_MODEL" default:"llama2"`
|
|
|
|
// DefaultTemperature is the default temperature for text generation
|
|
DefaultTemperature float64 `env:"OLLAMA_DEFAULT_TEMPERATURE" default:"0.7"`
|
|
|
|
// DefaultTopP is the default top_p value for text generation
|
|
DefaultTopP float64 `env:"OLLAMA_DEFAULT_TOP_P" default:"0.9"`
|
|
|
|
// DefaultMaxTokens is the default maximum number of tokens to generate
|
|
DefaultMaxTokens int `env:"OLLAMA_DEFAULT_MAX_TOKENS" default:"1000"`
|
|
|
|
// EnableStreaming enables streaming responses by default
|
|
EnableStreaming bool `env:"OLLAMA_ENABLE_STREAMING" default:"false"`
|
|
}
|
|
|
|
// DefaultConfig returns a default configuration for the Ollama SDK
|
|
func DefaultConfig() *Config {
|
|
return &Config{
|
|
BaseURL: "http://localhost:11434",
|
|
Timeout: 300 * time.Second,
|
|
RetryAttempts: 3,
|
|
RetryDelay: 2 * time.Second,
|
|
EnableLogging: true,
|
|
UserAgent: "TenderManagement-OllamaSDK/1.0",
|
|
DefaultModel: "llama2",
|
|
DefaultTemperature: 0.7,
|
|
DefaultTopP: 0.9,
|
|
DefaultMaxTokens: 1000,
|
|
EnableStreaming: 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.DefaultTemperature < 0 || c.DefaultTemperature > 2 {
|
|
return ErrInvalidConfig("DefaultTemperature must be between 0 and 2")
|
|
}
|
|
|
|
if c.DefaultTopP < 0 || c.DefaultTopP > 1 {
|
|
return ErrInvalidConfig("DefaultTopP must be between 0 and 1")
|
|
}
|
|
|
|
if c.DefaultMaxTokens < 0 {
|
|
return ErrInvalidConfig("DefaultMaxTokens cannot be negative")
|
|
}
|
|
|
|
return nil
|
|
}
|