Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 26 additions & 4 deletions pkg/inference/models/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,26 @@ func ToOpenAI(m types.Model) (*OpenAIModel, error) {
id = tags[0]
}

config, err := m.Config()
if err != nil {
return nil, fmt.Errorf("get config: %w", err)
}

var contextWindow int32
if cw := config.GetContextSize(); cw != nil {
contextWindow = *cw
}

return &OpenAIModel{
ID: id,
Object: "model",
Created: created,
OwnedBy: "docker",
ID: id,
Object: "model",
Created: created,
OwnedBy: "docker",
ContextWindow: contextWindow,
Architecture: config.GetArchitecture(),
Parameters: config.GetParameters(),
Quantization: config.GetQuantization(),
Size: config.GetSize(),
}, nil
Comment on lines +85 to 105
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

The m.Config() method can return a nil config with a nil error. If this happens, the subsequent calls to methods on config (e.g., config.GetContextSize() on line 91) will cause a nil pointer dereference, leading to a panic. You should add a check to handle the case where config is nil.

 	config, err := m.Config()
 	if err != nil {
 		return nil, fmt.Errorf("get config: %w", err)
 	}

 	if config == nil {
 		// If there's no config, return the model with only basic info.
 		return &OpenAIModel{
 			ID:      id,
 			Object:  "model",
 			Created: created,
 			OwnedBy: "docker",
 		}, nil
 	}

 	var contextWindow int32
 	if cw := config.GetContextSize(); cw != nil {
 		contextWindow = *cw
 	}

 	return &OpenAIModel{
 		ID:            id,
 		Object:        "model",
 		Created:       created,
 		OwnedBy:       "docker",
 		ContextWindow: contextWindow,
 		Architecture:  config.GetArchitecture(),
 		Parameters:    config.GetParameters(),
 		Quantization:  config.GetQuantization(),
 		Size:          config.GetSize(),
 	}, nil

}

Expand All @@ -100,6 +115,13 @@ type OpenAIModel struct {
Created int64 `json:"created"`
// OwnedBy is the model owner. At the moment, it is always "docker".
OwnedBy string `json:"owned_by"`

// Additional metadata
ContextWindow int32 `json:"context_window,omitempty"`
Architecture string `json:"architecture,omitempty"`
Parameters string `json:"parameters,omitempty"`
Quantization string `json:"quantization,omitempty"`
Size string `json:"size,omitempty"`
}

// OpenAIModelList represents a list of models using OpenAI conventions.
Expand Down