Skip to content

Commit 941ccab

Browse files
authored
Merge pull request #1473 from roumy/add_authent_ollama
add authentification for ollama instance
2 parents 57cd563 + 1291b35 commit 941ccab

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

plugins/ai/ollama/ollama.go

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import (
1515
"github.com/danielmiessler/fabric/plugins"
1616
)
1717

18+
const defaultBaseUrl = "http://localhost:11434"
19+
1820
func NewClient() (ret *Client) {
1921
vendorName := "Ollama"
2022
ret = &Client{}
@@ -26,26 +28,41 @@ func NewClient() (ret *Client) {
2628
}
2729

2830
ret.ApiUrl = ret.AddSetupQuestionCustom("API URL", true,
29-
"Enter your Ollama URL (as a reminder, it is usually http://localhost:1234/v1')")
31+
"Enter your Ollama URL (as a reminder, it is usually http://localhost:11434')")
32+
ret.ApiUrl.Value = defaultBaseUrl
33+
ret.ApiKey = ret.PluginBase.AddSetupQuestion("API key", false)
34+
ret.ApiKey.Value = ""
3035

3136
return
3237
}
3338

3439
type Client struct {
3540
*plugins.PluginBase
3641
ApiUrl *plugins.SetupQuestion
37-
42+
ApiKey *plugins.SetupQuestion
3843
apiUrl *url.URL
3944
client *ollamaapi.Client
4045
}
4146

47+
type transport_sec struct {
48+
underlyingTransport http.RoundTripper
49+
ApiKey *plugins.SetupQuestion
50+
}
51+
52+
func (t *transport_sec) RoundTrip(req *http.Request) (*http.Response, error) {
53+
if t.ApiKey.Value != "" {
54+
req.Header.Add("Authorization", "Bearer "+t.ApiKey.Value)
55+
}
56+
return t.underlyingTransport.RoundTrip(req)
57+
}
58+
4259
func (o *Client) configure() (err error) {
4360
if o.apiUrl, err = url.Parse(o.ApiUrl.Value); err != nil {
4461
fmt.Printf("cannot parse URL: %s: %v\n", o.ApiUrl.Value, err)
4562
return
4663
}
4764

48-
o.client = ollamaapi.NewClient(o.apiUrl, &http.Client{Timeout: 1200000 * time.Millisecond})
65+
o.client = ollamaapi.NewClient(o.apiUrl, &http.Client{Timeout: 1200000 * time.Millisecond, Transport: &transport_sec{underlyingTransport: http.DefaultTransport, ApiKey: o.ApiKey}})
4966
return
5067
}
5168

0 commit comments

Comments
 (0)