engines
2 years ago
modules
2 years ago
queries
2 years ago
admin.php
2 years ago
api.php
2 years ago
core.php
2 years ago
init.php
2 years ago
reply.php
2 years ago
rest.php
2 years ago
reply.php
244 lines
| 1 | <?php |
| 2 | |
| 3 | class Meow_MWAI_Reply implements JsonSerializable { |
| 4 | public $id = null; |
| 5 | public $result = ''; |
| 6 | public $results = []; |
| 7 | public $usage = [ |
| 8 | 'prompt_tokens' => 0, |
| 9 | 'completion_tokens' => 0, |
| 10 | 'total_tokens' => 0, |
| 11 | 'price' => null, |
| 12 | ]; |
| 13 | public $query = null; |
| 14 | public $type = 'text'; |
| 15 | |
| 16 | // This is when models return a message that needs to be executed (functions, tools, etc) |
| 17 | public $needFeedbacks = []; |
| 18 | |
| 19 | public function __construct( $query = null ) { |
| 20 | $this->query = $query; |
| 21 | } |
| 22 | |
| 23 | #[\ReturnTypeWillChange] |
| 24 | public function jsonSerialize() { |
| 25 | $isEmbedding = false; |
| 26 | $embeddingsDimensions = null; |
| 27 | $embedddingsMessage = null; |
| 28 | if ( is_array( $this->results ) && count( $this->results ) > 0 ) { |
| 29 | $isEmbedding = is_array( $this->results[0] ); |
| 30 | if ( $isEmbedding ) { |
| 31 | $embeddingsDimensions = count( $this->results[0] ); |
| 32 | $embedddingsMessage = "A $embeddingsDimensions-dimensional embedding was returned."; |
| 33 | } |
| 34 | } |
| 35 | $data = [ |
| 36 | 'result' => $isEmbedding ? $embedddingsMessage : $this->result, |
| 37 | 'results' => $isEmbedding ? [] : $this->results, |
| 38 | 'usage' => $this->usage, |
| 39 | 'system' => [ |
| 40 | 'class' => get_class( $this ), |
| 41 | ] |
| 42 | ]; |
| 43 | if ( !empty( $this->needFeedbacks ) ) { |
| 44 | $data['needFeedbacks'] = $this->needFeedbacks; |
| 45 | } |
| 46 | return $data; |
| 47 | } |
| 48 | |
| 49 | public function set_usage( $usage ) { |
| 50 | $this->usage = $usage; |
| 51 | } |
| 52 | |
| 53 | public function set_id( $id ) { |
| 54 | $this->id = $id; |
| 55 | } |
| 56 | |
| 57 | public function set_type( $type ) { |
| 58 | $this->type = $type; |
| 59 | } |
| 60 | |
| 61 | public function get_total_tokens() { |
| 62 | return $this->usage['total_tokens']; |
| 63 | } |
| 64 | |
| 65 | public function get_in_tokens( $query = null ) { |
| 66 | $in_tokens = $this->usage['prompt_tokens']; |
| 67 | if ( empty( $in_tokens ) && $query ) { |
| 68 | $in_tokens = $query->get_in_tokens(); |
| 69 | } |
| 70 | return $in_tokens; |
| 71 | } |
| 72 | |
| 73 | public function get_out_tokens() { |
| 74 | $out_tokens = $this->usage['completion_tokens']; |
| 75 | if ( empty( $out_tokens ) ) { |
| 76 | $out_tokens = Meow_MWAI_Core::estimate_tokens( $this->result ); |
| 77 | } |
| 78 | return $out_tokens; |
| 79 | } |
| 80 | |
| 81 | public function get_price() { |
| 82 | // If it's not set return null, but it can be 0 |
| 83 | if ( !isset( $this->usage['price'] ) ) { |
| 84 | return null; |
| 85 | } |
| 86 | return $this->usage['price']; |
| 87 | } |
| 88 | |
| 89 | public function get_units() { |
| 90 | if ( isset( $this->usage['total_tokens'] ) ) { |
| 91 | return $this->usage['total_tokens']; |
| 92 | } |
| 93 | else if ( isset( $this->usage['images'] ) ) { |
| 94 | return $this->usage['images']; |
| 95 | } |
| 96 | else if ( isset( $this->usage['seconds'] ) ) { |
| 97 | return $this->usage['seconds']; |
| 98 | } |
| 99 | return null; |
| 100 | } |
| 101 | |
| 102 | public function get_type() { |
| 103 | return $this->type; |
| 104 | } |
| 105 | |
| 106 | public function set_reply( $reply ) { |
| 107 | $this->result = $reply; |
| 108 | $this->results[] = [ $reply ]; |
| 109 | } |
| 110 | |
| 111 | public function replace( $search, $replace ) { |
| 112 | $this->result = str_replace( $search, $replace, $this->result ); |
| 113 | $this->results = array_map( function( $result ) use ( $search, $replace ) { |
| 114 | return str_replace( $search, $replace, $result ); |
| 115 | }, $this->results ); |
| 116 | } |
| 117 | |
| 118 | private function extract_arguments( $funcArgs ) { |
| 119 | $finalArgs = []; |
| 120 | if ( is_string( $funcArgs ) ) { |
| 121 | $arguments = trim( str_replace( "\n", "", $funcArgs ) ); |
| 122 | if ( substr( $arguments, 0, 1 ) == '{' ) { |
| 123 | $arguments = json_decode( $arguments, true ); |
| 124 | $finalArgs = $arguments; |
| 125 | } |
| 126 | } |
| 127 | else if ( is_array( $funcArgs ) ) { |
| 128 | $finalArgs = $funcArgs; |
| 129 | } |
| 130 | return $finalArgs; |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Set the choices from OpenAI as the results. |
| 135 | * The last (or only) result is set as the result. |
| 136 | * @param array $choices ID of the model to use. |
| 137 | */ |
| 138 | public function set_choices( $choices, $rawMessage = null) { |
| 139 | $this->results = []; |
| 140 | if ( is_array( $choices ) ) { |
| 141 | foreach ( $choices as $choice ) { |
| 142 | |
| 143 | // It's chat completion |
| 144 | if ( isset( $choice['message'] ) ) { |
| 145 | |
| 146 | // It's text content |
| 147 | if ( isset( $choice['message']['content'] ) ) { |
| 148 | $content = trim( $choice['message']['content'] ); |
| 149 | $this->results[] = $content; |
| 150 | $this->result = $content; |
| 151 | } |
| 152 | |
| 153 | // It's a tool call (OpenAI-style and Anthropic-style) |
| 154 | $needFeedbacks = []; |
| 155 | if ( isset( $choice['message']['tool_calls'] ) ) { |
| 156 | $tools = $choice['message']['tool_calls']; |
| 157 | foreach ( $tools as $tool ) { |
| 158 | if ( $tool['type'] === 'function' ) { |
| 159 | $needFeedbacks[] = [ |
| 160 | 'toolId' => $tool['id'], |
| 161 | 'mode' => 'interactive', |
| 162 | 'type' => 'tool_call', |
| 163 | 'name' => trim( $tool['function']['name'] ), |
| 164 | 'arguments' => $this->extract_arguments( $tool['function']['arguments'] ), |
| 165 | // Represent the original message that triggered the function call |
| 166 | 'rawMessage' => $rawMessage ? $rawMessage : $choice['message'], |
| 167 | ]; |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | // If it's a function call (Open-AI style; usually for a final execution) |
| 173 | if ( isset( $choice['message']['function_call'] ) ) { |
| 174 | $content = $choice['message']['function_call']; |
| 175 | $needFeedbacks[] = [ |
| 176 | 'toolId' => null, |
| 177 | 'mode' => 'static', |
| 178 | 'type' => 'function_call', |
| 179 | 'name' => trim( $choice['message']['function_call']['name'] ), |
| 180 | 'arguments' => $this->extract_arguments( $tool['message']['function_call']['arguments'] ), |
| 181 | 'rawMessage' => $rawMessage ? $rawMessage : $choice['message'], |
| 182 | ]; |
| 183 | } |
| 184 | |
| 185 | // Resolve the original function from the query |
| 186 | if ( !empty( $needFeedbacks ) ) { |
| 187 | foreach ( $needFeedbacks as &$needFeedback ) { |
| 188 | if ( $needFeedback['type'] !== 'function_call' && $needFeedback['type'] !== 'tool_call' ) { |
| 189 | continue; |
| 190 | } |
| 191 | foreach ( $this->query->functions as $function ) { |
| 192 | if ( $function->name == $needFeedback['name'] ) { |
| 193 | $needFeedback['function'] = $function; |
| 194 | break; |
| 195 | } |
| 196 | } |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | $this->needFeedbacks = $needFeedbacks; |
| 201 | } |
| 202 | |
| 203 | // It's text completion |
| 204 | else if ( isset( $choice['text'] ) ) { |
| 205 | |
| 206 | // TODO: Assistants return an array (so actually not really a text completion) |
| 207 | // We should probably make this clearer and analyze all the outputs from different endpoints. |
| 208 | if ( is_array( $choice['text'] ) ) { |
| 209 | $text = trim( $choice['text']['value'] ); |
| 210 | $this->results[] = $text; |
| 211 | $this->result = $text; |
| 212 | } |
| 213 | else { |
| 214 | $text = trim( $choice['text'] ); |
| 215 | $this->results[] = $text; |
| 216 | $this->result = $text; |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | // It's url/image |
| 221 | else if ( isset( $choice['url'] ) ) { |
| 222 | $url = trim( $choice['url'] ); |
| 223 | $this->results[] = $url; |
| 224 | $this->result = $url; |
| 225 | } |
| 226 | |
| 227 | // It's embedding |
| 228 | else if ( isset( $choice['embedding'] ) ) { |
| 229 | $content = $choice['embedding']; |
| 230 | $this->results[] = $content; |
| 231 | $this->result = $content; |
| 232 | } |
| 233 | } |
| 234 | } |
| 235 | else { |
| 236 | $this->result = $choices; |
| 237 | $this->results[] = $choices; |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | public function toJson() { |
| 242 | return json_encode( $this ); |
| 243 | } |
| 244 | } |