core.php
2 years ago
factory.php
2 years ago
google.php
2 years ago
huggingface.php
2 years ago
openai.php
2 years ago
openrouter.php
2 years ago
huggingface.php
99 lines
| 1 | <?php |
| 2 | |
| 3 | class Meow_MWAI_Engines_HuggingFace extends Meow_MWAI_Engines_OpenAI |
| 4 | { |
| 5 | |
| 6 | public function __construct( $core, $env ) |
| 7 | { |
| 8 | parent::__construct( $core, $env ); |
| 9 | } |
| 10 | |
| 11 | protected function set_environment() { |
| 12 | $env = $this->env; |
| 13 | $this->apiKey = $env['apikey']; |
| 14 | if ( !$this->envType === 'huggingface' ) { |
| 15 | throw new Exception( 'Unknown environment type: ' . $this->envType ); |
| 16 | } |
| 17 | } |
| 18 | |
| 19 | protected function build_body( $query, $streamCallback = null ) { |
| 20 | $body = parent::build_body( $query, $streamCallback ); |
| 21 | // To use "Text Generation Inference" (OpenAI's API) with HuggingFace, we need to specify TGI as the model. |
| 22 | $body['model'] = 'tgi'; |
| 23 | // Certain OpenAI features, like function calling, are not compatible with TGI. Currently, the Messages API supports the following chat completion parameters: stream, max_tokens, frequency_penalty, logprobs, seed, temperature, and top_p. Let's remove everything else. |
| 24 | $body = array_intersect_key( $body, array_flip( array( 'model', 'stream', 'max_tokens', 'frequency_penalty', 'logprobs', 'seed', 'temperature', 'top_p', 'messages' ) ) ); |
| 25 | return $body; |
| 26 | } |
| 27 | |
| 28 | protected function build_url( $query, $endpoint = null ) { |
| 29 | $model = $query->model; |
| 30 | if ( isset( $this->env['customModels'] ) ) { |
| 31 | foreach ( $this->env['customModels'] as $customModel ) { |
| 32 | if ( $customModel['name'] === $model ) { |
| 33 | $endpoint = $customModel['apiUrl'] . '/v1/'; |
| 34 | break; |
| 35 | } |
| 36 | } |
| 37 | } |
| 38 | if ( $endpoint === null ) { |
| 39 | throw new Exception( 'Model not found for HuggingFace: ' . $model ); |
| 40 | } |
| 41 | $url = parent::build_url( $query, $endpoint ); |
| 42 | return $url; |
| 43 | } |
| 44 | |
| 45 | protected function build_headers( $query ) { |
| 46 | parent::build_headers( $query ); |
| 47 | $headers = array( |
| 48 | 'Content-Type' => 'application/json', |
| 49 | 'Authorization' => 'Bearer ' . $this->apiKey, |
| 50 | 'User-Agent' => 'AI Engine', |
| 51 | ); |
| 52 | return $headers; |
| 53 | } |
| 54 | |
| 55 | protected function get_service_name() { |
| 56 | return "HuggingFace"; |
| 57 | } |
| 58 | |
| 59 | public function get_models() { |
| 60 | $models = []; |
| 61 | if ( isset( $this->env['customModels'] ) ) { |
| 62 | foreach ( $this->env['customModels'] as $model ) { |
| 63 | $tags = isset( $model['tags'] ) ? $model['tags'] : []; |
| 64 | if ( !in_array( 'core', $tags ) ) { |
| 65 | $tags[] = 'core'; |
| 66 | } |
| 67 | if ( !in_array( 'chat', $tags ) ) { |
| 68 | $tags[] = 'chat'; |
| 69 | } |
| 70 | $mode = in_array( 'image', $tags ) ? 'image' : 'chat'; |
| 71 | $models[] = array( |
| 72 | 'model' => $model['name'], |
| 73 | 'name' => $model['name'], |
| 74 | 'mode' => $mode, |
| 75 | 'tags' => $tags, |
| 76 | ); |
| 77 | } |
| 78 | } |
| 79 | return $models; |
| 80 | } |
| 81 | |
| 82 | public function get_price( Meow_MWAI_Query_Base $query, Meow_MWAI_Reply $reply ) { |
| 83 | return null; |
| 84 | } |
| 85 | |
| 86 | // Check if there are errors in the response from OpenAI, and throw an exception if so. |
| 87 | protected function handle_response_errors( $data ) { |
| 88 | if ( isset( $data['error'] ) ) { |
| 89 | $message = $data['error']; |
| 90 | if ( is_array( $message ) ) { |
| 91 | $message = $message['message']; |
| 92 | } |
| 93 | if ( preg_match( '/API key provided(: .*)\./', $message, $matches ) ) { |
| 94 | $message = str_replace( $matches[1], '', $message ); |
| 95 | } |
| 96 | throw new Exception( $message ); |
| 97 | } |
| 98 | } |
| 99 | } |