-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
398cc57
commit ddb0efc
Showing
3 changed files
with
95 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package ollama | ||
|
||
import ( | ||
"bufio" | ||
"encoding/json" | ||
"errors" | ||
"io" | ||
"log/slog" | ||
"net/http" | ||
"net/url" | ||
) | ||
|
||
func (c *Config) ensureModel() error { | ||
body := ModelRequest{ | ||
Name: c.model, | ||
} | ||
|
||
u, _ := url.JoinPath(c.url, "/api/show") | ||
resp, err := c.client.Post(u, "application/json", jsonReader(body)) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
if resp.StatusCode == http.StatusOK { | ||
return nil | ||
} | ||
|
||
return c.pullModel() | ||
} | ||
|
||
func (c *Config) pullModel() error { | ||
body := PullRequest{ | ||
Name: c.model, | ||
Stream: true, | ||
} | ||
|
||
u, _ := url.JoinPath(c.url, "/api/pull") | ||
resp, err := c.client.Post(u, "application/json", jsonReader(body)) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
defer resp.Body.Close() | ||
|
||
if resp.StatusCode != http.StatusOK { | ||
return errors.New("unable to pull model") | ||
} | ||
|
||
reader := bufio.NewReader(resp.Body) | ||
|
||
for i := 0; ; i++ { | ||
data, err := reader.ReadBytes('\n') | ||
|
||
if errors.Is(err, io.EOF) { | ||
break | ||
} | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
if len(data) == 0 { | ||
continue | ||
} | ||
|
||
var pull PullResponse | ||
|
||
if err := json.Unmarshal([]byte(data), &pull); err != nil { | ||
return err | ||
} | ||
|
||
slog.Info("download model", "model", c.model, "status", pull.Status) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
type ModelRequest struct { | ||
Name string `json:"name"` | ||
} | ||
|
||
type PullRequest struct { | ||
Name string `json:"name"` | ||
Stream bool `json:"stream"` | ||
} | ||
|
||
type PullResponse struct { | ||
Status string `json:"status"` | ||
} |