@@ -22,6 +22,7 @@ import (
2222
2323 "github.com/sourcegraph/sourcegraph/internal/completions/tokenusage"
2424 "github.com/sourcegraph/sourcegraph/internal/completions/types"
25+ "github.com/sourcegraph/sourcegraph/internal/conf/conftypes"
2526 "github.com/sourcegraph/sourcegraph/internal/httpcli"
2627 "github.com/sourcegraph/sourcegraph/lib/errors"
2728)
@@ -68,7 +69,8 @@ func (c *awsBedrockAnthropicCompletionStreamClient) Complete(
6869 completion += content .Text
6970 }
7071
71- err = c .tokenManager .UpdateTokenCountsFromModelUsage (response .Usage .InputTokens , response .Usage .OutputTokens , "anthropic/" + requestParams .Model , string (feature ), tokenusage .AwsBedrock )
72+ parsedModelId := conftypes .NewBedrockModelRefFromModelID (requestParams .Model )
73+ err = c .tokenManager .UpdateTokenCountsFromModelUsage (response .Usage .InputTokens , response .Usage .OutputTokens , "anthropic/" + parsedModelId .Model , string (feature ), tokenusage .AwsBedrock )
7274 if err != nil {
7375 return nil , err
7476 }
@@ -153,7 +155,8 @@ func (a *awsBedrockAnthropicCompletionStreamClient) Stream(
153155 case "message_delta" :
154156 if event .Delta != nil {
155157 stopReason = event .Delta .StopReason
156- err = a .tokenManager .UpdateTokenCountsFromModelUsage (inputPromptTokens , event .Usage .OutputTokens , "anthropic/" + requestParams .Model , string (feature ), tokenusage .AwsBedrock )
158+ parsedModelId := conftypes .NewBedrockModelRefFromModelID (requestParams .Model )
159+ err = a .tokenManager .UpdateTokenCountsFromModelUsage (inputPromptTokens , event .Usage .OutputTokens , "anthropic/" + parsedModelId .Model , string (feature ), tokenusage .AwsBedrock )
157160 if err != nil {
158161 logger .Warn ("Failed to count tokens with the token manager %w " , log .Error (err ))
159162 }
@@ -232,19 +235,8 @@ func (c *awsBedrockAnthropicCompletionStreamClient) makeRequest(ctx context.Cont
232235 if err != nil {
233236 return nil , errors .Wrap (err , "marshalling request body" )
234237 }
235- apiURL , err := url .Parse (c .endpoint )
236- if err != nil || apiURL .Scheme == "" {
237- apiURL = & url.URL {
238- Scheme : "https" ,
239- Host : fmt .Sprintf ("bedrock-runtime.%s.amazonaws.com" , defaultConfig .Region ),
240- }
241- }
242238
243- if stream {
244- apiURL .Path = fmt .Sprintf ("/model/%s/invoke-with-response-stream" , requestParams .Model )
245- } else {
246- apiURL .Path = fmt .Sprintf ("/model/%s/invoke" , requestParams .Model )
247- }
239+ apiURL := buildApiUrl (c .endpoint , requestParams .Model , stream , defaultConfig .Region )
248240
249241 req , err := http .NewRequestWithContext (ctx , http .MethodPost , apiURL .String (), bytes .NewReader (reqBody ))
250242 if err != nil {
@@ -282,6 +274,41 @@ func (c *awsBedrockAnthropicCompletionStreamClient) makeRequest(ctx context.Cont
282274 return resp , nil
283275}
284276
277+ // Builds a bedrock api URL from the configured endpoint url.
278+ // If the endpoint isn't valid, falls back to the default endpoint for the specified fallbackRegion
279+ func buildApiUrl (endpoint string , model string , stream bool , fallbackRegion string ) * url.URL {
280+ apiURL , err := url .Parse (endpoint )
281+ if err != nil || apiURL .Scheme == "" {
282+ apiURL = & url.URL {
283+ Scheme : "https" ,
284+ Host : fmt .Sprintf ("bedrock-runtime.%s.amazonaws.com" , fallbackRegion ),
285+ }
286+ }
287+
288+ bedrockModelRef := conftypes .NewBedrockModelRefFromModelID (model )
289+
290+ if bedrockModelRef .ProvisionedCapacity != nil {
291+ // We need to Query escape the provisioned capacity ARN, since otherwise
292+ // the AWS API Gateway interprets the path as a path and doesn't route
293+ // to the Bedrock service. This would results in abstract Coral errors
294+ if stream {
295+ apiURL .RawPath = fmt .Sprintf ("/model/%s/invoke-with-response-stream" , url .QueryEscape (* bedrockModelRef .ProvisionedCapacity ))
296+ apiURL .Path = fmt .Sprintf ("/model/%s/invoke-with-response-stream" , * bedrockModelRef .ProvisionedCapacity )
297+ } else {
298+ apiURL .RawPath = fmt .Sprintf ("/model/%s/invoke" , url .QueryEscape (* bedrockModelRef .ProvisionedCapacity ))
299+ apiURL .Path = fmt .Sprintf ("/model/%s/invoke" , * bedrockModelRef .ProvisionedCapacity )
300+ }
301+ } else {
302+ if stream {
303+ apiURL .Path = fmt .Sprintf ("/model/%s/invoke-with-response-stream" , bedrockModelRef .Model )
304+ } else {
305+ apiURL .Path = fmt .Sprintf ("/model/%s/invoke" , bedrockModelRef .Model )
306+ }
307+ }
308+
309+ return apiURL
310+ }
311+
285312func awsConfigOptsForKeyConfig (endpoint string , accessToken string ) []func (* config.LoadOptions ) error {
286313 configOpts := []func (* config.LoadOptions ) error {}
287314 if endpoint != "" {
0 commit comments