ollama/api/client.go

118 lines
2.3 KiB
Go
Raw Normal View History

package api
import (
2023-07-04 04:47:00 +00:00
"bufio"
"bytes"
"context"
"encoding/json"
"io"
"net/http"
2023-07-06 22:02:10 +00:00
"net/url"
)
type Client struct {
2023-07-06 22:02:10 +00: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-06 23:53:14 +00: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 23:53:14 +00:00
request, err := http.NewRequestWithContext(ctx, method, c.base.JoinPath(path).String(), opts.requestBody)
if err != nil {
return err
}
2023-07-06 22:02:10 +00:00
request.Header.Set("Content-Type", "application/json")
request.Header.Set("Accept", "application/json")
2023-07-04 04:47:00 +00:00
2023-07-06 22:02:10 +00:00
response, err := http.DefaultClient.Do(request)
2023-07-04 04:47:00 +00:00
if err != nil {
return err
}
2023-07-06 22:02:10 +00:00
defer response.Body.Close()
2023-07-04 04:47:00 +00:00
2023-07-06 23:53:14 +00:00
if opts.responseFunc != nil {
scanner := bufio.NewScanner(response.Body)
for scanner.Scan() {
if err := opts.responseFunc(scanner.Bytes()); err != nil {
return err
}
}
}
2023-07-06 21:05:55 +00:00
return nil
}
2023-07-06 21:05:55 +00:00
type GenerateResponseFunc func(GenerateResponse) error
2023-07-06 21:05:55 +00:00
func (c *Client) Generate(ctx context.Context, req *GenerateRequest, fn GenerateResponseFunc) error {
2023-07-06 23:53:14 +00: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 04:47:00 +00:00
}
2023-07-06 16:24:49 +00:00
2023-07-06 21:05:55 +00:00
type PullProgressFunc func(PullProgress) error
func (c *Client) Pull(ctx context.Context, req *PullRequest, fn PullProgressFunc) error {
2023-07-06 23:53:14 +00: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 23:53:14 +00:00
return fn(resp)
}),
)
2023-07-06 16:24:49 +00:00
}