api

package
v0.1.34 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 7, 2024 License: MIT Imports: 19 Imported by: 334

Documentation

Overview

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.

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidHostPort = errors.New("invalid port specified in OLLAMA_HOST")
View Source
var ErrInvalidOpts = errors.New("invalid options")

Functions

func FormatParams

func FormatParams(params map[string][]string) (map[string]interface{}, error)

FormatParams converts specified parameter options to their correct types

Types

type ChatRequest

type ChatRequest struct {
	Model     string    `json:"model"`
	Messages  []Message `json:"messages"`
	Stream    *bool     `json:"stream,omitempty"`
	Format    string    `json:"format"`
	KeepAlive *Duration `json:"keep_alive,omitempty"`

	Options map[string]interface{} `json:"options"`
}

type ChatResponse

type ChatResponse struct {
	Model     string    `json:"model"`
	CreatedAt time.Time `json:"created_at"`
	Message   Message   `json:"message"`

	Done bool `json:"done"`

	Metrics
}

type ChatResponseFunc

type ChatResponseFunc func(ChatResponse) error

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.

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client encapsulates client state for interacting with the ollama service. Use ClientFromEnvironment to create new Clients.

func ClientFromEnvironment

func ClientFromEnvironment() (*Client, error)

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.

func NewClient added in v0.1.33

func NewClient(base *url.URL, http *http.Client) *Client

func (*Client) Chat

func (c *Client) Chat(ctx context.Context, req *ChatRequest, fn ChatResponseFunc) error

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).

func (*Client) Copy

func (c *Client) Copy(ctx context.Context, req *CopyRequest) error

func (*Client) Create

func (c *Client) Create(ctx context.Context, req *CreateRequest, fn CreateProgressFunc) error

func (*Client) CreateBlob

func (c *Client) CreateBlob(ctx context.Context, digest string, r io.Reader) error

func (*Client) Delete

func (c *Client) Delete(ctx context.Context, req *DeleteRequest) error

func (*Client) Embeddings

func (c *Client) Embeddings(ctx context.Context, req *EmbeddingRequest) (*EmbeddingResponse, error)

func (*Client) Generate

func (c *Client) Generate(ctx context.Context, req *GenerateRequest, fn GenerateResponseFunc) error

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).

func (*Client) Heartbeat

func (c *Client) Heartbeat(ctx context.Context) error

func (*Client) List

func (c *Client) List(ctx context.Context) (*ListResponse, error)

func (*Client) Pull

func (c *Client) Pull(ctx context.Context, req *PullRequest, fn PullProgressFunc) error

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.

func (*Client) Push

func (c *Client) Push(ctx context.Context, req *PushRequest, fn PushProgressFunc) error

func (*Client) Show

func (c *Client) Show(ctx context.Context, req *ShowRequest) (*ShowResponse, error)

func (*Client) Version

func (c *Client) Version(ctx context.Context) (string, error)

type CopyRequest

type CopyRequest struct {
	Source      string `json:"source"`
	Destination string `json:"destination"`
}

type CreateProgressFunc

type CreateProgressFunc func(ProgressResponse) error

type CreateRequest

type CreateRequest struct {
	Model        string `json:"model"`
	Path         string `json:"path"`
	Modelfile    string `json:"modelfile"`
	Stream       *bool  `json:"stream,omitempty"`
	Quantization string `json:"quantization,omitempty"`

	// Name is deprecated, see Model
	Name string `json:"name"`
}

type DeleteRequest

type DeleteRequest struct {
	Model string `json:"model"`

	// Name is deprecated, see Model
	Name string `json:"name"`
}

type Duration

type Duration struct {
	time.Duration
}

func (Duration) MarshalJSON added in v0.1.34

func (d Duration) MarshalJSON() ([]byte, error)

func (*Duration) UnmarshalJSON

func (d *Duration) UnmarshalJSON(b []byte) (err error)

type EmbeddingRequest

type EmbeddingRequest struct {
	Model     string    `json:"model"`
	Prompt    string    `json:"prompt"`
	KeepAlive *Duration `json:"keep_alive,omitempty"`

	Options map[string]interface{} `json:"options"`
}

type EmbeddingResponse

type EmbeddingResponse struct {
	Embedding []float64 `json:"embedding"`
}

type GenerateRequest

type GenerateRequest struct {
	// Model is the model name; it should be a name familiar to Ollama from
	// the library at https://ollama.com/library
	Model string `json:"model"`

	// Prompt is the textual prompt to send to the model.
	Prompt string `json:"prompt"`

	// System overrides the model's default system message/prompt.
	System string `json:"system"`

	// Template overrides the model's default prompt template.
	Template string `json:"template"`

	// Context is the context parameter returned from a previous call to
	// Generate call. It can be used to keep a short conversational memory.
	Context []int `json:"context,omitempty"`

	// Stream specifies whether the response is streaming; it is true by default.
	Stream *bool `json:"stream,omitempty"`

	// Raw set to true means that no formatting will be applied to the prompt.
	Raw bool `json:"raw,omitempty"`

	// Format specifies the format to return a response in.
	Format string `json:"format"`

	// KeepAlive controls how long the model will stay loaded in memory following
	// this request.
	KeepAlive *Duration `json:"keep_alive,omitempty"`

	// Images is an optional list of base64-encoded images accompanying this
	// request, for multimodal models.
	Images []ImageData `json:"images,omitempty"`

	// Options lists model-specific options. For example, temperature can be
	// set through this field, if the model supports it.
	Options map[string]interface{} `json:"options"`
}

GenerateRequest describes a request sent by Client.Generate. While you have to specify the Model and Prompt fields, all the other fields have reasonable defaults for basic uses.

type GenerateResponse

type GenerateResponse struct {
	Model     string    `json:"model"`
	CreatedAt time.Time `json:"created_at"`
	Response  string    `json:"response"`

	Done    bool  `json:"done"`
	Context []int `json:"context,omitempty"`

	Metrics
}

type GenerateResponseFunc

type GenerateResponseFunc func(GenerateResponse) error

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.

type ImageData

type ImageData []byte

type ListResponse

type ListResponse struct {
	Models []ModelResponse `json:"models"`
}

type Message

type Message struct {
	Role    string      `json:"role"` // one of ["system", "user", "assistant"]
	Content string      `json:"content"`
	Images  []ImageData `json:"images,omitempty"`
}

type Metrics

type Metrics struct {
	TotalDuration      time.Duration `json:"total_duration,omitempty"`
	LoadDuration       time.Duration `json:"load_duration,omitempty"`
	PromptEvalCount    int           `json:"prompt_eval_count,omitempty"`
	PromptEvalDuration time.Duration `json:"prompt_eval_duration,omitempty"`
	EvalCount          int           `json:"eval_count,omitempty"`
	EvalDuration       time.Duration `json:"eval_duration,omitempty"`
}

func (*Metrics) Summary

func (m *Metrics) Summary()

type ModelDetails

type ModelDetails struct {
	ParentModel       string   `json:"parent_model"`
	Format            string   `json:"format"`
	Family            string   `json:"family"`
	Families          []string `json:"families"`
	ParameterSize     string   `json:"parameter_size"`
	QuantizationLevel string   `json:"quantization_level"`
}

type ModelResponse

type ModelResponse struct {
	Name       string       `json:"name"`
	Model      string       `json:"model"`
	ModifiedAt time.Time    `json:"modified_at"`
	Size       int64        `json:"size"`
	Digest     string       `json:"digest"`
	Details    ModelDetails `json:"details,omitempty"`
}

type OllamaHost added in v0.1.33

type OllamaHost struct {
	Scheme string
	Host   string
	Port   string
}

func GetOllamaHost added in v0.1.33

func GetOllamaHost() (OllamaHost, error)

type Options

type Options struct {
	Runner

	// Predict options used at runtime
	NumKeep          int      `json:"num_keep,omitempty"`
	Seed             int      `json:"seed,omitempty"`
	NumPredict       int      `json:"num_predict,omitempty"`
	TopK             int      `json:"top_k,omitempty"`
	TopP             float32  `json:"top_p,omitempty"`
	TFSZ             float32  `json:"tfs_z,omitempty"`
	TypicalP         float32  `json:"typical_p,omitempty"`
	RepeatLastN      int      `json:"repeat_last_n,omitempty"`
	Temperature      float32  `json:"temperature,omitempty"`
	RepeatPenalty    float32  `json:"repeat_penalty,omitempty"`
	PresencePenalty  float32  `json:"presence_penalty,omitempty"`
	FrequencyPenalty float32  `json:"frequency_penalty,omitempty"`
	Mirostat         int      `json:"mirostat,omitempty"`
	MirostatTau      float32  `json:"mirostat_tau,omitempty"`
	MirostatEta      float32  `json:"mirostat_eta,omitempty"`
	PenalizeNewline  bool     `json:"penalize_newline,omitempty"`
	Stop             []string `json:"stop,omitempty"`
}

Options specified in GenerateRequest, if you add a new option here add it to the API docs also

func DefaultOptions

func DefaultOptions() Options

func (*Options) FromMap

func (opts *Options) FromMap(m map[string]interface{}) error

type ProgressResponse

type ProgressResponse struct {
	Status    string `json:"status"`
	Digest    string `json:"digest,omitempty"`
	Total     int64  `json:"total,omitempty"`
	Completed int64  `json:"completed,omitempty"`
}

type PullProgressFunc

type PullProgressFunc func(ProgressResponse) error

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.

type PullRequest

type PullRequest struct {
	Model    string `json:"model"`
	Insecure bool   `json:"insecure,omitempty"`
	Username string `json:"username"`
	Password string `json:"password"`
	Stream   *bool  `json:"stream,omitempty"`

	// Name is deprecated, see Model
	Name string `json:"name"`
}

type PushProgressFunc

type PushProgressFunc func(ProgressResponse) error

type PushRequest

type PushRequest struct {
	Model    string `json:"model"`
	Insecure bool   `json:"insecure,omitempty"`
	Username string `json:"username"`
	Password string `json:"password"`
	Stream   *bool  `json:"stream,omitempty"`

	// Name is deprecated, see Model
	Name string `json:"name"`
}

type Runner

type Runner struct {
	UseNUMA   bool `json:"numa,omitempty"`
	NumCtx    int  `json:"num_ctx,omitempty"`
	NumBatch  int  `json:"num_batch,omitempty"`
	NumGQA    int  `json:"num_gqa,omitempty"`
	NumGPU    int  `json:"num_gpu,omitempty"`
	MainGPU   int  `json:"main_gpu,omitempty"`
	LowVRAM   bool `json:"low_vram,omitempty"`
	F16KV     bool `json:"f16_kv,omitempty"`
	LogitsAll bool `json:"logits_all,omitempty"`
	VocabOnly bool `json:"vocab_only,omitempty"`
	UseMMap   bool `json:"use_mmap,omitempty"`
	UseMLock  bool `json:"use_mlock,omitempty"`
	NumThread int  `json:"num_thread,omitempty"`

	// Unused: RopeFrequencyBase is ignored. Instead the value in the model will be used
	RopeFrequencyBase float32 `json:"rope_frequency_base,omitempty"`
	// Unused: RopeFrequencyScale is ignored. Instead the value in the model will be used
	RopeFrequencyScale float32 `json:"rope_frequency_scale,omitempty"`
}

Runner options which must be set when the model is loaded into memory

type ShowRequest

type ShowRequest struct {
	Model    string `json:"model"`
	System   string `json:"system"`
	Template string `json:"template"`

	Options map[string]interface{} `json:"options"`

	// Name is deprecated, see Model
	Name string `json:"name"`
}

type ShowResponse

type ShowResponse struct {
	License    string       `json:"license,omitempty"`
	Modelfile  string       `json:"modelfile,omitempty"`
	Parameters string       `json:"parameters,omitempty"`
	Template   string       `json:"template,omitempty"`
	System     string       `json:"system,omitempty"`
	Details    ModelDetails `json:"details,omitempty"`
	Messages   []Message    `json:"messages,omitempty"`
}

type StatusError

type StatusError struct {
	StatusCode   int
	Status       string
	ErrorMessage string `json:"error"`
}

func (StatusError) Error

func (e StatusError) Error() string

type TokenResponse

type TokenResponse struct {
	Token string `json:"token"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL