| 24 | } |
| 25 | |
| 26 | export class LobeAzureAI implements LobeRuntimeAI { |
| 27 | client: ModelClient; |
| 28 | |
| 29 | constructor(params?: AzureAIParams) { |
| 30 | if (!params?.apiKey || !params?.baseURL) |
| 31 | throw AgentRuntimeError.createError(AgentRuntimeErrorType.InvalidProviderAPIKey); |
| 32 | |
| 33 | this.client = createClient(params?.baseURL, new AzureKeyCredential(params?.apiKey)); |
| 34 | |
| 35 | this.baseURL = params?.baseURL; |
| 36 | } |
| 37 | |
| 38 | baseURL: string; |
| 39 | |
| 40 | async chat(payload: ChatStreamPayload, options?: ChatMethodOptions) { |
| 41 | // Remove internal apiMode parameter to prevent sending to Azure AI API |
| 42 | |
| 43 | const { |
| 44 | messages, |
| 45 | model, |
| 46 | temperature, |
| 47 | top_p, |
| 48 | apiMode: _, |
| 49 | preserveThinking: _pt, |
| 50 | ...params |
| 51 | } = payload; |
| 52 | // o1 series models on Azure OpenAI does not support streaming currently |
| 53 | const enableStreaming = model.includes('o1') ? false : (params.stream ?? true); |
| 54 | |
| 55 | const updatedMessages = messages.map((message) => ({ |
| 56 | ...message, |
| 57 | role: |
| 58 | // Convert 'system' role to 'user' or 'developer' based on the model |
| 59 | (model.includes('o1') || model.includes('o3')) && message.role === 'system' |
| 60 | ? [...systemToUserModels].some((sub) => model.includes(sub)) |
| 61 | ? 'user' |
| 62 | : 'developer' |
| 63 | : message.role, |
| 64 | })); |
| 65 | |
| 66 | try { |
| 67 | const response = this.client.path('/chat/completions').post({ |
| 68 | body: { |
| 69 | messages: updatedMessages as OpenAI.ChatCompletionMessageParam[], |
| 70 | model, |
| 71 | ...params, |
| 72 | stream: enableStreaming, |
| 73 | temperature: model.includes('o3') || model.includes('o4') ? undefined : temperature, |
| 74 | tool_choice: params.tools ? 'auto' : undefined, |
| 75 | top_p: model.includes('o3') || model.includes('o4') ? undefined : top_p, |
| 76 | }, |
| 77 | }); |
| 78 | |
| 79 | if (enableStreaming) { |
| 80 | const unifiedStream = await (async () => { |
| 81 | if (typeof window === 'undefined') { |
| 82 | /** |
| 83 | * In Node.js the SDK exposes a Node readable stream, so we convert it to a Web ReadableStream |
nothing calls this directly
no outgoing calls
no test coverage detected