path.go 1.19 KB
Newer Older
1
package ml
Michael Yang's avatar
Michael Yang committed
2
3
4
5
6
7
8
9
10
11
12
13
14

import (
	"os"
	"path/filepath"
	"runtime"
)

// LibPath is a path to lookup dynamic libraries
// in development it's usually 'build/lib/ollama'
// in distribution builds it's 'lib/ollama' on Windows
// '../lib/ollama' on Linux and the executable's directory on macOS
// note: distribution builds, additional GPU-specific libraries are
// found in subdirectories of the returned path, such as
Daniel Hiltgen's avatar
Daniel Hiltgen committed
15
// 'cuda_v12', 'rocm', etc.
Michael Yang's avatar
Michael Yang committed
16
17
18
19
20
21
var LibOllamaPath string = func() string {
	exe, err := os.Executable()
	if err != nil {
		return ""
	}

22
23
24
25
	if eval, err := filepath.EvalSymlinks(exe); err == nil {
		exe = eval
	}

26
	var libPath string
Michael Yang's avatar
Michael Yang committed
27
28
29
30
31
	switch runtime.GOOS {
	case "windows":
		libPath = filepath.Join(filepath.Dir(exe), "lib", "ollama")
	case "linux":
		libPath = filepath.Join(filepath.Dir(exe), "..", "lib", "ollama")
32
33
	case "darwin":
		libPath = filepath.Dir(exe)
Michael Yang's avatar
Michael Yang committed
34
35
36
37
38
39
40
	}

	cwd, err := os.Getwd()
	if err != nil {
		return ""
	}

41
42
43
44
	paths := []string{
		libPath,

		// build paths for development
Michael Yang's avatar
Michael Yang committed
45
46
47
48
		filepath.Join(filepath.Dir(exe), "build", "lib", "ollama"),
		filepath.Join(cwd, "build", "lib", "ollama"),
	}

49
	for _, p := range paths {
Michael Yang's avatar
Michael Yang committed
50
51
52
53
54
		if _, err := os.Stat(p); err == nil {
			return p
		}
	}

55
	return filepath.Dir(exe)
Michael Yang's avatar
Michael Yang committed
56
}()