Skip to content

Commit 1e4891d

Browse files
authored
Merge pull request #1465 from arturfromtabnine/ar/fix-azure-ai-anthropic-stream-response
feat: add openai compatible response support for anthropic on azure
2 parents d42a079 + 7dbac76 commit 1e4891d

8 files changed

Lines changed: 283 additions & 269 deletions

File tree

src/providers/anthropic/chatComplete.ts

Lines changed: 260 additions & 255 deletions
Large diffs are not rendered by default.

src/providers/anthropic/complete.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ export const AnthropicCompleteResponseTransform: (
8686
) => {
8787
if (responseStatus !== 200) {
8888
const errorResposne = AnthropicErrorResponseTransform(
89-
response as AnthropicErrorResponse
89+
response as AnthropicErrorResponse,
90+
ANTHROPIC
9091
);
9192
if (errorResposne) return errorResposne;
9293
}

src/providers/anthropic/index.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
import { ANTHROPIC } from '../../globals';
12
import { ProviderConfigs } from '../types';
23
import AnthropicAPIConfig from './api';
34
import {
45
AnthropicChatCompleteConfig,
5-
AnthropicChatCompleteResponseTransform,
6-
AnthropicChatCompleteStreamChunkTransform,
6+
getAnthropicChatCompleteResponseTransform,
7+
getAnthropicStreamChunkTransform,
78
} from './chatComplete';
89
import {
910
AnthropicCompleteConfig,
@@ -24,8 +25,8 @@ const AnthropicConfig: ProviderConfigs = {
2425
responseTransforms: {
2526
'stream-complete': AnthropicCompleteStreamChunkTransform,
2627
complete: AnthropicCompleteResponseTransform,
27-
chatComplete: AnthropicChatCompleteResponseTransform,
28-
'stream-chatComplete': AnthropicChatCompleteStreamChunkTransform,
28+
chatComplete: getAnthropicChatCompleteResponseTransform(ANTHROPIC),
29+
'stream-chatComplete': getAnthropicStreamChunkTransform(ANTHROPIC),
2930
messages: AnthropicMessagesResponseTransform,
3031
},
3132
};

src/providers/anthropic/messages.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export const AnthropicMessagesResponseTransform = (
1313
responseStatus: number
1414
): MessagesResponse | ErrorResponse => {
1515
if (responseStatus !== 200 && 'error' in response) {
16-
return AnthropicErrorResponseTransform(response);
16+
return AnthropicErrorResponseTransform(response, ANTHROPIC);
1717
}
1818

1919
if ('model' in response) return response;

src/providers/anthropic/utils.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
import { ANTHROPIC } from '../../globals';
21
import { ErrorResponse } from '../types';
32
import { generateErrorResponse } from '../utils';
43
import { AnthropicErrorResponse } from './types';
54

65
export const AnthropicErrorResponseTransform: (
7-
response: AnthropicErrorResponse
8-
) => ErrorResponse = (response) => {
6+
response: AnthropicErrorResponse,
7+
provider: string
8+
) => ErrorResponse = (response, provider) => {
99
return generateErrorResponse(
1010
{
1111
message: response.error?.message,
1212
type: response.error?.type,
1313
param: null,
1414
code: null,
1515
},
16-
ANTHROPIC
16+
provider
1717
);
1818
};

src/providers/azure-ai-inference/index.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ import {
2727
} from './utils';
2828
import {
2929
AnthropicChatCompleteConfig,
30-
AnthropicChatCompleteResponseTransform,
30+
getAnthropicChatCompleteResponseTransform,
31+
getAnthropicStreamChunkTransform,
3132
} from '../anthropic/chatComplete';
3233
import {
3334
AzureAIInferenceMessagesConfig,
@@ -43,7 +44,7 @@ const AzureAIInferenceAPIConfig: ProviderConfigs = {
4344
? AnthropicChatCompleteConfig
4445
: AzureAIInferenceChatCompleteConfig;
4546
const chatCompleteResponseTransform = isAnthropicModel
46-
? AnthropicChatCompleteResponseTransform
47+
? getAnthropicChatCompleteResponseTransform(AZURE_AI_INFERENCE)
4748
: AzureAIInferenceChatCompleteResponseTransform(AZURE_AI_INFERENCE);
4849
return {
4950
complete: AzureAIInferenceCompleteConfig,
@@ -68,6 +69,10 @@ const AzureAIInferenceAPIConfig: ProviderConfigs = {
6869
},
6970
responseTransforms: {
7071
complete: AzureAIInferenceCompleteResponseTransform(AZURE_AI_INFERENCE),
72+
...(isAnthropicModel && {
73+
'stream-chatComplete':
74+
getAnthropicStreamChunkTransform(AZURE_AI_INFERENCE),
75+
}),
7176
chatComplete: chatCompleteResponseTransform,
7277
messages: AzureAIInferenceMessagesResponseTransform,
7378
embed: AzureAIInferenceEmbedResponseTransform(AZURE_AI_INFERENCE),

src/providers/azure-ai-inference/messages.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ export const AzureAIInferenceMessagesResponseTransform = (
1414
): MessagesResponse | ErrorResponse => {
1515
if (responseStatus !== 200) {
1616
const errorResposne = AnthropicErrorResponseTransform(
17-
response as AnthropicErrorResponse
17+
response as AnthropicErrorResponse,
18+
AZURE_AI_INFERENCE
1819
);
1920
if (errorResposne) return errorResposne;
2021
}

src/providers/google-vertex-ai/messages.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ export const VertexAnthropicMessagesResponseTransform = (
2323
): MessagesResponse | ErrorResponse => {
2424
if (responseStatus !== 200) {
2525
const errorResposne = AnthropicErrorResponseTransform(
26-
response as AnthropicErrorResponse
26+
response as AnthropicErrorResponse,
27+
GOOGLE_VERTEX_AI
2728
);
2829
if (errorResposne) return errorResposne;
2930
}

0 commit comments

Comments
 (0)