123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328 |
- package api
- import (
- "bufio"
- "bytes"
- "context"
- "encoding/json"
- "errors"
- "fmt"
- "io"
- "net"
- "net/http"
- "net/url"
- "os"
- "runtime"
- "strings"
- "github.com/ollama/ollama/format"
- "github.com/ollama/ollama/version"
- )
- type Client struct {
- base *url.URL
- http *http.Client
- }
- func checkError(resp *http.Response, body []byte) error {
- if resp.StatusCode < http.StatusBadRequest {
- return nil
- }
- apiError := StatusError{StatusCode: resp.StatusCode}
- err := json.Unmarshal(body, &apiError)
- if err != nil {
- // Use the full body as the message if we fail to decode a response.
- apiError.ErrorMessage = string(body)
- }
- return apiError
- }
- func ClientFromEnvironment() (*Client, error) {
- defaultPort := "11434"
- scheme, hostport, ok := strings.Cut(os.Getenv("OLLAMA_HOST"), "://")
- switch {
- case !ok:
- scheme, hostport = "http", os.Getenv("OLLAMA_HOST")
- case scheme == "http":
- defaultPort = "80"
- case scheme == "https":
- defaultPort = "443"
- }
- // trim trailing slashes
- hostport = strings.TrimRight(hostport, "/")
- host, port, err := net.SplitHostPort(hostport)
- if err != nil {
- host, port = "127.0.0.1", defaultPort
- if ip := net.ParseIP(strings.Trim(hostport, "[]")); ip != nil {
- host = ip.String()
- } else if hostport != "" {
- host = hostport
- }
- }
- return &Client{
- base: &url.URL{
- Scheme: scheme,
- Host: net.JoinHostPort(host, port),
- },
- http: http.DefaultClient,
- }, nil
- }
- func (c *Client) do(ctx context.Context, method, path string, reqData, respData any) error {
- var reqBody io.Reader
- var data []byte
- var err error
- switch reqData := reqData.(type) {
- case io.Reader:
- // reqData is already an io.Reader
- reqBody = reqData
- case nil:
- // noop
- default:
- data, err = json.Marshal(reqData)
- if err != nil {
- return err
- }
- reqBody = bytes.NewReader(data)
- }
- requestURL := c.base.JoinPath(path)
- request, err := http.NewRequestWithContext(ctx, method, requestURL.String(), reqBody)
- if err != nil {
- return err
- }
- 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()))
- respObj, err := c.http.Do(request)
- 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
- }
- const maxBufferSize = 512 * format.KiloByte
- func (c *Client) stream(ctx context.Context, method, path string, data any, fn func([]byte) error) error {
- var buf *bytes.Buffer
- if data != nil {
- bts, err := json.Marshal(data)
- if err != nil {
- return err
- }
- buf = bytes.NewBuffer(bts)
- }
- requestURL := c.base.JoinPath(path)
- request, err := http.NewRequestWithContext(ctx, method, requestURL.String(), buf)
- if err != nil {
- return err
- }
- request.Header.Set("Content-Type", "application/json")
- request.Header.Set("Accept", "application/x-ndjson")
- request.Header.Set("User-Agent", fmt.Sprintf("ollama/%s (%s %s) Go/%s", version.Version, runtime.GOARCH, runtime.GOOS, runtime.Version()))
- response, err := c.http.Do(request)
- if err != nil {
- return err
- }
- defer response.Body.Close()
- scanner := bufio.NewScanner(response.Body)
- // increase the buffer size to avoid running out of space
- scanBuf := make([]byte, 0, maxBufferSize)
- scanner.Buffer(scanBuf, maxBufferSize)
- for scanner.Scan() {
- var errorResponse struct {
- Error string `json:"error,omitempty"`
- }
- bts := scanner.Bytes()
- if err := json.Unmarshal(bts, &errorResponse); err != nil {
- return fmt.Errorf("unmarshal: %w", err)
- }
- if errorResponse.Error != "" {
- return fmt.Errorf(errorResponse.Error)
- }
- if response.StatusCode >= http.StatusBadRequest {
- return StatusError{
- StatusCode: response.StatusCode,
- Status: response.Status,
- ErrorMessage: errorResponse.Error,
- }
- }
- if err := fn(bts); err != nil {
- return err
- }
- }
- return nil
- }
- type GenerateResponseFunc func(GenerateResponse) error
- func (c *Client) Generate(ctx context.Context, req *GenerateRequest, fn GenerateResponseFunc) error {
- 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)
- })
- }
- type ChatResponseFunc func(ChatResponse) error
- 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)
- })
- }
- type PullProgressFunc func(ProgressResponse) error
- func (c *Client) Pull(ctx context.Context, req *PullRequest, fn PullProgressFunc) error {
- return c.stream(ctx, http.MethodPost, "/api/pull", req, func(bts []byte) error {
- var resp ProgressResponse
- if err := json.Unmarshal(bts, &resp); err != nil {
- return err
- }
- return fn(resp)
- })
- }
- type PushProgressFunc func(ProgressResponse) error
- 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 {
- var resp ProgressResponse
- if err := json.Unmarshal(bts, &resp); err != nil {
- return err
- }
- return fn(resp)
- })
- }
- type CreateProgressFunc func(ProgressResponse) error
- 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 {
- var resp ProgressResponse
- if err := json.Unmarshal(bts, &resp); err != nil {
- return err
- }
- return fn(resp)
- })
- }
- 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
- }
- 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
- }
- 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
- }
- 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
- }
- func (c *Client) Heartbeat(ctx context.Context) error {
- if err := c.do(ctx, http.MethodHead, "/", nil, nil); err != nil {
- return err
- }
- return nil
- }
- 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
- }
- func (c *Client) CreateBlob(ctx context.Context, digest string, r io.Reader) error {
- if err := c.do(ctx, http.MethodHead, fmt.Sprintf("/api/blobs/%s", digest), nil, nil); err != nil {
- var statusError StatusError
- if !errors.As(err, &statusError) || statusError.StatusCode != http.StatusNotFound {
- return err
- }
- if err := c.do(ctx, http.MethodPost, fmt.Sprintf("/api/blobs/%s", digest), r, nil); err != nil {
- return err
- }
- }
- return nil
- }
- 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
- }
|