|
| 1 | +package completions |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/stretchr/testify/assert" |
| 8 | + |
| 9 | + "github.com/sourcegraph/sourcegraph/internal/completions/types" |
| 10 | + "github.com/sourcegraph/sourcegraph/lib/pointers" |
| 11 | + "github.com/sourcegraph/sourcegraph/schema" |
| 12 | +) |
| 13 | + |
| 14 | +func testAPIProviderAnthropic(t *testing.T, infra *apiProviderTestInfra) { |
| 15 | + // validAnthropicRequestData bundles the messages sent between major |
| 16 | + // components of the Completions API. |
| 17 | + type validAnthropicRequestData struct { |
| 18 | + // InitialCompletionRequest is the request sent by the user to the |
| 19 | + // Sourcegraph instance. |
| 20 | + InitialCompletionRequest types.CodyCompletionRequestParameters |
| 21 | + |
| 22 | + // OutboundAnthropicRequest is the data sent from this Sourcegraph |
| 23 | + // instance to the API Provider (e.g. Anthropic, Cody Gateway, AWS |
| 24 | + // Bedrock, etc.) |
| 25 | + OutboundAnthropicRequest map[string]any |
| 26 | + |
| 27 | + // InboundAnthropicRequest is the response from the API Provider |
| 28 | + // to the Sourcegraph instance. |
| 29 | + InboundAnthropicRequest map[string]any |
| 30 | + } |
| 31 | + |
| 32 | + // getValidTestData returns a valid set of request data. |
| 33 | + getValidTestData := func() validAnthropicRequestData { |
| 34 | + initialCompletionRequest := types.CodyCompletionRequestParameters{ |
| 35 | + CompletionRequestParameters: types.CompletionRequestParameters{ |
| 36 | + Messages: []types.Message{ |
| 37 | + { |
| 38 | + Speaker: "human", |
| 39 | + Text: "please make this code better", |
| 40 | + }, |
| 41 | + }, |
| 42 | + Stream: pointers.Ptr(false), |
| 43 | + }, |
| 44 | + } |
| 45 | + |
| 46 | + // Anthropic-specific request object we expect to see sent to Cody Gateway. |
| 47 | + // See `anthropicRequestParameters`. |
| 48 | + outboundAnthropicRequest := map[string]any{ |
| 49 | + "model": "claude-2.0", |
| 50 | + "messages": []map[string]any{ |
| 51 | + { |
| 52 | + "role": "user", |
| 53 | + "content": []map[string]any{ |
| 54 | + { |
| 55 | + "type": "text", |
| 56 | + "text": "please make this code better", |
| 57 | + }, |
| 58 | + }, |
| 59 | + }, |
| 60 | + }, |
| 61 | + } |
| 62 | + |
| 63 | + // Stock response we would receive from Anthropic. |
| 64 | + // |
| 65 | + // The expected output is found also defined in the Anthropic completion provider codebase, |
| 66 | + // as `anthropicNonStreamingResponse`.` But it's easier to keep those types unexported. |
| 67 | + inboundAnthropicRequest := map[string]any{ |
| 68 | + "content": []map[string]string{ |
| 69 | + { |
| 70 | + "speak": "user", |
| 71 | + "text": "you should totally rewrite it in Rust!", |
| 72 | + }, |
| 73 | + }, |
| 74 | + "usage": map[string]int{ |
| 75 | + "input_token": 100, |
| 76 | + "output_tokens": 200, |
| 77 | + }, |
| 78 | + "stop_reason": "max_tokens", |
| 79 | + } |
| 80 | + |
| 81 | + return validAnthropicRequestData{ |
| 82 | + InitialCompletionRequest: initialCompletionRequest, |
| 83 | + OutboundAnthropicRequest: outboundAnthropicRequest, |
| 84 | + InboundAnthropicRequest: inboundAnthropicRequest, |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + t.Run("WithDefaultConfig", func(t *testing.T) { |
| 89 | + infra.SetSiteConfig(schema.SiteConfiguration{ |
| 90 | + CodyEnabled: pointers.Ptr(true), |
| 91 | + CodyPermissions: pointers.Ptr(false), |
| 92 | + CodyRestrictUsersFeatureFlag: pointers.Ptr(false), |
| 93 | + |
| 94 | + // LicenseKey is required in order to use Cody, but other than |
| 95 | + // that we don't provide any "completions" configuration. |
| 96 | + // This will default to Anthropic models. |
| 97 | + LicenseKey: "license-key", |
| 98 | + Completions: nil, |
| 99 | + }) |
| 100 | + |
| 101 | + // Confirm that the default configuration `Completions: nil` will use |
| 102 | + // Cody Gateway as the LLM API Provider for the Anthropic models. |
| 103 | + t.Run("ViaCodyGateway", func(t *testing.T) { |
| 104 | + // The Model isn't included in the CompletionRequestParameters, so we have the getModelFn callback |
| 105 | + // return claude-2. The Site Configuration will then route this to Cody Gateway (and not BYOK Anthropic), |
| 106 | + // and we sanity check the request to Cody Gateway matches what is expected, and we serve a valid response. |
| 107 | + infra.PushGetModelResult("anthropic/claude-2", nil) |
| 108 | + |
| 109 | + // Generate some basic test data and confirm that the completions handler |
| 110 | + // code works as expected. |
| 111 | + testData := getValidTestData() |
| 112 | + |
| 113 | + // Register our hook to verify Cody Gateway got called with |
| 114 | + // the requested data. |
| 115 | + infra.AssertCodyGatewayReceivesRequestWithResponse( |
| 116 | + t, assertLLMRequestOptions{ |
| 117 | + WantRequestPath: "/v1/completions/anthropic-messages", |
| 118 | + WantRequestObj: &testData.OutboundAnthropicRequest, |
| 119 | + OutResponseObj: &testData.InboundAnthropicRequest, |
| 120 | + }) |
| 121 | + |
| 122 | + status, responseBody := infra.CallChatCompletionAPI(t, testData.InitialCompletionRequest) |
| 123 | + |
| 124 | + assert.Equal(t, http.StatusOK, status) |
| 125 | + infra.AssertCompletionsResponse(t, responseBody, types.CompletionResponse{ |
| 126 | + Completion: "you should totally rewrite it in Rust!", |
| 127 | + StopReason: "max_tokens", |
| 128 | + Logprobs: nil, |
| 129 | + }) |
| 130 | + }) |
| 131 | + }) |
| 132 | + |
| 133 | + t.Run("ViaBYOK", func(t *testing.T) { |
| 134 | + const ( |
| 135 | + anthropicAPIKeyInConfig = "secret-api-key" |
| 136 | + anthropicAPIEndpointInConfig = "https://byok.anthropic.com/path/from/config" |
| 137 | + chatModelInConfig = "anthropic/claude-3-opus" |
| 138 | + codeModelInConfig = "anthropic/claude-3-haiku" |
| 139 | + ) |
| 140 | + |
| 141 | + infra.SetSiteConfig(schema.SiteConfiguration{ |
| 142 | + CodyEnabled: pointers.Ptr(true), |
| 143 | + CodyPermissions: pointers.Ptr(false), |
| 144 | + CodyRestrictUsersFeatureFlag: pointers.Ptr(false), |
| 145 | + |
| 146 | + // LicenseKey is required in order to use Cody. |
| 147 | + LicenseKey: "license-key", |
| 148 | + Completions: &schema.Completions{ |
| 149 | + Provider: "anthropic", |
| 150 | + AccessToken: anthropicAPIKeyInConfig, |
| 151 | + Endpoint: anthropicAPIEndpointInConfig, |
| 152 | + |
| 153 | + ChatModel: chatModelInConfig, |
| 154 | + CompletionModel: codeModelInConfig, |
| 155 | + }, |
| 156 | + }) |
| 157 | + |
| 158 | + t.Run("ChatModel", func(t *testing.T) { |
| 159 | + // Start with the stock test data, but customize it to reflect |
| 160 | + // what we expect to see based on the site configuration. |
| 161 | + testData := getValidTestData() |
| 162 | + testData.OutboundAnthropicRequest["model"] = "anthropic/claude-3-opus" |
| 163 | + |
| 164 | + // Register our hook to verify Cody Gateway got called with |
| 165 | + // the requested data. |
| 166 | + infra.AssertGenericExternalAPIRequestWithResponse( |
| 167 | + t, assertLLMRequestOptions{ |
| 168 | + WantRequestPath: "/path/from/config", |
| 169 | + WantRequestObj: &testData.OutboundAnthropicRequest, |
| 170 | + OutResponseObj: &testData.InboundAnthropicRequest, |
| 171 | + WantHeaders: map[string]string{ |
| 172 | + // Yes, Anthropic's API uses "X-Api-Key" rather than the "Authorization" header. 🤷 |
| 173 | + "X-Api-Key": anthropicAPIKeyInConfig, |
| 174 | + }, |
| 175 | + }) |
| 176 | + |
| 177 | + infra.PushGetModelResult(chatModelInConfig, nil) |
| 178 | + status, responseBody := infra.CallChatCompletionAPI(t, testData.InitialCompletionRequest) |
| 179 | + |
| 180 | + assert.Equal(t, http.StatusOK, status) |
| 181 | + infra.AssertCompletionsResponse(t, responseBody, types.CompletionResponse{ |
| 182 | + Completion: "you should totally rewrite it in Rust!", |
| 183 | + StopReason: "max_tokens", |
| 184 | + Logprobs: nil, |
| 185 | + }) |
| 186 | + }) |
| 187 | + }) |
| 188 | +} |
0 commit comments