models.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package server
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io"
  6. "net/http"
  7. "os"
  8. "path"
  9. "strconv"
  10. )
  11. const directoryURL = "https://ollama.ai/api/models"
  12. type Model struct {
  13. Name string `json:"name"`
  14. DisplayName string `json:"display_name"`
  15. Parameters string `json:"parameters"`
  16. URL string `json:"url"`
  17. ShortDescription string `json:"short_description"`
  18. Description string `json:"description"`
  19. PublishedBy string `json:"published_by"`
  20. OriginalAuthor string `json:"original_author"`
  21. OriginalURL string `json:"original_url"`
  22. License string `json:"license"`
  23. }
  24. func (m *Model) FullName() string {
  25. home, err := os.UserHomeDir()
  26. if err != nil {
  27. panic(err)
  28. }
  29. return path.Join(home, ".ollama", "models", m.Name+".bin")
  30. }
  31. func getRemote(model string) (*Model, error) {
  32. // resolve the model download from our directory
  33. resp, err := http.Get(directoryURL)
  34. if err != nil {
  35. return nil, fmt.Errorf("failed to get directory: %w", err)
  36. }
  37. defer resp.Body.Close()
  38. body, err := io.ReadAll(resp.Body)
  39. if err != nil {
  40. return nil, fmt.Errorf("failed to read directory: %w", err)
  41. }
  42. var models []Model
  43. err = json.Unmarshal(body, &models)
  44. if err != nil {
  45. return nil, fmt.Errorf("failed to parse directory: %w", err)
  46. }
  47. for _, m := range models {
  48. if m.Name == model {
  49. return &m, nil
  50. }
  51. }
  52. return nil, fmt.Errorf("model not found in directory: %s", model)
  53. }
  54. func saveModel(model *Model, fn func(total, completed int64)) error {
  55. // this models cache directory is created by the server on startup
  56. client := &http.Client{}
  57. req, err := http.NewRequest("GET", model.URL, nil)
  58. if err != nil {
  59. return fmt.Errorf("failed to download model: %w", err)
  60. }
  61. // check for resume
  62. alreadyDownloaded := int64(0)
  63. fileInfo, err := os.Stat(model.FullName())
  64. if err != nil {
  65. if !os.IsNotExist(err) {
  66. return fmt.Errorf("failed to check resume model file: %w", err)
  67. }
  68. // file doesn't exist, create it now
  69. } else {
  70. alreadyDownloaded = fileInfo.Size()
  71. req.Header.Add("Range", fmt.Sprintf("bytes=%d-", alreadyDownloaded))
  72. }
  73. resp, err := client.Do(req)
  74. if err != nil {
  75. return fmt.Errorf("failed to download model: %w", err)
  76. }
  77. defer resp.Body.Close()
  78. if resp.StatusCode == http.StatusRequestedRangeNotSatisfiable {
  79. // already downloaded
  80. fn(alreadyDownloaded, alreadyDownloaded)
  81. return nil
  82. }
  83. if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusPartialContent {
  84. return fmt.Errorf("failed to download model: %s", resp.Status)
  85. }
  86. out, err := os.OpenFile(model.FullName(), os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0o644)
  87. if err != nil {
  88. panic(err)
  89. }
  90. defer out.Close()
  91. totalSize, _ := strconv.ParseInt(resp.Header.Get("Content-Length"), 10, 64)
  92. buf := make([]byte, 1024)
  93. totalBytes := alreadyDownloaded
  94. totalSize += alreadyDownloaded
  95. for {
  96. n, err := resp.Body.Read(buf)
  97. if err != nil && err != io.EOF {
  98. return err
  99. }
  100. if n == 0 {
  101. break
  102. }
  103. if _, err := out.Write(buf[:n]); err != nil {
  104. return err
  105. }
  106. totalBytes += int64(n)
  107. fn(totalSize, totalBytes)
  108. }
  109. fn(totalSize, totalSize)
  110. return nil
  111. }