parse protocol for OLLAMA_HOST

This commit is contained in:
Jeffrey Morgan 2023-08-17 18:20:38 -04:00
parent cabaada956
commit 54bb49a502

View file

@ -10,9 +10,10 @@ import (
"net/http" "net/http"
"net/url" "net/url"
"os" "os"
"strings"
) )
const DefaultHost = "http://localhost:11434" const DefaultHost = "localhost:11434"
var ( var (
envHost = os.Getenv("OLLAMA_HOST") envHost = os.Getenv("OLLAMA_HOST")
@ -53,23 +54,21 @@ func Host() string {
// FromEnv creates a new client using Host() as the host. An error is returns // FromEnv creates a new client using Host() as the host. An error is returns
// if the host is invalid. // if the host is invalid.
func FromEnv() (*Client, error) { func FromEnv() (*Client, error) {
u, err := url.Parse(Host()) h := Host()
if !strings.HasPrefix(h, "http://") && !strings.HasPrefix(h, "https://") {
h = "http://" + h
}
u, err := url.Parse(h)
if err != nil { if err != nil {
return nil, err return nil, fmt.Errorf("could not parse host: %w", err)
}
return &Client{Base: *u}, nil
}
func NewClient(hosts ...string) *Client {
host := DefaultHost
if len(hosts) > 0 {
host = hosts[0]
} }
return &Client{ if u.Port() == "" {
Base: url.URL{Scheme: "http", Host: host}, u.Host += ":11434"
HTTP: http.Client{},
} }
return &Client{Base: *u, HTTP: http.Client{}}, nil
} }
func (c *Client) do(ctx context.Context, method, path string, reqData, respData any) error { func (c *Client) do(ctx context.Context, method, path string, reqData, respData any) error {