Skip to content

Commit

Permalink
llms/anthropic: Implement WithBaseURL and WithHTTPClient for Anthropi…
Browse files Browse the repository at this point in the history
…c LLM (#671)

* implement WithBaseUrl and WithHttpClient for anthropic model

* fix build errors

* URL not url

* fix more errors
  • Loading branch information
GRVYDEV authored Mar 13, 2024
1 parent ca2969c commit 6eaa82c
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 9 deletions.
7 changes: 5 additions & 2 deletions llms/anthropic/anthropicllm.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package anthropic
import (
"context"
"errors"
"net/http"
"os"

"github.com/tmc/langchaingo/callbacks"
Expand Down Expand Up @@ -34,7 +35,9 @@ func New(opts ...Option) (*LLM, error) {

func newClient(opts ...Option) (*anthropicclient.Client, error) {
options := &options{
token: os.Getenv(tokenEnvVarName),
token: os.Getenv(tokenEnvVarName),
baseURL: anthropicclient.DefaultBaseURL,
httpClient: http.DefaultClient,
}

for _, opt := range opts {
Expand All @@ -45,7 +48,7 @@ func newClient(opts ...Option) (*anthropicclient.Client, error) {
return nil, ErrMissingToken
}

return anthropicclient.New(options.token, options.model)
return anthropicclient.New(options.token, options.model, options.baseURL, options.httpClient)
}

// Call requests a completion for the given prompt.
Expand Down
26 changes: 24 additions & 2 deletions llms/anthropic/anthropicllm_option.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package anthropic

import (
"github.com/tmc/langchaingo/llms/anthropic/internal/anthropicclient"
)

const (
tokenEnvVarName = "ANTHROPIC_API_KEY" //nolint:gosec
)

type options struct {
token string
model string
token string
model string
baseURL string
httpClient anthropicclient.Doer
}

type Option func(*options)
Expand All @@ -25,3 +31,19 @@ func WithModel(model string) Option {
opts.model = model
}
}

// WithBaseUrl passes the Anthropic base URL to the client.
// If not set, the default base URL is used.
func WithBaseURL(baseURL string) Option {
return func(opts *options) {
opts.baseURL = baseURL
}
}

// WithHTTPClient allows setting a custom HTTP client. If not set, the default value
// is http.DefaultClient.
func WithHTTPClient(client anthropicclient.Doer) Option {
return func(opts *options) {
opts.httpClient = client
}
}
9 changes: 5 additions & 4 deletions llms/anthropic/internal/anthropicclient/anthropicclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import (
"context"
"errors"
"net/http"
"strings"
)

const (
defaultBaseURL = "https://api.anthropic.com/v1"
DefaultBaseURL = "https://api.anthropic.com/v1"
)

// ErrEmptyResponse is returned when the Anthropic API returns an empty response.
Expand Down Expand Up @@ -40,12 +41,12 @@ func WithHTTPClient(client Doer) Option {
}

// New returns a new Anthropic client.
func New(token string, model string, opts ...Option) (*Client, error) {
func New(token string, model string, baseURL string, httpClient Doer, opts ...Option) (*Client, error) {
c := &Client{
Model: model,
token: token,
baseURL: defaultBaseURL,
httpClient: http.DefaultClient,
baseURL: strings.TrimSuffix(baseURL, "/"),
httpClient: httpClient,
}

for _, opt := range opts {
Expand Down
2 changes: 1 addition & 1 deletion llms/anthropic/internal/anthropicclient/completions.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func (c *Client) createCompletion(ctx context.Context, payload *completionPayloa
}

if c.baseURL == "" {
c.baseURL = defaultBaseURL
c.baseURL = DefaultBaseURL
}

url := fmt.Sprintf("%s/complete", c.baseURL)
Expand Down

0 comments on commit 6eaa82c

Please sign in to comment.