on main help sign in
chore: add extra body & header to openai and anthropic
a3f6f0fKujtim Hoxha
internal/config/config.go+2
@@ -75,6 +75,8 @@ type ProviderConfig struct {
75
75
76 // Extra headers to send with each request to the provider.
76 // Extra headers to send with each request to the provider.
77 ExtraHeaders map[string]string `json:"extra_headers,omitempty"`
77 ExtraHeaders map[string]string `json:"extra_headers,omitempty"`
78+ // Extra body
79+ ExtraBody map[string]any `json:"extra_body,omitempty"`
78
80
79 // Used to pass extra parameters to the provider.
81 // Used to pass extra parameters to the provider.
80 ExtraParams map[string]string `json:"-"`
82 ExtraParams map[string]string `json:"-"`
internal/config/load.go+1
@@ -141,6 +141,7 @@ func (c *Config) configureProviders(env env.Env, resolver VariableResolver, know
141 Type: p.Type,
141 Type: p.Type,
142 Disable: config.Disable,
142 Disable: config.Disable,
143 ExtraHeaders: config.ExtraHeaders,
143 ExtraHeaders: config.ExtraHeaders,
144+ ExtraBody: config.ExtraBody,
144 ExtraParams: make(map[string]string),
145 ExtraParams: make(map[string]string),
145 Models: p.Models,
146 Models: p.Models,
146 }
147 }
internal/llm/provider/anthropic.go+6
@@ -45,6 +45,12 @@ func createAnthropicClient(opts providerClientOptions, useBedrock bool) anthropi
45 if useBedrock {
45 if useBedrock {
46 anthropicClientOptions = append(anthropicClientOptions, bedrock.WithLoadDefaultConfig(context.Background()))
46 anthropicClientOptions = append(anthropicClientOptions, bedrock.WithLoadDefaultConfig(context.Background()))
47 }
47 }
48+ for _, header := range opts.extraHeaders {
49+ anthropicClientOptions = append(anthropicClientOptions, option.WithHeaderAdd(header, opts.extraHeaders[header]))
50+ }
51+ for key, value := range opts.extraBody {
52+ anthropicClientOptions = append(anthropicClientOptions, option.WithJSONSet(key, value))
53+ }
48 return anthropic.NewClient(anthropicClientOptions...)
54 return anthropic.NewClient(anthropicClientOptions...)
49}
55}
50
56
internal/llm/provider/openai.go+6−4
@@ -44,10 +44,12 @@ func createOpenAIClient(opts providerClientOptions) openai.Client {
44 }
44 }
45 }
45 }
46
46
47− if opts.extraHeaders != nil {
48− for key, value := range opts.extraHeaders {
47+ for key, value := range opts.extraHeaders {
49− openaiClientOptions = append(openaiClientOptions, option.WithHeader(key, value))
48+ openaiClientOptions = append(openaiClientOptions, option.WithHeader(key, value))
50− }
49+ }
50+
51+ for extraKey, extraValue := range opts.extraBody {
52+ openaiClientOptions = append(openaiClientOptions, option.WithJSONSet(extraKey, extraValue))
51 }
53 }
52
54
53 return openai.NewClient(openaiClientOptions...)
55 return openai.NewClient(openaiClientOptions...)
internal/llm/provider/provider.go+2
@@ -70,6 +70,7 @@ type providerClientOptions struct {
70 systemMessage string
70 systemMessage string
71 maxTokens int64
71 maxTokens int64
72 extraHeaders map[string]string
72 extraHeaders map[string]string
73+ extraBody map[string]any
73 extraParams map[string]string
74 extraParams map[string]string
74}
75}
75
76
@@ -147,6 +148,7 @@ func NewProvider(cfg config.ProviderConfig, opts ...ProviderClientOption) (Provi
147 config: cfg,
148 config: cfg,
148 apiKey: resolvedAPIKey,
149 apiKey: resolvedAPIKey,
149 extraHeaders: cfg.ExtraHeaders,
150 extraHeaders: cfg.ExtraHeaders,
151+ extraBody: cfg.ExtraBody,
150 model: func(tp config.SelectedModelType) provider.Model {
152 model: func(tp config.SelectedModelType) provider.Model {
151 return *config.Get().GetModelByType(tp)
153 return *config.Get().GetModelByType(tp)
152 },
154 },