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