Skip to content

Commit b66246f

Browse files
Merge pull request #188 from ibuildthecloud/openai-remote
feat: support models by OpenAI API using "name from url" syntax
2 parents 941240c + 34ee1d2 commit b66246f

File tree

1 file changed

+37
-4
lines changed

1 file changed

+37
-4
lines changed

pkg/remote/remote.go

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package remote
33
import (
44
"context"
55
"fmt"
6+
"net/url"
7+
"os"
68
"slices"
79
"sort"
810
"strings"
@@ -87,6 +89,28 @@ func (c *Client) Supports(ctx context.Context, modelName string) (bool, error) {
8789
return true, nil
8890
}
8991

92+
func isHTTPURL(toolName string) bool {
93+
return strings.HasPrefix(toolName, "http://") ||
94+
strings.HasPrefix(toolName, "https://")
95+
}
96+
97+
func (c *Client) clientFromURL(apiURL string) (*openai.Client, error) {
98+
parsed, err := url.Parse(apiURL)
99+
if err != nil {
100+
return nil, err
101+
}
102+
env := strings.ToUpper(strings.ReplaceAll(parsed.Hostname(), ".", "_")) + "_API_KEY"
103+
apiKey := os.Getenv(env)
104+
if apiKey == "" {
105+
apiKey = "<unset>"
106+
}
107+
return openai.NewClient(openai.Options{
108+
BaseURL: apiURL,
109+
Cache: c.cache,
110+
APIKey: apiKey,
111+
})
112+
}
113+
90114
func (c *Client) load(ctx context.Context, toolName string) (*openai.Client, error) {
91115
c.clientsLock.Lock()
92116
defer c.clientsLock.Unlock()
@@ -96,6 +120,19 @@ func (c *Client) load(ctx context.Context, toolName string) (*openai.Client, err
96120
return client, nil
97121
}
98122

123+
if c.clients == nil {
124+
c.clients = make(map[string]*openai.Client)
125+
}
126+
127+
if isHTTPURL(toolName) {
128+
remoteClient, err := c.clientFromURL(toolName)
129+
if err != nil {
130+
return nil, err
131+
}
132+
c.clients[toolName] = remoteClient
133+
return remoteClient, nil
134+
}
135+
99136
prg, err := loader.Program(ctx, toolName, "")
100137
if err != nil {
101138
return nil, err
@@ -120,10 +157,6 @@ func (c *Client) load(ctx context.Context, toolName string) (*openai.Client, err
120157
return nil, err
121158
}
122159

123-
if c.clients == nil {
124-
c.clients = make(map[string]*openai.Client)
125-
}
126-
127160
c.clients[toolName] = client
128161
return client, nil
129162
}

0 commit comments

Comments
 (0)