ollama/api/client.go

144 lines
2.9 KiB
Go
Raw Normal View History

package api
import (
2023-07-04 00:47:00 -04:00
"bufio"
"bytes"
"context"
"encoding/json"
2023-07-07 17:04:43 -04:00
"fmt"
"io"
"net/http"
2023-07-06 18:02:10 -04:00
"net/url"
)
type Client struct {
2023-07-06 18:02:10 -04:00
base url.URL
}
func NewClient(hosts ...string) *Client {
host := "127.0.0.1:11434"
if len(hosts) > 0 {
host = hosts[0]
}
return &Client{
base: url.URL{Scheme: "http", Host: host},
}
}
2023-07-07 17:04:43 -04:00
func StatusError(status int, message ...string) error {
if status < 400 {
return nil
}
if len(message) > 0 && len(message[0]) > 0 {
return fmt.Errorf("%d %s: %s", status, http.StatusText(status), message[0])
}
return fmt.Errorf("%d %s", status, http.StatusText(status))
}
2023-07-06 19:53:14 -04:00
type options struct {
requestBody io.Reader
responseFunc func(bts []byte) error
}
func OptionRequestBody(data any) func(*options) {
bts, err := json.Marshal(data)
if err != nil {
panic(err)
}
return func(opts *options) {
opts.requestBody = bytes.NewReader(bts)
}
}
func OptionResponseFunc(fn func([]byte) error) func(*options) {
return func(opts *options) {
opts.responseFunc = fn
}
}
func (c *Client) stream(ctx context.Context, method, path string, fns ...func(*options)) error {
var opts options
for _, fn := range fns {
fn(&opts)
}
2023-07-06 19:53:14 -04:00
request, err := http.NewRequestWithContext(ctx, method, c.base.JoinPath(path).String(), opts.requestBody)
if err != nil {
return err
}
2023-07-06 18:02:10 -04:00
request.Header.Set("Content-Type", "application/json")
request.Header.Set("Accept", "application/json")
2023-07-04 00:47:00 -04:00
2023-07-06 18:02:10 -04:00
response, err := http.DefaultClient.Do(request)
2023-07-04 00:47:00 -04:00
if err != nil {
return err
}
2023-07-06 18:02:10 -04:00
defer response.Body.Close()
2023-07-04 00:47:00 -04:00
2023-07-06 19:53:14 -04:00
if opts.responseFunc != nil {
scanner := bufio.NewScanner(response.Body)
for scanner.Scan() {
2023-07-07 17:04:43 -04:00
var errorResponse struct {
Error string `json:"error"`
}
bts := scanner.Bytes()
if err := json.Unmarshal(bts, &errorResponse); err != nil {
return err
}
if err := StatusError(response.StatusCode, errorResponse.Error); err != nil {
return err
}
if err := opts.responseFunc(bts); err != nil {
2023-07-06 19:53:14 -04:00
return err
}
}
}
2023-07-06 17:05:55 -04:00
return nil
}
2023-07-06 17:05:55 -04:00
type GenerateResponseFunc func(GenerateResponse) error
2023-07-06 17:05:55 -04:00
func (c *Client) Generate(ctx context.Context, req *GenerateRequest, fn GenerateResponseFunc) error {
2023-07-06 19:53:14 -04:00
return c.stream(ctx, http.MethodPost, "/api/generate",
OptionRequestBody(req),
OptionResponseFunc(func(bts []byte) error {
var resp GenerateResponse
if err := json.Unmarshal(bts, &resp); err != nil {
return err
}
return fn(resp)
}),
)
2023-07-04 00:47:00 -04:00
}
2023-07-06 12:24:49 -04:00
2023-07-06 17:05:55 -04:00
type PullProgressFunc func(PullProgress) error
func (c *Client) Pull(ctx context.Context, req *PullRequest, fn PullProgressFunc) error {
2023-07-06 19:53:14 -04:00
return c.stream(ctx, http.MethodPost, "/api/pull",
OptionRequestBody(req),
OptionResponseFunc(func(bts []byte) error {
var resp PullProgress
if err := json.Unmarshal(bts, &resp); err != nil {
return err
}
if resp.Error.Message != "" {
// couldn't pull the model from the directory, proceed anyway
return nil
}
2023-07-06 19:53:14 -04:00
return fn(resp)
}),
)
2023-07-06 12:24:49 -04:00
}