Skip to content

Commit 2cbc913

Browse files
feat: anthropic integration added in http transport
1 parent 9bc0828 commit 2cbc913

File tree

4 files changed

+444
-0
lines changed

4 files changed

+444
-0
lines changed

core/providers/anthropic.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,8 @@ func prepareAnthropicChatRequest(messages []schemas.BifrostMessage, params *sche
483483
if params != nil && params.ToolChoice != nil {
484484
switch toolChoice := params.ToolChoice.Type; toolChoice {
485485
case schemas.ToolChoiceTypeFunction:
486+
fallthrough
487+
case "tool":
486488
preparedParams["tool_choice"] = map[string]interface{}{
487489
"type": "tool",
488490
"name": params.ToolChoice.Function.Name,
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package anthropic
2+
3+
import (
4+
"encoding/json"
5+
6+
"github.com/fasthttp/router"
7+
bifrost "github.com/maximhq/bifrost/core"
8+
"github.com/maximhq/bifrost/transports/bifrost-http/lib"
9+
"github.com/valyala/fasthttp"
10+
)
11+
12+
// AnthropicRouter holds route registrations for Anthropic endpoints.
13+
type AnthropicRouter struct {
14+
client *bifrost.Bifrost
15+
}
16+
17+
// NewAnthropicRouter creates a new AnthropicRouter with the given bifrost client.
18+
func NewAnthropicRouter(client *bifrost.Bifrost) *AnthropicRouter {
19+
return &AnthropicRouter{client: client}
20+
}
21+
22+
// RegisterRoutes registers all Anthropic routes on the given router.
23+
func (a *AnthropicRouter) RegisterRoutes(r *router.Router) {
24+
r.POST("/anthropic/v1/messages", a.handleMessages)
25+
}
26+
27+
// handleMessages handles POST /v1/messages
28+
func (a *AnthropicRouter) handleMessages(ctx *fasthttp.RequestCtx) {
29+
var req AnthropicMessageRequest
30+
if err := json.Unmarshal(ctx.PostBody(), &req); err != nil {
31+
ctx.SetStatusCode(fasthttp.StatusBadRequest)
32+
ctx.SetContentType("application/json")
33+
errResponse := map[string]string{"error": err.Error()}
34+
jsonBytes, _ := json.Marshal(errResponse)
35+
ctx.SetBody(jsonBytes)
36+
return
37+
}
38+
39+
if req.Model == "" {
40+
ctx.SetStatusCode(fasthttp.StatusBadRequest)
41+
ctx.SetBodyString("Model parameter is required")
42+
return
43+
}
44+
45+
bifrostReq := req.ConvertToBifrostRequest()
46+
47+
bifrostCtx := lib.ConvertToBifrostContext(ctx)
48+
49+
result, err := a.client.ChatCompletionRequest(*bifrostCtx, bifrostReq)
50+
if err != nil {
51+
ctx.SetStatusCode(fasthttp.StatusInternalServerError)
52+
ctx.SetContentType("application/json")
53+
jsonBytes, _ := json.Marshal(err)
54+
ctx.SetBody(jsonBytes)
55+
return
56+
}
57+
58+
anthropicResponse := DeriveAnthropicFromBifrostResponse(result)
59+
ctx.SetStatusCode(fasthttp.StatusOK)
60+
ctx.SetContentType("application/json")
61+
jsonBytes, _ := json.Marshal(anthropicResponse)
62+
ctx.SetBody(jsonBytes)
63+
}

0 commit comments

Comments
 (0)