106 lines
3.1 KiB
Go
106 lines
3.1 KiB
Go
package scriptorium
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"gitea.maximumdirect.net/eric/scriptorium/internal/domain"
|
|
"gitea.maximumdirect.net/eric/scriptorium/internal/profile"
|
|
)
|
|
|
|
// OpenAICompatibleProfile returns an in-memory profile for an OpenAI-compatible
|
|
// chat-completions endpoint.
|
|
func OpenAICompatibleProfile(cfg OpenAICompatibleProfileConfig) Profile {
|
|
return Profile{
|
|
ID: cfg.ID,
|
|
Endpoint: cfg.Endpoint,
|
|
Model: cfg.Model,
|
|
Temperature: cfg.Temperature,
|
|
MaxTokens: cfg.MaxTokens,
|
|
TopP: cfg.TopP,
|
|
TimeoutSeconds: cfg.TimeoutSeconds,
|
|
ServiceTier: cfg.ServiceTier,
|
|
ReasoningEffort: cfg.ReasoningEffort,
|
|
APIKeyRequired: cfg.APIKeyRequired,
|
|
ExtraParams: copyAnyMap(cfg.ExtraParams),
|
|
}
|
|
}
|
|
|
|
type memoryProfileRepository struct {
|
|
profiles map[string]domain.ExecutionProfile
|
|
}
|
|
|
|
func newMemoryProfileRepository(profiles []Profile) (*memoryProfileRepository, error) {
|
|
repo := &memoryProfileRepository{profiles: make(map[string]domain.ExecutionProfile, len(profiles))}
|
|
for _, publicProfile := range profiles {
|
|
prof, err := toDomainProfile(publicProfile)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if _, exists := repo.profiles[prof.ID]; exists {
|
|
return nil, fmt.Errorf("duplicate profile id %q", prof.ID)
|
|
}
|
|
repo.profiles[prof.ID] = prof
|
|
}
|
|
return repo, nil
|
|
}
|
|
|
|
func (r *memoryProfileRepository) GetProfile(_ context.Context, id string) (*domain.ExecutionProfile, error) {
|
|
if r == nil {
|
|
return nil, profile.ErrProfileNotFound
|
|
}
|
|
prof, ok := r.profiles[id]
|
|
if !ok {
|
|
return nil, profile.ErrProfileNotFound
|
|
}
|
|
prof.ExtraParams = copyAnyMap(prof.ExtraParams)
|
|
return &prof, nil
|
|
}
|
|
|
|
func toDomainProfile(publicProfile Profile) (domain.ExecutionProfile, error) {
|
|
prof := domain.ExecutionProfile{
|
|
ID: strings.TrimSpace(publicProfile.ID),
|
|
Endpoint: publicProfile.Endpoint,
|
|
Model: publicProfile.Model,
|
|
Temperature: publicProfile.Temperature,
|
|
MaxTokens: publicProfile.MaxTokens,
|
|
TopP: publicProfile.TopP,
|
|
TimeoutSeconds: publicProfile.TimeoutSeconds,
|
|
ServiceTier: publicProfile.ServiceTier,
|
|
ReasoningEffort: publicProfile.ReasoningEffort,
|
|
APIKeyRequired: publicProfile.APIKeyRequired,
|
|
ExtraParams: copyAnyMap(publicProfile.ExtraParams),
|
|
}
|
|
if err := validatePublicProfile(prof); err != nil {
|
|
return domain.ExecutionProfile{}, err
|
|
}
|
|
return prof, nil
|
|
}
|
|
|
|
func validatePublicProfile(prof domain.ExecutionProfile) error {
|
|
if strings.TrimSpace(prof.ID) == "" {
|
|
return errors.New("id is required")
|
|
}
|
|
if strings.TrimSpace(prof.Endpoint) == "" {
|
|
return errors.New("endpoint is required")
|
|
}
|
|
if strings.TrimSpace(prof.Model) == "" {
|
|
return errors.New("model is required")
|
|
}
|
|
if prof.Temperature < 0 || prof.Temperature > 2 {
|
|
return errors.New("temperature must be between 0 and 2")
|
|
}
|
|
if prof.MaxTokens < 0 {
|
|
return errors.New("max_tokens must be greater than or equal to 0")
|
|
}
|
|
if prof.TopP < 0 || prof.TopP > 1 {
|
|
return errors.New("top_p must be between 0 and 1")
|
|
}
|
|
if prof.TimeoutSeconds < 0 {
|
|
return errors.New("timeout_seconds must be greater than or equal to 0")
|
|
}
|
|
return nil
|
|
}
|