2024-04-10 17:31:55 +00:00
|
|
|
// Package api implements the client-side API for code wishing to interact
|
|
|
|
// with the ollama service. The methods of the [Client] type correspond to
|
|
|
|
// the ollama REST API as described in https://github.com/ollama/ollama/blob/main/docs/api.md
|
|
|
|
//
|
|
|
|
// The ollama command-line client itself uses this package to interact with
|
|
|
|
// the backend service.
|
2023-07-03 19:22:44 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2023-07-04 04:47:00 +00:00
|
|
|
"bufio"
|
2023-07-03 19:22:44 +00:00
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
2023-07-07 21:04:43 +00:00
|
|
|
"fmt"
|
2023-07-18 16:09:45 +00:00
|
|
|
"io"
|
2023-10-09 19:18:26 +00:00
|
|
|
"net"
|
2023-07-03 19:22:44 +00:00
|
|
|
"net/http"
|
2023-07-06 22:02:10 +00:00
|
|
|
"net/url"
|
2023-08-16 15:03:48 +00:00
|
|
|
"os"
|
2023-08-22 01:24:42 +00:00
|
|
|
"runtime"
|
2023-08-17 22:20:38 +00:00
|
|
|
"strings"
|
2023-08-22 01:24:42 +00:00
|
|
|
|
2024-03-26 20:04:17 +00:00
|
|
|
"github.com/ollama/ollama/format"
|
|
|
|
"github.com/ollama/ollama/version"
|
2023-08-16 15:03:48 +00:00
|
|
|
)
|
|
|
|
|
2024-04-10 17:31:55 +00:00
|
|
|
// Client encapsulates client state for interacting with the ollama
|
|
|
|
// service. Use [ClientFromEnvironment] to create new Clients.
|
2023-07-18 16:09:45 +00:00
|
|
|
type Client struct {
|
2023-10-09 19:18:26 +00:00
|
|
|
base *url.URL
|
2024-02-20 23:34:47 +00:00
|
|
|
http *http.Client
|
2023-07-11 20:05:51 +00:00
|
|
|
}
|
|
|
|
|
2023-07-18 16:09:45 +00:00
|
|
|
func checkError(resp *http.Response, body []byte) error {
|
2023-08-27 04:55:21 +00:00
|
|
|
if resp.StatusCode < http.StatusBadRequest {
|
2023-07-18 16:09:45 +00:00
|
|
|
return nil
|
2023-07-11 20:05:51 +00:00
|
|
|
}
|
|
|
|
|
2023-07-18 16:09:45 +00:00
|
|
|
apiError := StatusError{StatusCode: resp.StatusCode}
|
2023-07-11 20:05:51 +00:00
|
|
|
|
2023-07-18 16:09:45 +00:00
|
|
|
err := json.Unmarshal(body, &apiError)
|
|
|
|
if err != nil {
|
|
|
|
// Use the full body as the message if we fail to decode a response.
|
2023-07-20 18:45:12 +00:00
|
|
|
apiError.ErrorMessage = string(body)
|
2023-07-18 16:09:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return apiError
|
2023-07-06 22:02:10 +00:00
|
|
|
}
|
|
|
|
|
2024-04-10 17:31:55 +00:00
|
|
|
// ClientFromEnvironment creates a new [Client] using configuration from the
|
|
|
|
// environment variable OLLAMA_HOST, which points to the network host and
|
|
|
|
// port on which the ollama service is listenting. The format of this variable
|
|
|
|
// is:
|
|
|
|
//
|
|
|
|
// <scheme>://<host>:<port>
|
|
|
|
//
|
|
|
|
// If the variable is not specified, a default ollama host and port will be
|
|
|
|
// used.
|
2023-10-09 19:18:26 +00:00
|
|
|
func ClientFromEnvironment() (*Client, error) {
|
2023-10-26 17:47:41 +00:00
|
|
|
defaultPort := "11434"
|
|
|
|
|
2023-10-09 19:18:26 +00:00
|
|
|
scheme, hostport, ok := strings.Cut(os.Getenv("OLLAMA_HOST"), "://")
|
2023-10-26 17:47:41 +00:00
|
|
|
switch {
|
|
|
|
case !ok:
|
2023-10-09 19:18:26 +00:00
|
|
|
scheme, hostport = "http", os.Getenv("OLLAMA_HOST")
|
2023-10-26 17:47:41 +00:00
|
|
|
case scheme == "http":
|
|
|
|
defaultPort = "80"
|
|
|
|
case scheme == "https":
|
|
|
|
defaultPort = "443"
|
2023-10-09 19:18:26 +00:00
|
|
|
}
|
|
|
|
|
2023-10-26 17:47:41 +00:00
|
|
|
// trim trailing slashes
|
|
|
|
hostport = strings.TrimRight(hostport, "/")
|
|
|
|
|
2023-10-09 19:18:26 +00:00
|
|
|
host, port, err := net.SplitHostPort(hostport)
|
|
|
|
if err != nil {
|
2023-10-26 17:47:41 +00:00
|
|
|
host, port = "127.0.0.1", defaultPort
|
2023-10-20 18:32:28 +00:00
|
|
|
if ip := net.ParseIP(strings.Trim(hostport, "[]")); ip != nil {
|
2023-10-09 19:18:26 +00:00
|
|
|
host = ip.String()
|
2023-10-20 18:32:28 +00:00
|
|
|
} else if hostport != "" {
|
|
|
|
host = hostport
|
2023-10-09 19:18:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-20 23:34:47 +00:00
|
|
|
return &Client{
|
2023-10-09 19:18:26 +00:00
|
|
|
base: &url.URL{
|
|
|
|
Scheme: scheme,
|
|
|
|
Host: net.JoinHostPort(host, port),
|
|
|
|
},
|
2024-02-20 23:34:47 +00:00
|
|
|
http: http.DefaultClient,
|
|
|
|
}, nil
|
2023-07-18 16:09:45 +00:00
|
|
|
}
|
|
|
|
|
2024-03-30 16:50:05 +00:00
|
|
|
func NewClient(base *url.URL, http *http.Client) *Client {
|
|
|
|
return &Client{
|
|
|
|
base: base,
|
|
|
|
http: http,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-18 16:09:45 +00:00
|
|
|
func (c *Client) do(ctx context.Context, method, path string, reqData, respData any) error {
|
|
|
|
var reqBody io.Reader
|
|
|
|
var data []byte
|
|
|
|
var err error
|
2023-11-14 22:07:40 +00:00
|
|
|
|
|
|
|
switch reqData := reqData.(type) {
|
|
|
|
case io.Reader:
|
|
|
|
// reqData is already an io.Reader
|
|
|
|
reqBody = reqData
|
|
|
|
case nil:
|
|
|
|
// noop
|
|
|
|
default:
|
2023-07-18 16:09:45 +00:00
|
|
|
data, err = json.Marshal(reqData)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-11-14 22:07:40 +00:00
|
|
|
|
2023-07-18 16:09:45 +00:00
|
|
|
reqBody = bytes.NewReader(data)
|
|
|
|
}
|
|
|
|
|
2023-10-09 19:18:26 +00:00
|
|
|
requestURL := c.base.JoinPath(path)
|
2023-08-22 01:24:42 +00:00
|
|
|
request, err := http.NewRequestWithContext(ctx, method, requestURL.String(), reqBody)
|
2023-07-18 16:09:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-08-22 01:24:42 +00:00
|
|
|
request.Header.Set("Content-Type", "application/json")
|
|
|
|
request.Header.Set("Accept", "application/json")
|
|
|
|
request.Header.Set("User-Agent", fmt.Sprintf("ollama/%s (%s %s) Go/%s", version.Version, runtime.GOARCH, runtime.GOOS, runtime.Version()))
|
2023-07-18 16:09:45 +00:00
|
|
|
|
2023-10-09 19:18:26 +00:00
|
|
|
respObj, err := c.http.Do(request)
|
2023-07-18 16:09:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer respObj.Body.Close()
|
|
|
|
|
|
|
|
respBody, err := io.ReadAll(respObj.Body)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := checkError(respObj, respBody); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(respBody) > 0 && respData != nil {
|
|
|
|
if err := json.Unmarshal(respBody, respData); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2023-07-03 19:22:44 +00:00
|
|
|
}
|
|
|
|
|
2023-10-12 16:34:16 +00:00
|
|
|
const maxBufferSize = 512 * format.KiloByte
|
2023-10-04 18:09:00 +00:00
|
|
|
|
2023-07-11 20:05:51 +00:00
|
|
|
func (c *Client) stream(ctx context.Context, method, path string, data any, fn func([]byte) error) error {
|
2023-07-11 04:34:15 +00:00
|
|
|
var buf *bytes.Buffer
|
|
|
|
if data != nil {
|
|
|
|
bts, err := json.Marshal(data)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-07-06 23:53:14 +00:00
|
|
|
|
2023-07-11 04:34:15 +00:00
|
|
|
buf = bytes.NewBuffer(bts)
|
2023-07-03 19:22:44 +00:00
|
|
|
}
|
|
|
|
|
2023-10-09 19:18:26 +00:00
|
|
|
requestURL := c.base.JoinPath(path)
|
2023-08-22 01:24:42 +00:00
|
|
|
request, err := http.NewRequestWithContext(ctx, method, requestURL.String(), buf)
|
2023-07-03 19:22:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-07-06 22:02:10 +00:00
|
|
|
request.Header.Set("Content-Type", "application/json")
|
2023-10-11 16:54:27 +00:00
|
|
|
request.Header.Set("Accept", "application/x-ndjson")
|
2023-08-22 01:24:42 +00:00
|
|
|
request.Header.Set("User-Agent", fmt.Sprintf("ollama/%s (%s %s) Go/%s", version.Version, runtime.GOARCH, runtime.GOOS, runtime.Version()))
|
2023-07-04 04:47:00 +00:00
|
|
|
|
2023-10-09 19:18:26 +00:00
|
|
|
response, err := c.http.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-11 04:34:15 +00:00
|
|
|
scanner := bufio.NewScanner(response.Body)
|
2023-10-04 18:09:00 +00:00
|
|
|
// increase the buffer size to avoid running out of space
|
|
|
|
scanBuf := make([]byte, 0, maxBufferSize)
|
|
|
|
scanner.Buffer(scanBuf, maxBufferSize)
|
2023-07-11 04:34:15 +00:00
|
|
|
for scanner.Scan() {
|
|
|
|
var errorResponse struct {
|
2023-07-11 20:05:51 +00:00
|
|
|
Error string `json:"error,omitempty"`
|
2023-07-11 04:34:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bts := scanner.Bytes()
|
|
|
|
if err := json.Unmarshal(bts, &errorResponse); err != nil {
|
|
|
|
return fmt.Errorf("unmarshal: %w", err)
|
|
|
|
}
|
|
|
|
|
2023-07-20 19:12:08 +00:00
|
|
|
if errorResponse.Error != "" {
|
2023-07-24 21:48:17 +00:00
|
|
|
return fmt.Errorf(errorResponse.Error)
|
2023-07-20 19:12:08 +00:00
|
|
|
}
|
|
|
|
|
2023-08-27 04:55:21 +00:00
|
|
|
if response.StatusCode >= http.StatusBadRequest {
|
2023-07-11 20:05:51 +00:00
|
|
|
return StatusError{
|
2023-07-20 18:45:12 +00:00
|
|
|
StatusCode: response.StatusCode,
|
|
|
|
Status: response.Status,
|
|
|
|
ErrorMessage: errorResponse.Error,
|
2023-07-11 20:05:51 +00:00
|
|
|
}
|
2023-07-11 04:34:15 +00:00
|
|
|
}
|
|
|
|
|
2023-07-11 20:05:51 +00:00
|
|
|
if err := fn(bts); err != nil {
|
2023-07-11 04:34:15 +00:00
|
|
|
return err
|
2023-07-03 19:22:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-06 21:05:55 +00:00
|
|
|
return nil
|
|
|
|
}
|
2023-07-03 19:22:44 +00:00
|
|
|
|
2024-04-10 17:31:55 +00:00
|
|
|
// GenerateResponseFunc is a function that [Client.Generate] invokes every time
|
|
|
|
// a response is received from the service. If this function returns an error,
|
|
|
|
// [Client.Generate] will stop generating and return this error.
|
2023-07-06 21:05:55 +00:00
|
|
|
type GenerateResponseFunc func(GenerateResponse) error
|
2023-07-03 19:22:44 +00:00
|
|
|
|
2024-04-10 17:31:55 +00:00
|
|
|
// Generate generates a response for a given prompt. The req parameter should
|
|
|
|
// be populated with prompt details. fn is called for each response (there may
|
|
|
|
// be multiple responses, e.g. in case streaming is enabled).
|
2023-07-06 21:05:55 +00:00
|
|
|
func (c *Client) Generate(ctx context.Context, req *GenerateRequest, fn GenerateResponseFunc) error {
|
2023-07-11 04:34:15 +00:00
|
|
|
return c.stream(ctx, http.MethodPost, "/api/generate", req, 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
|
|
|
|
2024-04-10 17:31:55 +00:00
|
|
|
// ChatResponseFunc is a function that [Client.Chat] invokes every time
|
|
|
|
// a response is received from the service. If this function returns an error,
|
|
|
|
// [Client.Chat] will stop generating and return this error.
|
2023-12-05 19:57:33 +00:00
|
|
|
type ChatResponseFunc func(ChatResponse) error
|
|
|
|
|
2024-04-10 17:31:55 +00:00
|
|
|
// Chat generates the next message in a chat. [ChatRequest] may contain a
|
|
|
|
// sequence of messages which can be used to maintain chat history with a model.
|
|
|
|
// fn is called for each response (there may be multiple responses, e.g. if case
|
|
|
|
// streaming is enabled).
|
2023-12-05 19:57:33 +00:00
|
|
|
func (c *Client) Chat(ctx context.Context, req *ChatRequest, fn ChatResponseFunc) error {
|
|
|
|
return c.stream(ctx, http.MethodPost, "/api/chat", req, func(bts []byte) error {
|
|
|
|
var resp ChatResponse
|
|
|
|
if err := json.Unmarshal(bts, &resp); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return fn(resp)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-04-10 17:31:55 +00:00
|
|
|
// PullProgressFunc is a function that [Client.Pull] invokes every time there
|
|
|
|
// is progress with a "pull" request sent to the service. If this function
|
|
|
|
// returns an error, [Client.Pull] will stop the process and return this error.
|
2023-07-19 01:51:30 +00:00
|
|
|
type PullProgressFunc func(ProgressResponse) error
|
2023-07-06 21:05:55 +00:00
|
|
|
|
2024-04-10 17:31:55 +00:00
|
|
|
// Pull downloads a model from the ollama library. fn is called each time
|
|
|
|
// progress is made on the request and can be used to display a progress bar,
|
|
|
|
// etc.
|
2023-07-06 21:05:55 +00:00
|
|
|
func (c *Client) Pull(ctx context.Context, req *PullRequest, fn PullProgressFunc) error {
|
2023-07-11 04:34:15 +00:00
|
|
|
return c.stream(ctx, http.MethodPost, "/api/pull", req, func(bts []byte) error {
|
2023-07-19 01:51:30 +00:00
|
|
|
var resp ProgressResponse
|
2023-07-11 04:34:15 +00:00
|
|
|
if err := json.Unmarshal(bts, &resp); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return fn(resp)
|
|
|
|
})
|
2023-07-06 16:24:49 +00:00
|
|
|
}
|
2023-07-17 00:02:22 +00:00
|
|
|
|
2023-07-19 01:51:30 +00:00
|
|
|
type PushProgressFunc func(ProgressResponse) error
|
2023-07-17 00:02:22 +00:00
|
|
|
|
|
|
|
func (c *Client) Push(ctx context.Context, req *PushRequest, fn PushProgressFunc) error {
|
|
|
|
return c.stream(ctx, http.MethodPost, "/api/push", req, func(bts []byte) error {
|
2023-07-19 01:51:30 +00:00
|
|
|
var resp ProgressResponse
|
2023-07-17 00:02:22 +00:00
|
|
|
if err := json.Unmarshal(bts, &resp); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return fn(resp)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-07-25 18:25:13 +00:00
|
|
|
type CreateProgressFunc func(ProgressResponse) error
|
2023-07-17 00:02:22 +00:00
|
|
|
|
|
|
|
func (c *Client) Create(ctx context.Context, req *CreateRequest, fn CreateProgressFunc) error {
|
|
|
|
return c.stream(ctx, http.MethodPost, "/api/create", req, func(bts []byte) error {
|
2023-07-25 18:25:13 +00:00
|
|
|
var resp ProgressResponse
|
2023-07-17 00:02:22 +00:00
|
|
|
if err := json.Unmarshal(bts, &resp); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return fn(resp)
|
|
|
|
})
|
|
|
|
}
|
2023-07-18 16:09:45 +00:00
|
|
|
|
|
|
|
func (c *Client) List(ctx context.Context) (*ListResponse, error) {
|
|
|
|
var lr ListResponse
|
|
|
|
if err := c.do(ctx, http.MethodGet, "/api/tags", nil, &lr); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &lr, nil
|
|
|
|
}
|
2023-07-20 23:09:23 +00:00
|
|
|
|
2023-07-24 15:27:28 +00:00
|
|
|
func (c *Client) Copy(ctx context.Context, req *CopyRequest) error {
|
|
|
|
if err := c.do(ctx, http.MethodPost, "/api/copy", req, nil); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-07-22 06:02:12 +00:00
|
|
|
func (c *Client) Delete(ctx context.Context, req *DeleteRequest) error {
|
|
|
|
if err := c.do(ctx, http.MethodDelete, "/api/delete", req, nil); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2023-07-20 23:09:23 +00:00
|
|
|
}
|
2023-07-31 20:25:57 +00:00
|
|
|
|
2023-09-06 18:04:17 +00:00
|
|
|
func (c *Client) Show(ctx context.Context, req *ShowRequest) (*ShowResponse, error) {
|
|
|
|
var resp ShowResponse
|
|
|
|
if err := c.do(ctx, http.MethodPost, "/api/show", req, &resp); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &resp, nil
|
|
|
|
}
|
|
|
|
|
2023-07-31 20:25:57 +00:00
|
|
|
func (c *Client) Heartbeat(ctx context.Context) error {
|
2023-08-01 18:50:38 +00:00
|
|
|
if err := c.do(ctx, http.MethodHead, "/", nil, nil); err != nil {
|
2023-07-31 20:25:57 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2024-01-04 20:00:52 +00:00
|
|
|
func (c *Client) Embeddings(ctx context.Context, req *EmbeddingRequest) (*EmbeddingResponse, error) {
|
|
|
|
var resp EmbeddingResponse
|
|
|
|
if err := c.do(ctx, http.MethodPost, "/api/embeddings", req, &resp); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &resp, nil
|
|
|
|
}
|
2023-11-14 22:07:40 +00:00
|
|
|
|
2023-11-15 18:59:38 +00:00
|
|
|
func (c *Client) CreateBlob(ctx context.Context, digest string, r io.Reader) error {
|
2024-04-05 16:30:09 +00:00
|
|
|
return c.do(ctx, http.MethodPost, fmt.Sprintf("/api/blobs/%s", digest), r, nil)
|
2023-11-14 22:07:40 +00:00
|
|
|
}
|
2023-11-22 17:41:44 +00:00
|
|
|
|
|
|
|
func (c *Client) Version(ctx context.Context) (string, error) {
|
|
|
|
var version struct {
|
|
|
|
Version string `json:"version"`
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := c.do(ctx, http.MethodGet, "/api/version", nil, &version); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return version.Version, nil
|
|
|
|
}
|