azure.php
90 lines
| 1 | <?php |
| 2 | |
| 3 | trait Meow_MWAI_Engines_Trait_Azure { |
| 4 | /** |
| 5 | * Check if environment is Azure |
| 6 | */ |
| 7 | protected function is_azure( $env ) { |
| 8 | return isset( $env['type'] ) && $env['type'] === 'azure'; |
| 9 | } |
| 10 | |
| 11 | /** |
| 12 | * Build Azure endpoint URL |
| 13 | */ |
| 14 | protected function build_azure_url( $env, $endpoint, $model = null ) { |
| 15 | $url = trailingslashit( $env['endpoint'] ); |
| 16 | |
| 17 | // Add deployment name if model is provided |
| 18 | if ( $model && !empty( $env['deployments'] ) ) { |
| 19 | $deployment = $this->get_azure_deployment( $env, $model ); |
| 20 | if ( $deployment ) { |
| 21 | $url .= 'openai/deployments/' . $deployment . '/'; |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | // Add the endpoint |
| 26 | $url .= $endpoint; |
| 27 | |
| 28 | // Add API version |
| 29 | if ( !empty( $env['apiversion'] ) ) { |
| 30 | $url .= '?api-version=' . $env['apiversion']; |
| 31 | } |
| 32 | |
| 33 | return $url; |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Get Azure deployment name for a model |
| 38 | */ |
| 39 | protected function get_azure_deployment( $env, $model ) { |
| 40 | if ( empty( $env['deployments'] ) ) { |
| 41 | return $model; |
| 42 | } |
| 43 | |
| 44 | foreach ( $env['deployments'] as $deployment ) { |
| 45 | if ( $deployment['model'] === $model ) { |
| 46 | return $deployment['name']; |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | // Default to model name if no deployment found |
| 51 | return $model; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Build Azure headers |
| 56 | */ |
| 57 | protected function build_azure_headers( $env, $headers = [] ) { |
| 58 | if ( !empty( $env['apikey'] ) ) { |
| 59 | $headers['api-key'] = $env['apikey']; |
| 60 | } |
| 61 | |
| 62 | // Azure requires Content-Type |
| 63 | if ( !isset( $headers['Content-Type'] ) ) { |
| 64 | $headers['Content-Type'] = 'application/json'; |
| 65 | } |
| 66 | |
| 67 | return $headers; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Transform body for Azure compatibility |
| 72 | */ |
| 73 | protected function transform_for_azure( $body, $env ) { |
| 74 | // Azure doesn't support certain OpenAI-specific parameters |
| 75 | $unsupported = [ 'logit_bias', 'user' ]; |
| 76 | foreach ( $unsupported as $param ) { |
| 77 | if ( isset( $body[$param] ) ) { |
| 78 | unset( $body[$param] ); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | // Azure uses deployment names instead of model names |
| 83 | if ( isset( $body['model'] ) ) { |
| 84 | unset( $body['model'] ); |
| 85 | } |
| 86 | |
| 87 | return $body; |
| 88 | } |
| 89 | } |
| 90 |