anthropic.php
1 year ago
core.php
1 year ago
factory.php
1 year ago
google.php
1 year ago
huggingface.php
1 year ago
openai.php
1 year ago
openrouter.php
1 year ago
replicate.php
1 year ago
huggingface.php
113 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_messages( $query ) { |
| 20 | $messages = parent::build_messages( $query ); |
| 21 | |
| 22 | // If the role is not either "user" or "assistant", we need to set it to "assistant". |
| 23 | foreach ( $messages as &$message ) { |
| 24 | if ( !in_array( $message['role'], array( 'user', 'assistant' ) ) ) { |
| 25 | $message['role'] = 'assistant'; |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | $messages = $this->streamline_messages( $messages ); |
| 30 | return $messages; |
| 31 | } |
| 32 | |
| 33 | protected function build_body( $query, $streamCallback = null, $extra = null ) { |
| 34 | $body = parent::build_body( $query, $streamCallback ); |
| 35 | // To use "Text Generation Inference" (OpenAI's API) with HuggingFace, we need to specify TGI as the model. |
| 36 | $body['model'] = 'tgi'; |
| 37 | // 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. |
| 38 | $body = array_intersect_key( $body, array_flip( array( 'model', 'stream', 'max_tokens', 'frequency_penalty', 'logprobs', 'seed', 'temperature', 'top_p', 'messages' ) ) ); |
| 39 | return $body; |
| 40 | } |
| 41 | |
| 42 | protected function build_url( $query, $endpoint = null ) { |
| 43 | $model = $query->model; |
| 44 | if ( isset( $this->env['customModels'] ) ) { |
| 45 | foreach ( $this->env['customModels'] as $customModel ) { |
| 46 | if ( $customModel['name'] === $model ) { |
| 47 | $endpoint = $customModel['apiUrl'] . '/v1/'; |
| 48 | break; |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | if ( $endpoint === null ) { |
| 53 | throw new Exception( 'Model not found for HuggingFace: ' . $model ); |
| 54 | } |
| 55 | $url = parent::build_url( $query, $endpoint ); |
| 56 | return $url; |
| 57 | } |
| 58 | |
| 59 | protected function build_headers( $query ) { |
| 60 | parent::build_headers( $query ); |
| 61 | $headers = array( |
| 62 | 'Content-Type' => 'application/json', |
| 63 | 'Authorization' => 'Bearer ' . $this->apiKey, |
| 64 | 'User-Agent' => 'AI Engine', |
| 65 | ); |
| 66 | return $headers; |
| 67 | } |
| 68 | |
| 69 | protected function get_service_name() { |
| 70 | return "HuggingFace"; |
| 71 | } |
| 72 | |
| 73 | public function get_models() { |
| 74 | $models = []; |
| 75 | if ( isset( $this->env['customModels'] ) ) { |
| 76 | foreach ( $this->env['customModels'] as $model ) { |
| 77 | $tags = isset( $model['tags'] ) ? $model['tags'] : []; |
| 78 | if ( !in_array( 'core', $tags ) ) { |
| 79 | $tags[] = 'core'; |
| 80 | } |
| 81 | if ( !in_array( 'chat', $tags ) ) { |
| 82 | $tags[] = 'chat'; |
| 83 | } |
| 84 | $features = in_array( 'image', $tags ) ? ['text-to-image'] : ['completion']; |
| 85 | $models[] = array( |
| 86 | 'model' => $model['name'], |
| 87 | 'name' => $model['name'], |
| 88 | 'features' => $features, |
| 89 | 'tags' => $tags, |
| 90 | ); |
| 91 | } |
| 92 | } |
| 93 | return $models; |
| 94 | } |
| 95 | |
| 96 | public function get_price( Meow_MWAI_Query_Base $query, Meow_MWAI_Reply $reply ) { |
| 97 | return null; |
| 98 | } |
| 99 | |
| 100 | // Check if there are errors in the response from OpenAI, and throw an exception if so. |
| 101 | protected function handle_response_errors( $data ) { |
| 102 | if ( isset( $data['error'] ) ) { |
| 103 | $message = $data['error']; |
| 104 | if ( is_array( $message ) ) { |
| 105 | $message = $message['message']; |
| 106 | } |
| 107 | if ( preg_match( '/API key provided(: .*)\./', $message, $matches ) ) { |
| 108 | $message = str_replace( $matches[1], '', $message ); |
| 109 | } |
| 110 | throw new Exception( $message ); |
| 111 | } |
| 112 | } |
| 113 | } |