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
3 years ago
reply.php
2 years ago
rest.php
2 years ago
reply.php
164 lines
| 1 | <?php |
| 2 | |
| 3 | class Meow_MWAI_Reply implements JsonSerializable { |
| 4 | public $id = null; |
| 5 | public $result = ''; |
| 6 | public $results = []; |
| 7 | public $usage = [ 'prompt_tokens' => 0, 'completion_tokens' => 0, 'total_tokens' => 0 ]; |
| 8 | public $query = null; |
| 9 | public $type = 'text'; |
| 10 | |
| 11 | // Function Call |
| 12 | public $functionCall = null; |
| 13 | |
| 14 | public function __construct( $query = null ) { |
| 15 | $this->query = $query; |
| 16 | } |
| 17 | |
| 18 | #[\ReturnTypeWillChange] |
| 19 | public function jsonSerialize() { |
| 20 | return [ |
| 21 | 'result' => $this->result, |
| 22 | 'results' => $this->results, |
| 23 | 'usage' => $this->usage, |
| 24 | 'system' => [ |
| 25 | 'class' => get_class( $this ), |
| 26 | ] |
| 27 | ]; |
| 28 | } |
| 29 | |
| 30 | public function set_usage( $usage ) { |
| 31 | $this->usage = $usage; |
| 32 | } |
| 33 | |
| 34 | public function set_id( $id ) { |
| 35 | $this->id = $id; |
| 36 | } |
| 37 | |
| 38 | public function set_type( $type ) { |
| 39 | $this->type = $type; |
| 40 | } |
| 41 | |
| 42 | public function get_total_tokens() { |
| 43 | return $this->usage['total_tokens']; |
| 44 | } |
| 45 | |
| 46 | public function get_in_tokens( $query = null ) { |
| 47 | $in_tokens = $this->usage['prompt_tokens']; |
| 48 | if ( empty( $in_tokens ) && $query ) { |
| 49 | $in_tokens = $query->get_in_tokens(); |
| 50 | } |
| 51 | return $in_tokens; |
| 52 | } |
| 53 | |
| 54 | public function get_out_tokens() { |
| 55 | $out_tokens = $this->usage['completion_tokens']; |
| 56 | if ( empty( $out_tokens ) ) { |
| 57 | $out_tokens = Meow_MWAI_Core::estimate_tokens( $this->result ); |
| 58 | } |
| 59 | return $out_tokens; |
| 60 | } |
| 61 | |
| 62 | public function get_units() { |
| 63 | if ( isset( $this->usage['total_tokens'] ) ) { |
| 64 | return $this->usage['total_tokens']; |
| 65 | } |
| 66 | else if ( isset( $this->usage['images'] ) ) { |
| 67 | return $this->usage['images']; |
| 68 | } |
| 69 | else if ( isset( $this->usage['seconds'] ) ) { |
| 70 | return $this->usage['seconds']; |
| 71 | } |
| 72 | return null; |
| 73 | } |
| 74 | |
| 75 | public function get_type() { |
| 76 | return $this->type; |
| 77 | } |
| 78 | |
| 79 | public function set_reply( $reply ) { |
| 80 | $this->result = $reply; |
| 81 | $this->results[] = [ $reply ]; |
| 82 | } |
| 83 | |
| 84 | public function replace( $search, $replace ) { |
| 85 | $this->result = str_replace( $search, $replace, $this->result ); |
| 86 | $this->results = array_map( function( $result ) use ( $search, $replace ) { |
| 87 | return str_replace( $search, $replace, $result ); |
| 88 | }, $this->results ); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Set the choices from OpenAI as the results. |
| 93 | * The last (or only) result is set as the result. |
| 94 | * @param array $choices ID of the model to use. |
| 95 | */ |
| 96 | public function set_choices( $choices ) { |
| 97 | $this->results = []; |
| 98 | if ( is_array( $choices ) ) { |
| 99 | foreach ( $choices as $choice ) { |
| 100 | |
| 101 | // It's chat completion |
| 102 | if ( isset( $choice['message'] ) ) { |
| 103 | |
| 104 | // It's text content |
| 105 | if ( isset( $choice['message']['content'] ) ) { |
| 106 | $content = trim( $choice['message']['content'] ); |
| 107 | $this->results[] = $content; |
| 108 | $this->result = $content; |
| 109 | } |
| 110 | |
| 111 | // It's a function call |
| 112 | if ( isset( $choice['message']['function_call'] ) ) { |
| 113 | $content = $choice['message']['function_call']; |
| 114 | $name = trim( $content['name'] ); |
| 115 | $arguments = trim( str_replace( "\n", "", $content['arguments'] ) ); |
| 116 | if ( substr( $arguments, 0, 1 ) == '{' ) { |
| 117 | $arguments = json_decode( $arguments, true ); |
| 118 | } |
| 119 | $this->functionCall = [ 'name' => $name, 'arguments' => $arguments ]; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | // It's text completion |
| 124 | else if ( isset( $choice['text'] ) ) { |
| 125 | |
| 126 | // TODO: Assistants return an array (so actually not really a text completion) |
| 127 | // We should probably make this clearer and analyze all the outputs from different endpoints. |
| 128 | if ( is_array( $choice['text'] ) ) { |
| 129 | $text = trim( $choice['text']['value'] ); |
| 130 | $this->results[] = $text; |
| 131 | $this->result = $text; |
| 132 | } |
| 133 | else { |
| 134 | $text = trim( $choice['text'] ); |
| 135 | $this->results[] = $text; |
| 136 | $this->result = $text; |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | // It's url/image |
| 141 | else if ( isset( $choice['url'] ) ) { |
| 142 | $url = trim( $choice['url'] ); |
| 143 | $this->results[] = $url; |
| 144 | $this->result = $url; |
| 145 | } |
| 146 | |
| 147 | // It's embedding |
| 148 | else if ( isset( $choice['embedding'] ) ) { |
| 149 | $content = $choice['embedding']; |
| 150 | $this->results[] = $content; |
| 151 | $this->result = $content; |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | else { |
| 156 | $this->result = $choices; |
| 157 | $this->results[] = $choices; |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | public function toJson() { |
| 162 | return json_encode( $this ); |
| 163 | } |
| 164 | } |