| 
									
										
										
										
											2025-01-30 07:03:38 +08:00
										 |  |  | package discover | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 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
 | 
					
						
							| 
									
										
										
										
											2025-06-24 05:07:00 +08:00
										 |  |  | // 'cuda_v12', 'rocm', etc.
 | 
					
						
							| 
									
										
										
										
											2025-01-30 07:03:38 +08:00
										 |  |  | var LibOllamaPath string = func() string { | 
					
						
							|  |  |  | 	exe, err := os.Executable() | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return "" | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-14 14:37:59 +08:00
										 |  |  | 	if eval, err := filepath.EvalSymlinks(exe); err == nil { | 
					
						
							|  |  |  | 		exe = eval | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-31 04:21:38 +08:00
										 |  |  | 	var libPath string | 
					
						
							| 
									
										
										
										
											2025-01-30 07:03:38 +08:00
										 |  |  | 	switch runtime.GOOS { | 
					
						
							|  |  |  | 	case "windows": | 
					
						
							|  |  |  | 		libPath = filepath.Join(filepath.Dir(exe), "lib", "ollama") | 
					
						
							|  |  |  | 	case "linux": | 
					
						
							|  |  |  | 		libPath = filepath.Join(filepath.Dir(exe), "..", "lib", "ollama") | 
					
						
							| 
									
										
										
										
											2025-01-31 04:21:38 +08:00
										 |  |  | 	case "darwin": | 
					
						
							|  |  |  | 		libPath = filepath.Dir(exe) | 
					
						
							| 
									
										
										
										
											2025-01-30 07:03:38 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	cwd, err := os.Getwd() | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return "" | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-31 04:21:38 +08:00
										 |  |  | 	paths := []string{ | 
					
						
							|  |  |  | 		libPath, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// build paths for development
 | 
					
						
							| 
									
										
										
										
											2025-01-30 07:03:38 +08:00
										 |  |  | 		filepath.Join(filepath.Dir(exe), "build", "lib", "ollama"), | 
					
						
							|  |  |  | 		filepath.Join(cwd, "build", "lib", "ollama"), | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-31 04:21:38 +08:00
										 |  |  | 	for _, p := range paths { | 
					
						
							| 
									
										
										
										
											2025-01-30 07:03:38 +08:00
										 |  |  | 		if _, err := os.Stat(p); err == nil { | 
					
						
							|  |  |  | 			return p | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-31 04:21:38 +08:00
										 |  |  | 	return filepath.Dir(exe) | 
					
						
							| 
									
										
										
										
											2025-01-30 07:03:38 +08:00
										 |  |  | }() |