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