Skip to content

Commit

Permalink
Anthropic request headers helper (#233)
Browse files Browse the repository at this point in the history
* Add helper function for preparing common request headers

* Rename variable to not collide with imported package

* Get rid of unnecessary variable colliding with imported package

* Enhancements to Anthropic setRequestHeaders()

* PR feedback
  • Loading branch information
enzowritescode committed Aug 21, 2024
1 parent bd2c2c3 commit 3ecb618
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 22 deletions.
22 changes: 13 additions & 9 deletions server/ai/anthropic/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (

const (
MessageEndpoint = "https://api.anthropic.com/v1/messages"
APIKeyHeader = "X-API-Key" //nolint:gosec

StopReasonStopSequence = "stop_sequence"
StopReasonMaxTokens = "max_tokens"
Expand Down Expand Up @@ -97,9 +96,7 @@ func (c *Client) MessageCompletionNoStream(completionRequest MessageRequest) (st
return "", fmt.Errorf("could not create request: %w", err)
}

req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-API-Key", c.apiKey)
req.Header.Set("anthropic-version", "2023-06-01")
c.setRequestHeaders(req, false)

resp, err := c.httpClient.Do(req)
if err != nil {
Expand Down Expand Up @@ -135,11 +132,7 @@ func (c *Client) MessageCompletion(completionRequest MessageRequest) (*ai.TextSt
return nil, err
}

req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-API-Key", c.apiKey)
req.Header.Set("Accept", "text/event-stream")
req.Header.Set("Connection", "keep-alive")
req.Header.Set("anthropic-version", "2023-06-01")
c.setRequestHeaders(req, true)

output := make(chan string)
errChan := make(chan error)
Expand Down Expand Up @@ -203,3 +196,14 @@ func (c *Client) MessageCompletion(completionRequest MessageRequest) (*ai.TextSt

return &ai.TextStreamResult{Stream: output, Err: errChan}, nil
}

func (c *Client) setRequestHeaders(req *http.Request, isStreaming bool) {
req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-API-Key", c.apiKey)
req.Header.Set("anthropic-version", "2023-06-01")

if isStreaming {
req.Header.Set("Accept", "text/event-stream")
req.Header.Set("Connection", "keep-alive")
}
}
10 changes: 5 additions & 5 deletions server/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ func (p *Plugin) GetMetrics() metrics.Metrics {
}

func (p *Plugin) metricsMiddleware(c *gin.Context) {
metrics := p.GetMetrics()
if metrics == nil {
llmMetrics := p.GetMetrics()
if llmMetrics == nil {
c.Next()
return
}
p.GetMetrics().IncrementHTTPRequests()
llmMetrics.IncrementHTTPRequests()
now := time.Now()

c.Next()
Expand All @@ -34,9 +34,9 @@ func (p *Plugin) metricsMiddleware(c *gin.Context) {
status := c.Writer.Status()

if status < 200 || status > 299 {
p.GetMetrics().IncrementHTTPErrors()
llmMetrics.IncrementHTTPErrors()
}

endpoint := c.HandlerName()
p.GetMetrics().ObserveAPIEndpointDuration(endpoint, c.Request.Method, strconv.Itoa(status), elapsed)
llmMetrics.ObserveAPIEndpointDuration(endpoint, c.Request.Method, strconv.Itoa(status), elapsed)
}
16 changes: 8 additions & 8 deletions server/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,18 +139,18 @@ func (p *Plugin) OnDeactivate() error {
}

func (p *Plugin) getLLM(llmBotConfig ai.BotConfig) ai.LanguageModel {
metrics := p.metricsService.GetMetricsForAIService(llmBotConfig.Name)
llmMetrics := p.metricsService.GetMetricsForAIService(llmBotConfig.Name)

var llm ai.LanguageModel
switch llmBotConfig.Service.Type {
case "openai":
llm = openai.New(llmBotConfig.Service, metrics)
llm = openai.New(llmBotConfig.Service, llmMetrics)
case "openaicompatible":
llm = openai.NewCompatible(llmBotConfig.Service, metrics)
llm = openai.NewCompatible(llmBotConfig.Service, llmMetrics)
case "anthropic":
llm = anthropic.New(llmBotConfig.Service, metrics)
llm = anthropic.New(llmBotConfig.Service, llmMetrics)
case "asksage":
llm = asksage.New(llmBotConfig.Service, metrics)
llm = asksage.New(llmBotConfig.Service, llmMetrics)
}

cfg := p.getConfiguration()
Expand All @@ -172,12 +172,12 @@ func (p *Plugin) getTranscribe() ai.Transcriber {
break
}
}
metrics := p.metricsService.GetMetricsForAIService(botConfig.Name)
llmMetrics := p.metricsService.GetMetricsForAIService(botConfig.Name)
switch botConfig.Service.Type {
case "openai":
return openai.New(botConfig.Service, metrics)
return openai.New(botConfig.Service, llmMetrics)
case "openaicompatible":
return openai.NewCompatible(botConfig.Service, metrics)
return openai.NewCompatible(botConfig.Service, llmMetrics)
}
return nil
}
Expand Down

0 comments on commit 3ecb618

Please sign in to comment.