llm_driver_ollama

package
v0.3.36 Latest Latest
Warning

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

Go to latest
Published: Feb 28, 2025 License: BSD-3-Clause Imports: 12 Imported by: 0

README

OLLAMA direct API implementation

https://github.com/ollama/ollama/blob/main/examples/go-generate-streaming/main.go

go get github.com/ollama/ollama/api
package main

import (
	"context"
	"fmt"
	"log"

	"github.com/ollama/ollama/api"
)

func main() {
	client, err := api.ClientFromEnvironment()
	if err != nil {
		log.Fatal(err)
	}

	// By default, GenerateRequest is streaming.
	req := &api.GenerateRequest{
		Model:  "gemma2",
		Prompt: "how many planets are there?",
	}

	ctx := context.Background()
	respFunc := func(resp api.GenerateResponse) error {
		// Only print the response here; GenerateResponse has a number of other
		// interesting fields you want to examine.

		// In streaming mode, responses are partial so we call fmt.Print (and not
		// Println) in order to avoid spurious newlines being introduced. The
		// model will insert its own newlines if it wants.
		fmt.Print(resp.Response)
		return nil
	}

	err = client.Generate(ctx, req, respFunc)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println()
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CopyModel

func CopyModel(source, target string) (response string, err error)

func JsonFormat

func JsonFormat(value string) (response []byte)

func List

func List() (response string, err error)

func Preload

func Preload(name string) (response string, err error)

func Pull

func Pull(name string) (response string, err error)

func Remove

func Remove(name string) (response string, err error)

func Run

func Run(name string) (response string, err error)

func Version

func Version() (response string, err error)

Types

type OllamaAPI

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

OllamaAPI is a struct that manages interactions with the Ollama API, handling configuration and API client setup.

func NewOllamaAPI

func NewOllamaAPI(args ...interface{}) (instance *OllamaAPI, err error)

func (*OllamaAPI) Chat

func (instance *OllamaAPI) Chat(model string, oMessages llm_commons.AIRequestMessageList,
	options map[string]interface{}, keepAlive *api.Duration, format string,
	stream llm_commons.StreamHandler) (response *ollama_commons.OllamaCLIResponse)

func (*OllamaAPI) Delete

func (instance *OllamaAPI) Delete(model string) (response *ollama_commons.OllamaCLIResponse)

Delete removes a specific model identified by the given `model` name from the Ollama API server and returns the result.

func (*OllamaAPI) DriverOptions

func (instance *OllamaAPI) DriverOptions() (response *llm_commons.LLMDriverOptions)

func (*OllamaAPI) Generate

func (instance *OllamaAPI) Generate(model string, prompt *llm_commons.AIRequestMessage, raw bool,
	ctx interface{}, template, system string,
	options map[string]interface{}, keepAlive *api.Duration, format string,
	stream llm_commons.StreamHandler) (response *ollama_commons.OllamaCLIResponse)

func (*OllamaAPI) GetModel

func (instance *OllamaAPI) GetModel() (response string)

func (*OllamaAPI) List

func (instance *OllamaAPI) List() (response *ollama_commons.OllamaCLIResponse)

func (*OllamaAPI) ListNames

func (instance *OllamaAPI) ListNames() (response *ollama_commons.OllamaCLIResponse)

func (*OllamaAPI) Map

func (instance *OllamaAPI) Map() (response map[string]interface{})

func (*OllamaAPI) Options

func (instance *OllamaAPI) Options() (response *ollama_commons.OllamaOptions)

func (*OllamaAPI) Pull

func (instance *OllamaAPI) Pull(model string, enableStream bool) (response *ollama_commons.OllamaCLIResponse)

Pull Download or Update a model

func (*OllamaAPI) Serve

func (instance *OllamaAPI) Serve()

Serve start ollama server. WARN: USE ONLY IF THE SERVICE DOES NOT START AUTOMATICALLY!!!

func (*OllamaAPI) String

func (instance *OllamaAPI) String() (response string)

func (*OllamaAPI) Submit added in v0.3.36

func (instance *OllamaAPI) Submit(request *llm_commons.LLMRequest) (response *llm_commons.LLMResponse, err error)

func (*OllamaAPI) Version

func (instance *OllamaAPI) Version() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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