112 lines
2.4 KiB
Go
112 lines
2.4 KiB
Go
package ollama
|
|
|
|
import (
|
|
"bufio"
|
|
"bytes"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"net/http"
|
|
)
|
|
|
|
type PullModelRequest struct {
|
|
Model string `json:"name"`
|
|
Insecure *bool `json:"insecure,omitempty"`
|
|
Stream *bool `json:"stream,omitempty"`
|
|
}
|
|
|
|
type PullModelResponse struct {
|
|
Status string `json:"status"`
|
|
}
|
|
|
|
type PullModelResponseStream struct {
|
|
Status string `json:"status"`
|
|
Digest string `json:"digest"`
|
|
Total int `json:"total"`
|
|
Completed int `json:"completed"`
|
|
}
|
|
|
|
func (o Ollama) PullModel(reqBody PullModelRequest) (PullModelResponse, error) {
|
|
reqBody.Stream = PtrOf(false)
|
|
reqBodyBytes, err := json.Marshal(reqBody)
|
|
if err != nil {
|
|
return PullModelResponse{}, err
|
|
}
|
|
|
|
req, err := http.NewRequest(http.MethodPost, fmt.Sprintf("%s/pull", o.baseUrl), bytes.NewReader(reqBodyBytes))
|
|
if err != nil {
|
|
return PullModelResponse{}, err
|
|
}
|
|
|
|
for key, val := range o.customHeaders {
|
|
req.Header.Set(key, val)
|
|
}
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
|
if err != nil {
|
|
return PullModelResponse{}, err
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
return PullModelResponse{}, errors.New("status code is not 200")
|
|
}
|
|
|
|
var respBody PullModelResponse
|
|
if err := json.NewDecoder(resp.Body).Decode(&respBody); err != nil {
|
|
return PullModelResponse{}, err
|
|
}
|
|
return respBody, nil
|
|
}
|
|
|
|
func (o Ollama) PullModelStream(reqBody PullModelRequest, onChunk func(chunk PullModelResponseStream)) error {
|
|
reqBody.Stream = PtrOf(true)
|
|
reqBodyBytes, err := json.Marshal(reqBody)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
req, err := http.NewRequest(http.MethodPost, fmt.Sprintf("%s/pull", o.baseUrl), bytes.NewReader(reqBodyBytes))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
for key, val := range o.customHeaders {
|
|
req.Header.Set(key, val)
|
|
}
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
return errors.New("status code is not 200")
|
|
}
|
|
|
|
scanner := bufio.NewScanner(resp.Body)
|
|
|
|
for scanner.Scan() {
|
|
line := bytes.TrimSpace(scanner.Bytes())
|
|
|
|
var chunk PullModelResponseStream
|
|
if err := json.Unmarshal(line, &chunk); err != nil {
|
|
return err
|
|
}
|
|
|
|
onChunk(chunk)
|
|
if chunk.Status == "success" {
|
|
break
|
|
}
|
|
}
|
|
|
|
if err := scanner.Err(); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|