Skip to content
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

ollama: add WithHttpClient option #505

Merged
merged 3 commits into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
17 changes: 10 additions & 7 deletions llms/ollama/internal/ollamaclient/ollamaclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func checkError(resp *http.Response, body []byte) error {
return apiError
}

func NewClient(ourl *url.URL) (*Client, error) {
func NewClient(ourl *url.URL, ohttp *http.Client) (*Client, error) {
if ourl == nil {
scheme, hostport, ok := strings.Cut(os.Getenv("OLLAMA_HOST"), "://")
if !ok {
Expand All @@ -57,14 +57,17 @@ func NewClient(ourl *url.URL) (*Client, error) {
}
}

client := Client{
base: ourl,
if ohttp == nil {
ohttp = &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
},
}
}

client.http = http.Client{
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
},
client := Client{
base: ourl,
http: *ohttp,
}

return &client, nil
Expand Down
2 changes: 1 addition & 1 deletion llms/ollama/ollamallm.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func New(opts ...Option) (*LLM, error) {
opt(&o)
}

client, err := ollamaclient.NewClient(o.ollamaServerURL)
client, err := ollamaclient.NewClient(o.ollamaServerURL, o.httpClient)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion llms/ollama/ollamallm_chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func NewChat(opts ...Option) (*Chat, error) {
opt(&o)
}

client, err := ollamaclient.NewClient(o.ollamaServerURL)
client, err := ollamaclient.NewClient(o.ollamaServerURL, o.httpClient)
if err != nil {
return nil, err
}
Expand Down
9 changes: 9 additions & 0 deletions llms/ollama/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ package ollama

import (
"log"
"net/http"
"net/url"

"github.com/tmc/langchaingo/llms/ollama/internal/ollamaclient"
)

type options struct {
ollamaServerURL *url.URL
httpClient *http.Client
model string
ollamaOptions ollamaclient.Options
customModelTemplate string
Expand Down Expand Up @@ -52,6 +54,13 @@ func WithServerURL(rawURL string) Option {
}
}

// WithHTTPClient Set custom http client.
func WithHTTPClient(client *http.Client) Option {
return func(opts *options) {
opts.httpClient = client
}
}

// WithBackendUseNUMA Use NUMA optimization on certain systems.
func WithRunnerUseNUMA(numa bool) Option {
return func(opts *options) {
Expand Down
Loading