@@ -11,7 +11,6 @@ import (
1111 "io"
1212 "net/http"
1313 "strings"
14- "sync"
1514 "time"
1615
1716 "github.com/bytedance/sonic"
@@ -31,48 +30,6 @@ type AnthropicProvider struct {
3130 customProviderConfig * schemas.CustomProviderConfig // Custom provider config
3231}
3332
34- // anthropicChatResponsePool provides a pool for Anthropic chat response objects.
35- var anthropicChatResponsePool = sync.Pool {
36- New : func () interface {} {
37- return & anthropic.AnthropicMessageResponse {}
38- },
39- }
40-
41- // anthropicTextResponsePool provides a pool for Anthropic text response objects.
42- var anthropicTextResponsePool = sync.Pool {
43- New : func () interface {} {
44- return & anthropic.AnthropicTextResponse {}
45- },
46- }
47-
48- // acquireAnthropicChatResponse gets an Anthropic chat response from the pool and resets it.
49- func acquireAnthropicChatResponse () * anthropic.AnthropicMessageResponse {
50- resp := anthropicChatResponsePool .Get ().(* anthropic.AnthropicMessageResponse )
51- * resp = anthropic.AnthropicMessageResponse {} // Reset the struct
52- return resp
53- }
54-
55- // releaseAnthropicChatResponse returns an Anthropic chat response to the pool.
56- func releaseAnthropicChatResponse (resp * anthropic.AnthropicMessageResponse ) {
57- if resp != nil {
58- anthropicChatResponsePool .Put (resp )
59- }
60- }
61-
62- // acquireAnthropicTextResponse gets an Anthropic text response from the pool and resets it.
63- func acquireAnthropicTextResponse () * anthropic.AnthropicTextResponse {
64- resp := anthropicTextResponsePool .Get ().(* anthropic.AnthropicTextResponse )
65- * resp = anthropic.AnthropicTextResponse {} // Reset the struct
66- return resp
67- }
68-
69- // releaseAnthropicTextResponse returns an Anthropic text response to the pool.
70- func releaseAnthropicTextResponse (resp * anthropic.AnthropicTextResponse ) {
71- if resp != nil {
72- anthropicTextResponsePool .Put (resp )
73- }
74- }
75-
7633// NewAnthropicProvider creates a new Anthropic provider instance.
7734// It initializes the HTTP client with the provided configuration and sets up response pools.
7835// The client is configured with timeouts, concurrency limits, and optional proxy settings.
@@ -92,8 +49,8 @@ func NewAnthropicProvider(config *schemas.ProviderConfig, logger schemas.Logger)
9249
9350 // Pre-warm response pools
9451 for i := 0 ; i < config .ConcurrencyAndBufferSize .Concurrency ; i ++ {
95- anthropicTextResponsePool . Put (& anthropic.AnthropicTextResponse {})
96- anthropicChatResponsePool . Put (& anthropic.AnthropicMessageResponse {})
52+ anthropic . ReleaseTextResponse (& anthropic.AnthropicTextResponse {})
53+ anthropic . ReleaseChatResponse (& anthropic.AnthropicMessageResponse {})
9754 }
9855
9956 // Configure proxy if provided
@@ -186,6 +143,7 @@ func (provider *AnthropicProvider) TextCompletion(ctx context.Context, key schem
186143 if reqBody == nil {
187144 return nil , newBifrostOperationError ("text completion input is not provided" , nil , provider .GetProviderKey ())
188145 }
146+ defer anthropic .ReleaseTextRequest (reqBody )
189147
190148 // Use struct directly for JSON marshaling
191149 responseBody , latency , err := provider .completeRequest (ctx , reqBody , provider .networkConfig .BaseURL + "/v1/complete" , key .Value )
@@ -194,8 +152,8 @@ func (provider *AnthropicProvider) TextCompletion(ctx context.Context, key schem
194152 }
195153
196154 // Create response object from pool
197- response := acquireAnthropicTextResponse ()
198- defer releaseAnthropicTextResponse (response )
155+ response := anthropic . AcquireTextResponse ()
156+ defer anthropic . ReleaseTextResponse (response )
199157
200158 rawResponse , bifrostErr := handleProviderResponse (responseBody , response , provider .sendBackRawResponse )
201159 if bifrostErr != nil {
@@ -238,6 +196,7 @@ func (provider *AnthropicProvider) ChatCompletion(ctx context.Context, key schem
238196 if reqBody == nil {
239197 return nil , newBifrostOperationError ("chat completion input is not provided" , nil , provider .GetProviderKey ())
240198 }
199+ defer anthropic .ReleaseChatRequest (reqBody )
241200
242201 // Use struct directly for JSON marshaling
243202 responseBody , latency , err := provider .completeRequest (ctx , reqBody , provider .networkConfig .BaseURL + "/v1/messages" , key .Value )
@@ -246,8 +205,8 @@ func (provider *AnthropicProvider) ChatCompletion(ctx context.Context, key schem
246205 }
247206
248207 // Create response object from pool
249- response := acquireAnthropicChatResponse ()
250- defer releaseAnthropicChatResponse (response )
208+ response := anthropic . AcquireChatResponse ()
209+ defer anthropic . ReleaseChatResponse (response )
251210
252211 rawResponse , bifrostErr := handleProviderResponse (responseBody , response , provider .sendBackRawResponse )
253212 if bifrostErr != nil {
@@ -284,6 +243,7 @@ func (provider *AnthropicProvider) Responses(ctx context.Context, key schemas.Ke
284243 if reqBody == nil {
285244 return nil , newBifrostOperationError ("responses input is not provided" , nil , provider .GetProviderKey ())
286245 }
246+ defer anthropic .ReleaseChatRequest (reqBody ) // ToAnthropicResponsesRequest returns *AnthropicMessageRequest
287247
288248 // Use struct directly for JSON marshaling
289249 responseBody , latency , err := provider .completeRequest (ctx , reqBody , provider .networkConfig .BaseURL + "/v1/messages" , key .Value )
@@ -292,8 +252,8 @@ func (provider *AnthropicProvider) Responses(ctx context.Context, key schemas.Ke
292252 }
293253
294254 // Create response object from pool
295- response := acquireAnthropicChatResponse ()
296- defer releaseAnthropicChatResponse (response )
255+ response := anthropic . AcquireChatResponse ()
256+ defer anthropic . ReleaseChatResponse (response )
297257
298258 rawResponse , bifrostErr := handleProviderResponse (responseBody , response , provider .sendBackRawResponse )
299259 if bifrostErr != nil {
@@ -335,6 +295,7 @@ func (provider *AnthropicProvider) ChatCompletionStream(ctx context.Context, pos
335295 if reqBody == nil {
336296 return nil , newBifrostOperationError ("failed to convert request" , fmt .Errorf ("conversion returned nil" ), provider .GetProviderKey ())
337297 }
298+ defer anthropic .ReleaseChatRequest (reqBody )
338299 reqBody .Stream = schemas .Ptr (true )
339300
340301 // Prepare Anthropic headers
0 commit comments