Skip to content

Commit

Permalink
remove ollama tools workaround
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianliechti committed Dec 1, 2024
1 parent 9f06a59 commit 2c69a18
Show file tree
Hide file tree
Showing 12 changed files with 36 additions and 95 deletions.
1 change: 1 addition & 0 deletions config/config_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ func DetectModelType(id string) ModelType {
"o1",
"orca",
"phi",
"pixtral",
"qwen",
"stable-code",
"stablelm",
Expand Down
6 changes: 3 additions & 3 deletions pkg/provider/azure/completer.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ func NewCompleter(url, model string, options ...Option) (*Completer, error) {
url = "https://models.inference.ai.azure.com"
}

c := &Config{}
cfg := &Config{}

for _, option := range options {
option(c)
option(cfg)
}

return openai.NewCompleter(url, model, c.options...)
return openai.NewCompleter(url, model, cfg.options...)
}
6 changes: 3 additions & 3 deletions pkg/provider/azure/embedder.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ func NewEmbedder(url, model string, options ...Option) (*Embedder, error) {
url = "https://models.inference.ai.azure.com"
}

c := &Config{}
cfg := &Config{}

for _, option := range options {
option(c)
option(cfg)
}

return openai.NewEmbedder(url, model, c.options...)
return openai.NewEmbedder(url, model, cfg.options...)
}
8 changes: 4 additions & 4 deletions pkg/provider/custom/completer.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ func NewCompleter(url string, options ...Option) (*Completer, error) {
return nil, errors.New("invalid url")
}

c := &Config{
cfg := &Config{
url: url,
}

for _, option := range options {
option(c)
option(cfg)
}

client, err := grpc.NewClient(strings.TrimPrefix(c.url, "grpc://"),
client, err := grpc.NewClient(strings.TrimPrefix(cfg.url, "grpc://"),
grpc.WithTransportCredentials(insecure.NewCredentials()),
)

Expand All @@ -44,7 +44,7 @@ func NewCompleter(url string, options ...Option) (*Completer, error) {
}

return &Completer{
Config: c,
Config: cfg,
client: NewCompleterClient(client),
}, nil
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/provider/groq/completer.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ type Completer = openai.Completer
func NewCompleter(model string, options ...Option) (*Completer, error) {
url := "https://api.groq.com/openai/v1"

c := &Config{}
cfg := &Config{}

for _, option := range options {
option(c)
option(cfg)
}

return openai.NewCompleter(url, model, c.options...)
return openai.NewCompleter(url, model, cfg.options...)
}
6 changes: 3 additions & 3 deletions pkg/provider/groq/transcriber.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ func NewTranscriber(url, model string, options ...Option) (*Transcriber, error)
url = "https://api.groq.com/openai/v1"
}

c := &Config{}
cfg := &Config{}

for _, option := range options {
option(c)
option(cfg)
}

return openai.NewTranscriber(url, model, c.options...)
return openai.NewTranscriber(url, model, cfg.options...)
}
12 changes: 6 additions & 6 deletions pkg/provider/huggingface/completer.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func NewCompleter(url, model string, options ...Option) (*Completer, error) {
url = strings.TrimRight(url, "/")
url = strings.TrimRight(url, "/v1")

c := &Config{
cfg := &Config{
client: http.DefaultClient,

url: url,
Expand All @@ -27,17 +27,17 @@ func NewCompleter(url, model string, options ...Option) (*Completer, error) {
}

for _, option := range options {
option(c)
option(cfg)
}

ops := []openai.Option{}

if c.client != nil {
ops = append(ops, openai.WithClient(c.client))
if cfg.client != nil {
ops = append(ops, openai.WithClient(cfg.client))
}

if c.token != "" {
ops = append(ops, openai.WithToken(c.token))
if cfg.token != "" {
ops = append(ops, openai.WithToken(cfg.token))
}

return openai.NewCompleter(url+"/v1", model, ops...)
Expand Down
6 changes: 3 additions & 3 deletions pkg/provider/mistralrs/completer.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ func NewCompleter(url, model string, options ...Option) (*Completer, error) {
url = strings.TrimRight(url, "/")
url = strings.TrimSuffix(url, "/v1")

c := &Config{}
cfg := &Config{}

for _, option := range options {
option(c)
option(cfg)
}

return openai.NewCompleter(url+"/v1", model, c.options...)
return openai.NewCompleter(url+"/v1", model, cfg.options...)
}
6 changes: 3 additions & 3 deletions pkg/provider/mistralrs/embedder.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ func NewEmbedder(url, model string, options ...Option) (*Embedder, error) {
url = strings.TrimRight(url, "/")
url = strings.TrimSuffix(url, "/v1")

c := &Config{}
cfg := &Config{}

for _, option := range options {
option(c)
option(cfg)
}

return openai.NewEmbedder(url+"/v1", model, c.options...)
return openai.NewEmbedder(url+"/v1", model, cfg.options...)
}
60 changes: 2 additions & 58 deletions pkg/provider/ollama/completer.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
package ollama

import (
"context"
"strings"

"github.com/adrianliechti/llama/pkg/provider"
"github.com/adrianliechti/llama/pkg/provider/openai"
)

var _ provider.Completer = (*Completer)(nil)

type Completer struct {
completer *openai.Completer
}
type Completer = openai.Completer

func NewCompleter(url, model string, options ...Option) (*Completer, error) {
if url == "" {
Expand All @@ -28,55 +22,5 @@ func NewCompleter(url, model string, options ...Option) (*Completer, error) {
option(cfg)
}

opts := []openai.Option{}

if cfg.client != nil {
opts = append(opts, openai.WithClient(cfg.client))
}

completer, err := openai.NewCompleter(url+"/v1", model, opts...)

if err != nil {
return nil, err
}

return &Completer{
completer: completer,
}, nil
}

func (c *Completer) Complete(ctx context.Context, messages []provider.Message, options *provider.CompleteOptions) (*provider.Completion, error) {
if options == nil {
options = new(provider.CompleteOptions)
}

inputOptions := &provider.CompleteOptions{
Stream: options.Stream,

Stop: options.Stop,
Tools: options.Tools,

MaxTokens: options.MaxTokens,
Temperature: options.Temperature,

Format: options.Format,
}

if len(options.Tools) > 0 {
inputOptions.Stream = nil
}

result, err := c.completer.Complete(ctx, messages, inputOptions)

if err != nil {
return nil, err
}

if inputOptions.Stream == nil && options.Stream != nil {
if err := options.Stream(ctx, *result); err != nil {
return nil, err
}
}

return result, nil
return openai.NewCompleter(url+"/v1", model, cfg.options...)
}
6 changes: 4 additions & 2 deletions pkg/provider/ollama/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@ package ollama

import (
"net/http"

"github.com/adrianliechti/llama/pkg/provider/openai"
)

type Config struct {
client *http.Client
options []openai.Option
}

type Option func(*Config)

func WithClient(client *http.Client) Option {
return func(c *Config) {
c.client = client
c.options = append(c.options, openai.WithClient(client))
}
}
8 changes: 1 addition & 7 deletions pkg/provider/ollama/embedder.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,5 @@ func NewEmbedder(url, model string, options ...Option) (*Embedder, error) {
option(cfg)
}

opts := []openai.Option{}

if cfg.client != nil {
opts = append(opts, openai.WithClient(cfg.client))
}

return openai.NewEmbedder(url+"/v1", model, opts...)
return openai.NewEmbedder(url+"/v1", model, cfg.options...)
}

0 comments on commit 2c69a18

Please sign in to comment.