| 
									
										
										
										
											2024-04-11 01:31:55 +08: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
 | 
					
						
							| 
									
										
										
										
											2024-05-08 07:27:46 +08:00
										 |  |  | // the ollama REST API as described in [the API documentation].
 | 
					
						
							| 
									
										
										
										
											2024-04-11 01:31:55 +08:00
										 |  |  | // The ollama command-line client itself uses this package to interact with
 | 
					
						
							|  |  |  | // the backend service.
 | 
					
						
							| 
									
										
										
										
											2024-05-08 07:27:46 +08:00
										 |  |  | //
 | 
					
						
							|  |  |  | // # Examples
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // Several examples of using this package are available [in the GitHub
 | 
					
						
							|  |  |  | // repository].
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // [the API documentation]: https://github.com/ollama/ollama/blob/main/docs/api.md
 | 
					
						
							| 
									
										
										
										
											2025-02-28 02:51:12 +08:00
										 |  |  | // [in the GitHub repository]: https://github.com/ollama/ollama/tree/main/api/examples
 | 
					
						
							| 
									
										
										
										
											2023-07-04 03:22:44 +08:00
										 |  |  | package api | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2023-07-04 12:47:00 +08:00
										 |  |  | 	"bufio" | 
					
						
							| 
									
										
										
										
											2023-07-04 03:22:44 +08:00
										 |  |  | 	"bytes" | 
					
						
							|  |  |  | 	"context" | 
					
						
							|  |  |  | 	"encoding/json" | 
					
						
							| 
									
										
										
										
											2024-08-02 05:52:15 +08:00
										 |  |  | 	"errors" | 
					
						
							| 
									
										
										
										
											2023-07-08 05:04:43 +08:00
										 |  |  | 	"fmt" | 
					
						
							| 
									
										
										
										
											2023-07-19 00:09:45 +08:00
										 |  |  | 	"io" | 
					
						
							| 
									
										
										
										
											2023-07-04 03:22:44 +08:00
										 |  |  | 	"net/http" | 
					
						
							| 
									
										
										
										
											2023-07-07 06:02:10 +08:00
										 |  |  | 	"net/url" | 
					
						
							| 
									
										
										
										
											2023-08-22 09:24:42 +08:00
										 |  |  | 	"runtime" | 
					
						
							| 
									
										
										
										
											2025-05-28 07:50:57 +08:00
										 |  |  | 	"strconv" | 
					
						
							|  |  |  | 	"time" | 
					
						
							| 
									
										
										
										
											2023-08-22 09:24:42 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-05-28 07:50:57 +08:00
										 |  |  | 	"github.com/ollama/ollama/auth" | 
					
						
							| 
									
										
										
										
											2024-06-13 06:48:16 +08:00
										 |  |  | 	"github.com/ollama/ollama/envconfig" | 
					
						
							| 
									
										
										
										
											2024-03-27 04:04:17 +08:00
										 |  |  | 	"github.com/ollama/ollama/format" | 
					
						
							|  |  |  | 	"github.com/ollama/ollama/version" | 
					
						
							| 
									
										
										
										
											2023-08-16 23:03:48 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-11 01:31:55 +08:00
										 |  |  | // Client encapsulates client state for interacting with the ollama
 | 
					
						
							|  |  |  | // service. Use [ClientFromEnvironment] to create new Clients.
 | 
					
						
							| 
									
										
										
										
											2023-07-19 00:09:45 +08:00
										 |  |  | type Client struct { | 
					
						
							| 
									
										
										
										
											2023-10-10 03:18:26 +08:00
										 |  |  | 	base *url.URL | 
					
						
							| 
									
										
										
										
											2024-02-21 07:34:47 +08:00
										 |  |  | 	http *http.Client | 
					
						
							| 
									
										
										
										
											2023-07-12 04:05:51 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-19 00:09:45 +08:00
										 |  |  | func checkError(resp *http.Response, body []byte) error { | 
					
						
							| 
									
										
										
										
											2023-08-27 12:55:21 +08:00
										 |  |  | 	if resp.StatusCode < http.StatusBadRequest { | 
					
						
							| 
									
										
										
										
											2023-07-19 00:09:45 +08:00
										 |  |  | 		return nil | 
					
						
							| 
									
										
										
										
											2023-07-12 04:05:51 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-19 00:09:45 +08:00
										 |  |  | 	apiError := StatusError{StatusCode: resp.StatusCode} | 
					
						
							| 
									
										
										
										
											2023-07-12 04:05:51 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-19 00:09:45 +08: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-21 02:45:12 +08:00
										 |  |  | 		apiError.ErrorMessage = string(body) | 
					
						
							| 
									
										
										
										
											2023-07-19 00:09:45 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return apiError | 
					
						
							| 
									
										
										
										
											2023-07-07 06:02:10 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-11 01:31:55 +08:00
										 |  |  | // ClientFromEnvironment creates a new [Client] using configuration from the
 | 
					
						
							|  |  |  | // environment variable OLLAMA_HOST, which points to the network host and
 | 
					
						
							| 
									
										
										
										
											2024-11-11 09:30:27 +08:00
										 |  |  | // port on which the ollama service is listening. The format of this variable
 | 
					
						
							| 
									
										
										
										
											2024-04-11 01:31:55 +08:00
										 |  |  | // is:
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | //	<scheme>://<host>:<port>
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // If the variable is not specified, a default ollama host and port will be
 | 
					
						
							|  |  |  | // used.
 | 
					
						
							| 
									
										
										
										
											2023-10-10 03:18:26 +08:00
										 |  |  | func ClientFromEnvironment() (*Client, error) { | 
					
						
							| 
									
										
										
										
											2024-04-30 07:14:07 +08:00
										 |  |  | 	return &Client{ | 
					
						
							| 
									
										
										
										
											2024-07-04 07:44:57 +08:00
										 |  |  | 		base: envconfig.Host(), | 
					
						
							| 
									
										
										
										
											2024-04-30 07:14:07 +08:00
										 |  |  | 		http: http.DefaultClient, | 
					
						
							|  |  |  | 	}, nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-31 00:50:05 +08:00
										 |  |  | func NewClient(base *url.URL, http *http.Client) *Client { | 
					
						
							|  |  |  | 	return &Client{ | 
					
						
							|  |  |  | 		base: base, | 
					
						
							|  |  |  | 		http: http, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-05-28 07:50:57 +08:00
										 |  |  | func getAuthorizationToken(ctx context.Context, challenge string) (string, error) { | 
					
						
							|  |  |  | 	token, err := auth.Sign(ctx, []byte(challenge)) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return "", err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return token, nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-19 00:09:45 +08: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-15 06:07:40 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	switch reqData := reqData.(type) { | 
					
						
							|  |  |  | 	case io.Reader: | 
					
						
							|  |  |  | 		// reqData is already an io.Reader
 | 
					
						
							|  |  |  | 		reqBody = reqData | 
					
						
							|  |  |  | 	case nil: | 
					
						
							|  |  |  | 		// noop
 | 
					
						
							|  |  |  | 	default: | 
					
						
							| 
									
										
										
										
											2023-07-19 00:09:45 +08:00
										 |  |  | 		data, err = json.Marshal(reqData) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			return err | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2023-11-15 06:07:40 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-19 00:09:45 +08:00
										 |  |  | 		reqBody = bytes.NewReader(data) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-10 03:18:26 +08:00
										 |  |  | 	requestURL := c.base.JoinPath(path) | 
					
						
							| 
									
										
										
										
											2025-05-28 07:50:57 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	var token string | 
					
						
							|  |  |  | 	if envconfig.UseAuth() || c.base.Hostname() == "ollama.com" { | 
					
						
							|  |  |  | 		now := strconv.FormatInt(time.Now().Unix(), 10) | 
					
						
							|  |  |  | 		chal := fmt.Sprintf("%s,%s?ts=%s", method, path, now) | 
					
						
							|  |  |  | 		token, err = getAuthorizationToken(ctx, chal) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			return err | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		q := requestURL.Query() | 
					
						
							|  |  |  | 		q.Set("ts", now) | 
					
						
							|  |  |  | 		requestURL.RawQuery = q.Encode() | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-22 09:24:42 +08:00
										 |  |  | 	request, err := http.NewRequestWithContext(ctx, method, requestURL.String(), reqBody) | 
					
						
							| 
									
										
										
										
											2023-07-19 00:09:45 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-22 09:24:42 +08: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-19 00:09:45 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-05-28 07:50:57 +08:00
										 |  |  | 	if token != "" { | 
					
						
							|  |  |  | 		request.Header.Set("Authorization", token) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-10 03:18:26 +08:00
										 |  |  | 	respObj, err := c.http.Do(request) | 
					
						
							| 
									
										
										
										
											2023-07-19 00:09:45 +08: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-04 03:22:44 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-13 00:34:16 +08:00
										 |  |  | const maxBufferSize = 512 * format.KiloByte | 
					
						
							| 
									
										
										
										
											2023-10-05 02:09:00 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-12 04:05:51 +08:00
										 |  |  | func (c *Client) stream(ctx context.Context, method, path string, data any, fn func([]byte) error) error { | 
					
						
							| 
									
										
										
										
											2025-02-21 05:19:58 +08:00
										 |  |  | 	var buf io.Reader | 
					
						
							| 
									
										
										
										
											2023-07-11 12:34:15 +08:00
										 |  |  | 	if data != nil { | 
					
						
							|  |  |  | 		bts, err := json.Marshal(data) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			return err | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2023-07-07 07:53:14 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-11 12:34:15 +08:00
										 |  |  | 		buf = bytes.NewBuffer(bts) | 
					
						
							| 
									
										
										
										
											2023-07-04 03:22:44 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-10 03:18:26 +08:00
										 |  |  | 	requestURL := c.base.JoinPath(path) | 
					
						
							| 
									
										
										
										
											2025-05-28 07:50:57 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	var token string | 
					
						
							|  |  |  | 	if envconfig.UseAuth() || c.base.Hostname() == "ollama.com" { | 
					
						
							|  |  |  | 		var err error | 
					
						
							|  |  |  | 		now := strconv.FormatInt(time.Now().Unix(), 10) | 
					
						
							|  |  |  | 		chal := fmt.Sprintf("%s,%s?ts=%s", method, path, now) | 
					
						
							|  |  |  | 		token, err = getAuthorizationToken(ctx, chal) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			return err | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		q := requestURL.Query() | 
					
						
							|  |  |  | 		q.Set("ts", now) | 
					
						
							|  |  |  | 		requestURL.RawQuery = q.Encode() | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-22 09:24:42 +08:00
										 |  |  | 	request, err := http.NewRequestWithContext(ctx, method, requestURL.String(), buf) | 
					
						
							| 
									
										
										
										
											2023-07-04 03:22:44 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-07 06:02:10 +08:00
										 |  |  | 	request.Header.Set("Content-Type", "application/json") | 
					
						
							| 
									
										
										
										
											2023-10-12 00:54:27 +08:00
										 |  |  | 	request.Header.Set("Accept", "application/x-ndjson") | 
					
						
							| 
									
										
										
										
											2023-08-22 09:24:42 +08: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 12:47:00 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-05-28 07:50:57 +08:00
										 |  |  | 	if token != "" { | 
					
						
							|  |  |  | 		request.Header.Set("Authorization", token) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-10 03:18:26 +08:00
										 |  |  | 	response, err := c.http.Do(request) | 
					
						
							| 
									
										
										
										
											2023-07-04 12:47:00 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-07-07 06:02:10 +08:00
										 |  |  | 	defer response.Body.Close() | 
					
						
							| 
									
										
										
										
											2023-07-04 12:47:00 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-11 12:34:15 +08:00
										 |  |  | 	scanner := bufio.NewScanner(response.Body) | 
					
						
							| 
									
										
										
										
											2023-10-05 02:09:00 +08:00
										 |  |  | 	// increase the buffer size to avoid running out of space
 | 
					
						
							|  |  |  | 	scanBuf := make([]byte, 0, maxBufferSize) | 
					
						
							|  |  |  | 	scanner.Buffer(scanBuf, maxBufferSize) | 
					
						
							| 
									
										
										
										
											2023-07-11 12:34:15 +08:00
										 |  |  | 	for scanner.Scan() { | 
					
						
							|  |  |  | 		var errorResponse struct { | 
					
						
							| 
									
										
										
										
											2023-07-12 04:05:51 +08:00
										 |  |  | 			Error string `json:"error,omitempty"` | 
					
						
							| 
									
										
										
										
											2023-07-11 12:34:15 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		bts := scanner.Bytes() | 
					
						
							|  |  |  | 		if err := json.Unmarshal(bts, &errorResponse); err != nil { | 
					
						
							|  |  |  | 			return fmt.Errorf("unmarshal: %w", err) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-21 03:12:08 +08:00
										 |  |  | 		if errorResponse.Error != "" { | 
					
						
							| 
									
										
										
										
											2024-08-02 05:52:15 +08:00
										 |  |  | 			return errors.New(errorResponse.Error) | 
					
						
							| 
									
										
										
										
											2023-07-21 03:12:08 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-27 12:55:21 +08:00
										 |  |  | 		if response.StatusCode >= http.StatusBadRequest { | 
					
						
							| 
									
										
										
										
											2023-07-12 04:05:51 +08:00
										 |  |  | 			return StatusError{ | 
					
						
							| 
									
										
										
										
											2023-07-21 02:45:12 +08:00
										 |  |  | 				StatusCode:   response.StatusCode, | 
					
						
							|  |  |  | 				Status:       response.Status, | 
					
						
							|  |  |  | 				ErrorMessage: errorResponse.Error, | 
					
						
							| 
									
										
										
										
											2023-07-12 04:05:51 +08:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2023-07-11 12:34:15 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-12 04:05:51 +08:00
										 |  |  | 		if err := fn(bts); err != nil { | 
					
						
							| 
									
										
										
										
											2023-07-11 12:34:15 +08:00
										 |  |  | 			return err | 
					
						
							| 
									
										
										
										
											2023-07-04 03:22:44 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-07 05:05:55 +08:00
										 |  |  | 	return nil | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2023-07-04 03:22:44 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-11 01:31:55 +08: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-07 05:05:55 +08:00
										 |  |  | type GenerateResponseFunc func(GenerateResponse) error | 
					
						
							| 
									
										
										
										
											2023-07-04 03:22:44 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-11 01:31:55 +08: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-07 05:05:55 +08:00
										 |  |  | func (c *Client) Generate(ctx context.Context, req *GenerateRequest, fn GenerateResponseFunc) error { | 
					
						
							| 
									
										
										
										
											2023-07-11 12:34:15 +08: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 12:47:00 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2023-07-07 00:24:49 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-11 01:31:55 +08: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-06 03:57:33 +08:00
										 |  |  | type ChatResponseFunc func(ChatResponse) error | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-11 01:31:55 +08: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-06 03:57:33 +08: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-11 01:31:55 +08: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 09:51:30 +08:00
										 |  |  | type PullProgressFunc func(ProgressResponse) error | 
					
						
							| 
									
										
										
										
											2023-07-07 05:05:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-11 01:31:55 +08: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-07 05:05:55 +08:00
										 |  |  | func (c *Client) Pull(ctx context.Context, req *PullRequest, fn PullProgressFunc) error { | 
					
						
							| 
									
										
										
										
											2023-07-11 12:34:15 +08:00
										 |  |  | 	return c.stream(ctx, http.MethodPost, "/api/pull", req, func(bts []byte) error { | 
					
						
							| 
									
										
										
										
											2023-07-19 09:51:30 +08:00
										 |  |  | 		var resp ProgressResponse | 
					
						
							| 
									
										
										
										
											2023-07-11 12:34:15 +08:00
										 |  |  | 		if err := json.Unmarshal(bts, &resp); err != nil { | 
					
						
							|  |  |  | 			return err | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		return fn(resp) | 
					
						
							|  |  |  | 	}) | 
					
						
							| 
									
										
										
										
											2023-07-07 00:24:49 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2023-07-17 08:02:22 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-08 07:27:46 +08:00
										 |  |  | // PushProgressFunc is a function that [Client.Push] invokes when progress is
 | 
					
						
							|  |  |  | // made.
 | 
					
						
							|  |  |  | // It's similar to other progress function types like [PullProgressFunc].
 | 
					
						
							| 
									
										
										
										
											2023-07-19 09:51:30 +08:00
										 |  |  | type PushProgressFunc func(ProgressResponse) error | 
					
						
							| 
									
										
										
										
											2023-07-17 08:02:22 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-08 07:27:46 +08:00
										 |  |  | // Push uploads a model to the model library; requires registering for ollama.ai
 | 
					
						
							|  |  |  | // and adding a public key first. fn is called each time progress is made on
 | 
					
						
							|  |  |  | // the request and can be used to display a progress bar, etc.
 | 
					
						
							| 
									
										
										
										
											2023-07-17 08:02:22 +08: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 09:51:30 +08:00
										 |  |  | 		var resp ProgressResponse | 
					
						
							| 
									
										
										
										
											2023-07-17 08:02:22 +08:00
										 |  |  | 		if err := json.Unmarshal(bts, &resp); err != nil { | 
					
						
							|  |  |  | 			return err | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		return fn(resp) | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-08 07:27:46 +08:00
										 |  |  | // CreateProgressFunc is a function that [Client.Create] invokes when progress
 | 
					
						
							|  |  |  | // is made.
 | 
					
						
							|  |  |  | // It's similar to other progress function types like [PullProgressFunc].
 | 
					
						
							| 
									
										
										
										
											2023-07-26 02:25:13 +08:00
										 |  |  | type CreateProgressFunc func(ProgressResponse) error | 
					
						
							| 
									
										
										
										
											2023-07-17 08:02:22 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-08 07:27:46 +08:00
										 |  |  | // Create creates a model from a [Modelfile]. fn is a progress function that
 | 
					
						
							|  |  |  | // behaves similarly to other methods (see [Client.Pull]).
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // [Modelfile]: https://github.com/ollama/ollama/blob/main/docs/modelfile.md
 | 
					
						
							| 
									
										
										
										
											2023-07-17 08:02:22 +08: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-26 02:25:13 +08:00
										 |  |  | 		var resp ProgressResponse | 
					
						
							| 
									
										
										
										
											2023-07-17 08:02:22 +08:00
										 |  |  | 		if err := json.Unmarshal(bts, &resp); err != nil { | 
					
						
							|  |  |  | 			return err | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		return fn(resp) | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2023-07-19 00:09:45 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-08 07:27:46 +08:00
										 |  |  | // List lists models that are available locally.
 | 
					
						
							| 
									
										
										
										
											2023-07-19 00:09:45 +08: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 { | 
					
						
							| 
									
										
										
										
											2024-05-14 08:17:36 +08:00
										 |  |  | 		return nil, err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return &lr, nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-14 08:54:19 +08:00
										 |  |  | // ListRunning lists running models.
 | 
					
						
							| 
									
										
										
										
											2024-06-07 01:11:45 +08:00
										 |  |  | func (c *Client) ListRunning(ctx context.Context) (*ProcessResponse, error) { | 
					
						
							|  |  |  | 	var lr ProcessResponse | 
					
						
							| 
									
										
										
										
											2024-05-14 08:17:36 +08:00
										 |  |  | 	if err := c.do(ctx, http.MethodGet, "/api/ps", nil, &lr); err != nil { | 
					
						
							| 
									
										
										
										
											2023-07-19 00:09:45 +08:00
										 |  |  | 		return nil, err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return &lr, nil | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2023-07-21 07:09:23 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-08 07:27:46 +08:00
										 |  |  | // Copy copies a model - creating a model with another name from an existing
 | 
					
						
							|  |  |  | // model.
 | 
					
						
							| 
									
										
										
										
											2023-07-24 23:27:28 +08: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 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-08 07:27:46 +08:00
										 |  |  | // Delete deletes a model and its data.
 | 
					
						
							| 
									
										
										
										
											2023-07-22 14:02:12 +08: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-21 07:09:23 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2023-08-01 04:25:57 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-08 07:27:46 +08:00
										 |  |  | // Show obtains model information, including details, modelfile, license etc.
 | 
					
						
							| 
									
										
										
										
											2023-09-07 02:04:17 +08: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 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-14 08:54:19 +08:00
										 |  |  | // Heartbeat checks if the server has started and is responsive; if yes, it
 | 
					
						
							| 
									
										
										
										
											2024-05-08 07:27:46 +08:00
										 |  |  | // returns nil, otherwise an error.
 | 
					
						
							| 
									
										
										
										
											2023-08-01 04:25:57 +08:00
										 |  |  | func (c *Client) Heartbeat(ctx context.Context) error { | 
					
						
							| 
									
										
										
										
											2023-08-02 02:50:38 +08:00
										 |  |  | 	if err := c.do(ctx, http.MethodHead, "/", nil, nil); err != nil { | 
					
						
							| 
									
										
										
										
											2023-08-01 04:25:57 +08:00
										 |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return nil | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2024-05-08 07:27:46 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-16 03:14:24 +08:00
										 |  |  | // Embed generates embeddings from a model.
 | 
					
						
							|  |  |  | func (c *Client) Embed(ctx context.Context, req *EmbedRequest) (*EmbedResponse, error) { | 
					
						
							|  |  |  | 	var resp EmbedResponse | 
					
						
							|  |  |  | 	if err := c.do(ctx, http.MethodPost, "/api/embed", req, &resp); err != nil { | 
					
						
							|  |  |  | 		return nil, err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return &resp, nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Embeddings generates an embedding from a model.
 | 
					
						
							| 
									
										
										
										
											2024-01-05 04:00:52 +08: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-15 06:07:40 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-08 07:27:46 +08:00
										 |  |  | // CreateBlob creates a blob from a file on the server. digest is the
 | 
					
						
							|  |  |  | // expected SHA256 digest of the file, and r represents the file.
 | 
					
						
							| 
									
										
										
										
											2023-11-16 02:59:38 +08:00
										 |  |  | func (c *Client) CreateBlob(ctx context.Context, digest string, r io.Reader) error { | 
					
						
							| 
									
										
										
										
											2024-04-06 00:30:09 +08:00
										 |  |  | 	return c.do(ctx, http.MethodPost, fmt.Sprintf("/api/blobs/%s", digest), r, nil) | 
					
						
							| 
									
										
										
										
											2023-11-15 06:07:40 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2023-11-23 01:41:44 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-08 07:27:46 +08:00
										 |  |  | // Version returns the Ollama server version as a string.
 | 
					
						
							| 
									
										
										
										
											2023-11-23 01:41:44 +08: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 | 
					
						
							|  |  |  | } |