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

Anthropic request headers helper #233

Merged
merged 5 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
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