Skip to content

Commit 9666308

Browse files
authored
feat(config): allow custom ollama path (#171)
1 parent 8a84d6a commit 9666308

File tree

4 files changed

+24
-20
lines changed

4 files changed

+24
-20
lines changed

config/config.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"os"
66
"path/filepath"
7+
"runtime"
78
"time"
89

910
"github.com/fsnotify/fsnotify"
@@ -15,6 +16,7 @@ type Config struct {
1516
Columns []string `mapstructure:"columns"`
1617
OllamaAPIKey string `mapstructure:"ollama_api_key"`
1718
OllamaAPIURL string `mapstructure:"ollama_api_url"`
19+
OllamaModelsDir string `mapstructure:"ollama_models_dir"`
1820
LMStudioFilePaths string `mapstructure:"lm_studio_file_paths"`
1921
LogLevel string `mapstructure:"log_level"`
2022
LogFilePath string `mapstructure:"log_file_path"`
@@ -29,6 +31,7 @@ var defaultConfig = Config{
2931
Columns: []string{"Name", "Size", "Quant", "Family", "Modified", "ID"},
3032
OllamaAPIKey: "",
3133
OllamaAPIURL: getAPIUrl(),
34+
OllamaModelsDir: GetOllamaModelDir(),
3235
LMStudioFilePaths: "",
3336
LogLevel: "info",
3437
SortOrder: "modified",
@@ -37,6 +40,18 @@ var defaultConfig = Config{
3740
DockerContainer: "",
3841
}
3942

43+
// GetOllamaModelDir returns the default Ollama models directory for the current OS
44+
func GetOllamaModelDir() string {
45+
homeDir := utils.GetHomeDir()
46+
if runtime.GOOS == "darwin" {
47+
return filepath.Join(homeDir, ".ollama", "models")
48+
} else if runtime.GOOS == "linux" {
49+
return "/usr/share/ollama/models"
50+
}
51+
// Add Windows path if needed
52+
return filepath.Join(homeDir, ".ollama", "models")
53+
}
54+
4055
// getAPIUrl determines the API URL based on environment variables.
4156
func getAPIUrl() string {
4257
if apiUrl := os.Getenv("OLLAMA_API_URL"); apiUrl != "" {
@@ -108,6 +123,7 @@ func LoadConfig() (Config, error) {
108123
viper.SetDefault("columns", defaultConfig.Columns)
109124
viper.SetDefault("ollama_api_key", defaultConfig.OllamaAPIKey)
110125
viper.SetDefault("ollama_api_url", defaultConfig.OllamaAPIURL)
126+
viper.SetDefault("ollama_models_dir", defaultConfig.OllamaModelsDir)
111127
viper.SetDefault("lm_studio_file_paths", defaultConfig.LMStudioFilePaths)
112128
viper.SetDefault("log_level", defaultConfig.LogLevel)
113129
viper.SetDefault("log_file_path", defaultConfig.LogFilePath)
@@ -119,6 +135,7 @@ func LoadConfig() (Config, error) {
119135
config.Columns = viper.GetStringSlice("columns")
120136
config.OllamaAPIKey = viper.GetString("ollama_api_key")
121137
config.OllamaAPIURL = viper.GetString("ollama_api_url")
138+
config.OllamaModelsDir = viper.GetString("ollama_models_dir")
122139
config.LMStudioFilePaths = viper.GetString("lm_studio_file_paths")
123140
config.LogLevel = viper.GetString("log_level")
124141
config.LogFilePath = viper.GetString("log_file_path")

config/config_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ func TestLoadConfig(t *testing.T) {
6161
Columns: []string{"Name", "Size"},
6262
OllamaAPIKey: "testkey",
6363
OllamaAPIURL: "http://testurl",
64+
OllamaModelsDir: GetOllamaModelDir(),
6465
LMStudioFilePaths: "/test/path",
6566
LogLevel: "debug",
6667
LogFilePath: "/test/path/gollama.log",
@@ -75,6 +76,7 @@ func TestLoadConfig(t *testing.T) {
7576
Columns: []string{"Name", "Size"},
7677
OllamaAPIKey: "testkey",
7778
OllamaAPIURL: "http://testurl",
79+
OllamaModelsDir: GetOllamaModelDir(),
7880
LMStudioFilePaths: "/test/path",
7981
LogLevel: "debug",
8082
LogFilePath: "/test/path/gollama.log",
@@ -97,6 +99,7 @@ func TestLoadConfig(t *testing.T) {
9799
Columns: []string{"Name", "Size", "Quant", "Family", "Modified", "ID"},
98100
OllamaAPIKey: "",
99101
OllamaAPIURL: "http://127.0.0.1:11434", // Default URL when no env vars are set
102+
OllamaModelsDir: GetOllamaModelDir(),
100103
LMStudioFilePaths: "",
101104
LogLevel: "info",
102105
SortOrder: "modified",
@@ -119,6 +122,7 @@ func TestLoadConfig(t *testing.T) {
119122
OllamaAPIKey: "",
120123
OllamaAPIURL: "http://ollama.icu.lol",
121124
LMStudioFilePaths: "",
125+
OllamaModelsDir: GetOllamaModelDir(),
122126
LogLevel: "info",
123127
SortOrder: "modified",
124128
StripString: "",

lmstudio/link.go

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"os"
66
"os/exec"
77
"path/filepath"
8-
"runtime"
98
"strings"
109
"text/template"
1110

@@ -114,18 +113,6 @@ func ScanModels(dirPath string) ([]Model, error) {
114113
return models, nil
115114
}
116115

117-
// GetOllamaModelDir returns the default Ollama models directory for the current OS
118-
func GetOllamaModelDir() string {
119-
homeDir := utils.GetHomeDir()
120-
if runtime.GOOS == "darwin" {
121-
return filepath.Join(homeDir, ".ollama", "models")
122-
} else if runtime.GOOS == "linux" {
123-
return "/usr/share/ollama/models"
124-
}
125-
// Add Windows path if needed
126-
return filepath.Join(homeDir, ".ollama", "models")
127-
}
128-
129116
// modelExists checks if a model is already registered with Ollama
130117
func modelExists(modelName string) bool {
131118
cmd := exec.Command("ollama", "list")
@@ -181,14 +168,12 @@ func createModelfile(modelName string, modelPath string) error {
181168

182169
// LinkModelToOllama links an LM Studio model to Ollama
183170
// If dryRun is true, it will only print what would happen without making any changes
184-
func LinkModelToOllama(model Model, dryRun bool, ollamaHost string) error {
171+
func LinkModelToOllama(model Model, dryRun bool, ollamaHost string, ollamaDir string) error {
185172
// Check if we're connecting to a local Ollama instance
186173
if !utils.IsLocalhost(ollamaHost) {
187174
return fmt.Errorf("linking LM Studio models to Ollama is only supported when connecting to a local Ollama instance (got %s)", ollamaHost)
188175
}
189176

190-
ollamaDir := GetOllamaModelDir()
191-
192177
if dryRun {
193178
logging.InfoLogger.Printf("[DRY RUN] Would create Ollama models directory at: %s", ollamaDir)
194179
} else if err := os.MkdirAll(ollamaDir, 0755); err != nil {

main.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"net/http"
99
"net/url"
1010
"os"
11-
"path/filepath"
1211
"sort"
1312
"strings"
1413

@@ -25,7 +24,6 @@ import (
2524
"github.com/sammcj/gollama/config"
2625
"github.com/sammcj/gollama/lmstudio"
2726
"github.com/sammcj/gollama/logging"
28-
"github.com/sammcj/gollama/utils"
2927
"github.com/sammcj/gollama/vramestimator"
3028
)
3129

@@ -314,7 +312,7 @@ func main() {
314312
}
315313

316314
if *ollamaDirFlag == "" {
317-
app.ollamaModelsDir = filepath.Join(utils.GetHomeDir(), ".ollama", "models")
315+
app.ollamaModelsDir = cfg.OllamaModelsDir
318316
}
319317
if *lmStudioDirFlag == "" {
320318
app.lmStudioModelsDir = cfg.LMStudioFilePaths
@@ -394,7 +392,7 @@ func main() {
394392

395393
for _, model := range models {
396394
fmt.Printf("%sProcessing model %s... ", prefix, model.Name)
397-
if err := lmstudio.LinkModelToOllama(model, *dryRunFlag, cfg.OllamaAPIURL); err != nil {
395+
if err := lmstudio.LinkModelToOllama(model, *dryRunFlag, cfg.OllamaAPIURL, app.ollamaModelsDir); err != nil {
398396
logging.ErrorLogger.Printf("Error linking model %s: %v\n", model.Name, err)
399397
fmt.Printf("failed: %v\n", err)
400398
failCount++

0 commit comments

Comments
 (0)