Skip to content

Commit c84ab5f

Browse files
wooruiSTRRL
andauthored
feat: support cloudflare AI Gateway flavored azure openai (#715)
* feat: support cloudflare AI Gateway flavored azure openai Signed-off-by: STRRL <[email protected]> * test: add test for cloudflare azure fullURL --------- Signed-off-by: STRRL <[email protected]> Co-authored-by: STRRL <[email protected]>
1 parent 2d58f8f commit c84ab5f

File tree

3 files changed

+48
-5
lines changed

3 files changed

+48
-5
lines changed

api_internal_test.go

+36
Original file line numberDiff line numberDiff line change
@@ -148,3 +148,39 @@ func TestAzureFullURL(t *testing.T) {
148148
})
149149
}
150150
}
151+
152+
func TestCloudflareAzureFullURL(t *testing.T) {
153+
cases := []struct {
154+
Name string
155+
BaseURL string
156+
Expect string
157+
}{
158+
{
159+
"CloudflareAzureBaseURLWithSlashAutoStrip",
160+
"https://gateway.ai.cloudflare.com/v1/dnekeim2i39dmm4mldemakiem3i4mkw3/demo/azure-openai/resource/chatgpt-demo/",
161+
"https://gateway.ai.cloudflare.com/v1/dnekeim2i39dmm4mldemakiem3i4mkw3/demo/azure-openai/resource/chatgpt-demo/" +
162+
"chat/completions?api-version=2023-05-15",
163+
},
164+
{
165+
"CloudflareAzureBaseURLWithoutSlashOK",
166+
"https://gateway.ai.cloudflare.com/v1/dnekeim2i39dmm4mldemakiem3i4mkw3/demo/azure-openai/resource/chatgpt-demo",
167+
"https://gateway.ai.cloudflare.com/v1/dnekeim2i39dmm4mldemakiem3i4mkw3/demo/azure-openai/resource/chatgpt-demo/" +
168+
"chat/completions?api-version=2023-05-15",
169+
},
170+
}
171+
172+
for _, c := range cases {
173+
t.Run(c.Name, func(t *testing.T) {
174+
az := DefaultAzureConfig("dummy", c.BaseURL)
175+
az.APIType = APITypeCloudflareAzure
176+
177+
cli := NewClientWithConfig(az)
178+
179+
actual := cli.fullURL("/chat/completions")
180+
if actual != c.Expect {
181+
t.Errorf("Expected %s, got %s", c.Expect, actual)
182+
}
183+
t.Logf("Full URL: %s", actual)
184+
})
185+
}
186+
}

client.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ func sendRequestStream[T streamable](client *Client, req *http.Request) (*stream
182182
func (c *Client) setCommonHeaders(req *http.Request) {
183183
// https://learn.microsoft.com/en-us/azure/cognitive-services/openai/reference#authentication
184184
// Azure API Key authentication
185-
if c.config.APIType == APITypeAzure {
185+
if c.config.APIType == APITypeAzure || c.config.APIType == APITypeCloudflareAzure {
186186
req.Header.Set(AzureAPIKeyHeader, c.config.authToken)
187187
} else if c.config.authToken != "" {
188188
// OpenAI or Azure AD authentication
@@ -246,7 +246,13 @@ func (c *Client) fullURL(suffix string, args ...any) string {
246246
)
247247
}
248248

249-
// c.config.APIType == APITypeOpenAI || c.config.APIType == ""
249+
// https://developers.cloudflare.com/ai-gateway/providers/azureopenai/
250+
if c.config.APIType == APITypeCloudflareAzure {
251+
baseURL := c.config.BaseURL
252+
baseURL = strings.TrimRight(baseURL, "/")
253+
return fmt.Sprintf("%s%s?api-version=%s", baseURL, suffix, c.config.APIVersion)
254+
}
255+
250256
return fmt.Sprintf("%s%s", c.config.BaseURL, suffix)
251257
}
252258

config.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@ const (
1616
type APIType string
1717

1818
const (
19-
APITypeOpenAI APIType = "OPEN_AI"
20-
APITypeAzure APIType = "AZURE"
21-
APITypeAzureAD APIType = "AZURE_AD"
19+
APITypeOpenAI APIType = "OPEN_AI"
20+
APITypeAzure APIType = "AZURE"
21+
APITypeAzureAD APIType = "AZURE_AD"
22+
APITypeCloudflareAzure APIType = "CLOUDFLARE_AZURE"
2223
)
2324

2425
const AzureAPIKeyHeader = "api-key"

0 commit comments

Comments
 (0)