From 97b54795f45d470faa7b0aa7aa14fd4b2277d9d5 Mon Sep 17 00:00:00 2001 From: Steph Milovic Date: Wed, 28 Aug 2024 08:04:37 -0600 Subject: [PATCH] [ResponseOps] Fix Azure Connector Streaming Validation (#191552) --- .../common/openai/constants.ts | 6 ++-- .../openai/lib/azure_openai_utils.test.ts | 31 ++++--------------- 2 files changed, 9 insertions(+), 28 deletions(-) diff --git a/x-pack/plugins/stack_connectors/common/openai/constants.ts b/x-pack/plugins/stack_connectors/common/openai/constants.ts index 1b1bd09618eb..c57720d9847a 100644 --- a/x-pack/plugins/stack_connectors/common/openai/constants.ts +++ b/x-pack/plugins/stack_connectors/common/openai/constants.ts @@ -36,8 +36,8 @@ export const DEFAULT_OPENAI_MODEL = 'gpt-4o'; export const OPENAI_CHAT_URL = 'https://api.openai.com/v1/chat/completions' as const; export const OPENAI_LEGACY_COMPLETION_URL = 'https://api.openai.com/v1/completions' as const; export const AZURE_OPENAI_CHAT_URL = - 'https://{your-resource-name}.openai.azure.com/openai/deployments/{deployment-id}/chat/completions?api-version={api-version}' as const; + '/openai/deployments/{deployment-id}/chat/completions?api-version={api-version}' as const; export const AZURE_OPENAI_COMPLETIONS_URL = - 'https://{your-resource-name}.openai.azure.com/openai/deployments/{deployment-id}/completions?api-version={api-version}' as const; + '/openai/deployments/{deployment-id}/completions?api-version={api-version}' as const; export const AZURE_OPENAI_COMPLETIONS_EXTENSIONS_URL = - 'https://{your-resource-name}.openai.azure.com/openai/deployments/{deployment-id}/extensions/chat/completions?api-version={api-version}' as const; + '/openai/deployments/{deployment-id}/extensions/chat/completions?api-version={api-version}' as const; diff --git a/x-pack/plugins/stack_connectors/server/connector_types/openai/lib/azure_openai_utils.test.ts b/x-pack/plugins/stack_connectors/server/connector_types/openai/lib/azure_openai_utils.test.ts index e9ad87a35ab7..6023d7715f4e 100644 --- a/x-pack/plugins/stack_connectors/server/connector_types/openai/lib/azure_openai_utils.test.ts +++ b/x-pack/plugins/stack_connectors/server/connector_types/openai/lib/azure_openai_utils.test.ts @@ -146,18 +146,14 @@ describe('Azure Open AI Utils', () => { const regex = transformApiUrlToRegex(AZURE_OPENAI_CHAT_URL); const match = chatUrl.match(regex); expect(match).not.toBeNull(); - expect(match![0]).toEqual( - 'https://My-test-resource-123.openai.azure.com/openai/deployments/NEW-DEPLOYMENT-321/chat/completions' - ); + expect(match![0]).toEqual('/openai/deployments/NEW-DEPLOYMENT-321/chat/completions'); }); it('should match valid completion url', () => { const regex = transformApiUrlToRegex(AZURE_OPENAI_COMPLETIONS_URL); const match = completionUrl.match(regex); expect(match).not.toBeNull(); - expect(match![0]).toEqual( - 'https://My-test-resource-123.openai.azure.com/openai/deployments/NEW-DEPLOYMENT-321/completions' - ); + expect(match![0]).toEqual('/openai/deployments/NEW-DEPLOYMENT-321/completions'); }); it('should match valid completion extensions url', () => { @@ -165,19 +161,14 @@ describe('Azure Open AI Utils', () => { const match = completionExtensionsUrl.match(regex); expect(match).not.toBeNull(); expect(match![0]).toEqual( - 'https://My-test-resource-123.openai.azure.com/openai/deployments/NEW-DEPLOYMENT-321/extensions/chat/completions' + '/openai/deployments/NEW-DEPLOYMENT-321/extensions/chat/completions' ); }); it('should not match invalid chat url', () => { const regex = transformApiUrlToRegex(AZURE_OPENAI_CHAT_URL); [ - 'https://openai.azure.com/openai/deployments/NEW-DEPLOYMENT-321/chat/completions?api-version=2023-05-15', - 'https://My-test-resource-123.openaiazure.com/openai/deployments/NEW-DEPLOYMENT-321/chat/completions?api-version=2023-05-15', - 'https://My-test-resource-123.openaiaazure.com/openai/deployments/NEW-DEPLOYMENT-321/chat/completions?api-version=2023-05-15', - 'https://My-test-resource-123openai.azure.com/openai/deployments/NEW-DEPLOYMENT-321/chat/completions?api-version=2023-05-15', - 'https://My-test-resource-123.openai.azure.com//openai/deployments/NEW-DEPLOYMENT-321/chat/completions?api-version=2023-05-15', - 'https:///My-test-resource-123.openai.azure.com/openai/deployments/NEW-DEPLOYMENT-321/chat/completions?api-version=2023-05-15', + 'https://openai.azure.com/openai/deployments/NEW-DEPLOYMENT-321/chat/not-completions?api-version=2023-05-15', ].forEach((url) => { const match = url.match(regex); expect(match).toBeNull(); @@ -187,12 +178,7 @@ describe('Azure Open AI Utils', () => { it('should not match invalid completion url', () => { const regex = transformApiUrlToRegex(AZURE_OPENAI_COMPLETIONS_URL); [ - 'https://fooai.com/completions?api-version=2023-05-15', - 'https://My-test-resource-123.openaiazure.com/openai/deployments/NEW-DEPLOYMENT-321/completions?api-version=2023-05-15', - 'https://My-test-resource-123.openaiaazure.com/openai/deployments/NEW-DEPLOYMENT-321/completions?api-version=2023-05-15', - 'https://My-test-resource-123openai.azure.com/openai/deployments/NEW-DEPLOYMENT-321/completions?api-version=2023-05-15', - 'https://My-test-resource-123.openai.azure.com//openai/deployments/NEW-DEPLOYMENT-321/completions?api-version=2023-05-15', - 'https:///My-test-resource-123.openai.azure.com/openai/deployments/NEW-DEPLOYMENT-321/completions?api-version=2023-05-15', + 'https://My-test-resource-123.openaiazure.com/openai/deployments/NEW-DEPLOYMENT-321/not-completions?api-version=2023-05-15', ].forEach((url) => { const match = url.match(regex); expect(match).toBeNull(); @@ -202,12 +188,7 @@ describe('Azure Open AI Utils', () => { it('should not match invalid completion extensions url', () => { const regex = transformApiUrlToRegex(AZURE_OPENAI_COMPLETIONS_EXTENSIONS_URL); [ - 'https://My-test-resource-123.openai.azure.com/openai/deployments/extensions/chat/completions?api-version=2023-05-15', - 'https://My-test-resource-123.openaiazure.com/openai/deployments/NEW-DEPLOYMENT-321/extensions/chat/completions?api-version=2023-05-15', - 'https://My-test-resource-123.openaiaazure.com/openai/deployments/NEW-DEPLOYMENT-321/extensions/chat/completions?api-version=2023-05-15', - 'https://My-test-resource-123openai.azure.com/openai/deployments/NEW-DEPLOYMENT-321/extensions/chat/completions?api-version=2023-05-15', - 'https:///My-test-resource-123.openai.azure.com/openai/deployments/NEW-DEPLOYMENT-321/extensions/chat/completions?api-version=2023-05-15', - 'https://My-test-resource-123.openai.azure.com/openai//deployments/NEW-DEPLOYMENT-321/extensions/chat/completions?api-version=2023-05-15', + 'https://My-test-resource-123.openai.azure.com/openai/deployments/extensions/chat/not-completions?api-version=2023-05-15', ].forEach((url) => { const match = url.match(regex); expect(match).toBeNull();