|
| 1 | +package converter |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/thushan/olla/internal/core/domain" |
| 5 | +) |
| 6 | + |
| 7 | +// BaseConverter provides common functionality for all converters |
| 8 | +type BaseConverter struct { |
| 9 | + providerType string |
| 10 | +} |
| 11 | + |
| 12 | +// NewBaseConverter creates a new base converter |
| 13 | +func NewBaseConverter(providerType string) *BaseConverter { |
| 14 | + return &BaseConverter{ |
| 15 | + providerType: providerType, |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +// FindProviderAlias finds the provider-specific alias for a unified model |
| 20 | +func (b *BaseConverter) FindProviderAlias(model *domain.UnifiedModel) (string, bool) { |
| 21 | + for _, alias := range model.Aliases { |
| 22 | + if alias.Source == b.providerType { |
| 23 | + return alias.Name, true |
| 24 | + } |
| 25 | + } |
| 26 | + return "", false |
| 27 | +} |
| 28 | + |
| 29 | +// FindProviderEndpoint finds the provider-specific endpoint for a unified model |
| 30 | +func (b *BaseConverter) FindProviderEndpoint(model *domain.UnifiedModel, providerName string) *domain.SourceEndpoint { |
| 31 | + for i := range model.SourceEndpoints { |
| 32 | + ep := &model.SourceEndpoints[i] |
| 33 | + if providerName != "" && ep.NativeName == providerName { |
| 34 | + return ep |
| 35 | + } |
| 36 | + } |
| 37 | + return nil |
| 38 | +} |
| 39 | + |
| 40 | +// ExtractMetadataString safely extracts a string value from metadata |
| 41 | +func (b *BaseConverter) ExtractMetadataString(metadata map[string]interface{}, key string) string { |
| 42 | + if val, ok := metadata[key].(string); ok { |
| 43 | + return val |
| 44 | + } |
| 45 | + return "" |
| 46 | +} |
| 47 | + |
| 48 | +// ExtractMetadataInt safely extracts an int value from metadata |
| 49 | +func (b *BaseConverter) ExtractMetadataInt(metadata map[string]interface{}, key string) int { |
| 50 | + if val, ok := metadata[key].(int); ok { |
| 51 | + return val |
| 52 | + } |
| 53 | + if val, ok := metadata[key].(float64); ok { |
| 54 | + return int(val) |
| 55 | + } |
| 56 | + return 0 |
| 57 | +} |
| 58 | + |
| 59 | +// ExtractMetadataBool safely extracts a bool value from metadata |
| 60 | +func (b *BaseConverter) ExtractMetadataBool(metadata map[string]interface{}, key string) bool { |
| 61 | + if val, ok := metadata[key].(bool); ok { |
| 62 | + return val |
| 63 | + } |
| 64 | + return false |
| 65 | +} |
| 66 | + |
| 67 | +// GetEndpointDiskSize returns the disk size from endpoint or model |
| 68 | +func (b *BaseConverter) GetEndpointDiskSize(model *domain.UnifiedModel, endpoint *domain.SourceEndpoint) int64 { |
| 69 | + if endpoint != nil && endpoint.DiskSize > 0 { |
| 70 | + return endpoint.DiskSize |
| 71 | + } |
| 72 | + return model.DiskSize |
| 73 | +} |
| 74 | + |
| 75 | +// DetermineModelState determines the model state from endpoint |
| 76 | +func (b *BaseConverter) DetermineModelState(endpoint *domain.SourceEndpoint, defaultState string) string { |
| 77 | + if endpoint != nil && endpoint.State != "" { |
| 78 | + return endpoint.State |
| 79 | + } |
| 80 | + return defaultState |
| 81 | +} |
| 82 | + |
| 83 | +// HasCapability checks if a model has a specific capability |
| 84 | +func (b *BaseConverter) HasCapability(capabilities []string, capability string) bool { |
| 85 | + return hasCapability(capabilities, capability) |
| 86 | +} |
| 87 | + |
| 88 | +// DetermineModelType determines the model type based on capabilities and metadata |
| 89 | +func (b *BaseConverter) DetermineModelType(model *domain.UnifiedModel, defaultType string) string { |
| 90 | + // Check metadata first |
| 91 | + if modelType := b.ExtractMetadataString(model.Metadata, "type"); modelType != "" { |
| 92 | + return modelType |
| 93 | + } |
| 94 | + |
| 95 | + // Check capabilities |
| 96 | + if b.HasCapability(model.Capabilities, "vision") { |
| 97 | + return "vlm" |
| 98 | + } |
| 99 | + if b.HasCapability(model.Capabilities, "embedding") || b.HasCapability(model.Capabilities, "embeddings") { |
| 100 | + return "embeddings" |
| 101 | + } |
| 102 | + |
| 103 | + return defaultType |
| 104 | +} |
| 105 | + |
| 106 | +// ConversionHelper groups all helper methods for use in concrete converters |
| 107 | +type ConversionHelper struct { |
| 108 | + *BaseConverter |
| 109 | + Model *domain.UnifiedModel |
| 110 | + Endpoint *domain.SourceEndpoint |
| 111 | + Alias string |
| 112 | +} |
| 113 | + |
| 114 | +// NewConversionHelper creates a helper for converting a specific model |
| 115 | +func (b *BaseConverter) NewConversionHelper(model *domain.UnifiedModel) *ConversionHelper { |
| 116 | + alias, _ := b.FindProviderAlias(model) |
| 117 | + endpoint := b.FindProviderEndpoint(model, alias) |
| 118 | + |
| 119 | + return &ConversionHelper{ |
| 120 | + BaseConverter: b, |
| 121 | + Model: model, |
| 122 | + Alias: alias, |
| 123 | + Endpoint: endpoint, |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +// ShouldSkip returns true if this model should be skipped for this provider |
| 128 | +func (h *ConversionHelper) ShouldSkip() bool { |
| 129 | + return h.Alias == "" |
| 130 | +} |
| 131 | + |
| 132 | +// GetDiskSize returns the appropriate disk size for the model |
| 133 | +func (h *ConversionHelper) GetDiskSize() int64 { |
| 134 | + return h.GetEndpointDiskSize(h.Model, h.Endpoint) |
| 135 | +} |
| 136 | + |
| 137 | +// GetState returns the model state |
| 138 | +func (h *ConversionHelper) GetState(defaultState string) string { |
| 139 | + return h.DetermineModelState(h.Endpoint, defaultState) |
| 140 | +} |
| 141 | + |
| 142 | +// GetModelType returns the determined model type |
| 143 | +func (h *ConversionHelper) GetModelType(defaultType string) string { |
| 144 | + return h.DetermineModelType(h.Model, defaultType) |
| 145 | +} |
| 146 | + |
| 147 | +// GetMetadataString extracts a string from model metadata |
| 148 | +func (h *ConversionHelper) GetMetadataString(key string) string { |
| 149 | + return h.ExtractMetadataString(h.Model.Metadata, key) |
| 150 | +} |
| 151 | + |
| 152 | +// GetMetadataInt extracts an int from model metadata |
| 153 | +func (h *ConversionHelper) GetMetadataInt(key string) int { |
| 154 | + return h.ExtractMetadataInt(h.Model.Metadata, key) |
| 155 | +} |
| 156 | + |
| 157 | +// GetMetadataBool extracts a bool from model metadata |
| 158 | +func (h *ConversionHelper) GetMetadataBool(key string) bool { |
| 159 | + return h.ExtractMetadataBool(h.Model.Metadata, key) |
| 160 | +} |
0 commit comments