Skip to content

feat: support models by OpenAI API using "name from url" syntax #188

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 37 additions & 4 deletions pkg/remote/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package remote
import (
"context"
"fmt"
"net/url"
"os"
"slices"
"sort"
"strings"
Expand Down Expand Up @@ -87,6 +89,28 @@ func (c *Client) Supports(ctx context.Context, modelName string) (bool, error) {
return true, nil
}

func isHTTPURL(toolName string) bool {
return strings.HasPrefix(toolName, "http://") ||
strings.HasPrefix(toolName, "https://")
}

func (c *Client) clientFromURL(apiURL string) (*openai.Client, error) {
parsed, err := url.Parse(apiURL)
if err != nil {
return nil, err
}
env := strings.ToUpper(strings.ReplaceAll(parsed.Hostname(), ".", "_")) + "_API_KEY"
apiKey := os.Getenv(env)
if apiKey == "" {
apiKey = "<unset>"
}
return openai.NewClient(openai.Options{
BaseURL: apiURL,
Cache: c.cache,
APIKey: apiKey,
})
}

func (c *Client) load(ctx context.Context, toolName string) (*openai.Client, error) {
c.clientsLock.Lock()
defer c.clientsLock.Unlock()
Expand All @@ -96,6 +120,19 @@ func (c *Client) load(ctx context.Context, toolName string) (*openai.Client, err
return client, nil
}

if c.clients == nil {
c.clients = make(map[string]*openai.Client)
}

if isHTTPURL(toolName) {
remoteClient, err := c.clientFromURL(toolName)
if err != nil {
return nil, err
}
c.clients[toolName] = remoteClient
return remoteClient, nil
}

prg, err := loader.Program(ctx, toolName, "")
if err != nil {
return nil, err
Expand All @@ -120,10 +157,6 @@ func (c *Client) load(ctx context.Context, toolName string) (*openai.Client, err
return nil, err
}

if c.clients == nil {
c.clients = make(map[string]*openai.Client)
}

c.clients[toolName] = client
return client, nil
}