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