@@ -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 , 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 {
@@ -237,6 +195,7 @@ func (provider *AnthropicProvider) ChatCompletion(ctx context.Context, key schem
237195 if reqBody == nil {
238196 return nil , newBifrostOperationError ("chat completion input is not provided" , nil , provider .GetProviderKey ())
239197 }
198+ defer anthropic .ReleaseChatRequest (reqBody )
240199
241200 // Use struct directly for JSON marshaling
242201 responseBody , err := provider .completeRequest (ctx , reqBody , provider .networkConfig .BaseURL + "/v1/messages" , key .Value )
@@ -245,8 +204,8 @@ func (provider *AnthropicProvider) ChatCompletion(ctx context.Context, key schem
245204 }
246205
247206 // Create response object from pool
248- response := acquireAnthropicChatResponse ()
249- defer releaseAnthropicChatResponse (response )
207+ response := anthropic . AcquireChatResponse ()
208+ defer anthropic . ReleaseChatResponse (response )
250209
251210 rawResponse , bifrostErr := handleProviderResponse (responseBody , response , provider .sendBackRawResponse )
252211 if bifrostErr != nil {
@@ -282,6 +241,7 @@ func (provider *AnthropicProvider) Responses(ctx context.Context, key schemas.Ke
282241 if reqBody == nil {
283242 return nil , newBifrostOperationError ("responses input is not provided" , nil , provider .GetProviderKey ())
284243 }
244+ defer anthropic .ReleaseChatRequest (reqBody ) // ToAnthropicResponsesRequest returns *AnthropicMessageRequest
285245
286246 // Use struct directly for JSON marshaling
287247 responseBody , err := provider .completeRequest (ctx , reqBody , provider .networkConfig .BaseURL + "/v1/messages" , key .Value )
@@ -290,8 +250,8 @@ func (provider *AnthropicProvider) Responses(ctx context.Context, key schemas.Ke
290250 }
291251
292252 // Create response object from pool
293- response := acquireAnthropicChatResponse ()
294- defer releaseAnthropicChatResponse (response )
253+ response := anthropic . AcquireChatResponse ()
254+ defer anthropic . ReleaseChatResponse (response )
295255
296256 rawResponse , bifrostErr := handleProviderResponse (responseBody , response , provider .sendBackRawResponse )
297257 if bifrostErr != nil {
@@ -332,6 +292,7 @@ func (provider *AnthropicProvider) ChatCompletionStream(ctx context.Context, pos
332292 if reqBody == nil {
333293 return nil , newBifrostOperationError ("failed to convert request" , fmt .Errorf ("conversion returned nil" ), provider .GetProviderKey ())
334294 }
295+ defer anthropic .ReleaseChatRequest (reqBody )
335296 reqBody .Stream = schemas .Ptr (true )
336297
337298 // Prepare Anthropic headers
0 commit comments