modules
3 years ago
admin.php
3 years ago
ai.php
3 years ago
answer.php
3 years ago
api.php
3 years ago
core.php
3 years ago
init.php
3 years ago
openai.php
3 years ago
query.php
3 years ago
queryembed.php
3 years ago
queryimage.php
3 years ago
querytext.php
3 years ago
querytranscribe.php
3 years ago
rest.php
3 years ago
security.php
3 years ago
answer.php
110 lines
| 1 | <?php |
| 2 | |
| 3 | class Meow_MWAI_Answer 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 | |
| 10 | public function __construct( $query = null ) { |
| 11 | $this->query = $query; |
| 12 | } |
| 13 | |
| 14 | public function jsonSerialize() { |
| 15 | return [ |
| 16 | 'class' => get_class( $this ), |
| 17 | 'result' => $this->result, |
| 18 | 'results' => $this->results, |
| 19 | 'usage' => $this->usage |
| 20 | ]; |
| 21 | } |
| 22 | |
| 23 | public function setQuery( $query ) { |
| 24 | $this->query = $query; |
| 25 | } |
| 26 | |
| 27 | public function setUsage( $usage ) { |
| 28 | $this->usage = $usage; |
| 29 | } |
| 30 | |
| 31 | public function getTotalTokens() { |
| 32 | return $this->usage['total_tokens']; |
| 33 | } |
| 34 | |
| 35 | public function getPromptTokens() { |
| 36 | return $this->usage['prompt_tokens']; |
| 37 | } |
| 38 | |
| 39 | public function getCompletionTokens() { |
| 40 | return $this->usage['completion_tokens']; |
| 41 | } |
| 42 | |
| 43 | public function getResults() { |
| 44 | return $this->results; |
| 45 | } |
| 46 | |
| 47 | public function getUsage() { |
| 48 | return $this->usage; |
| 49 | } |
| 50 | |
| 51 | public function getResult() { |
| 52 | return $this->result; |
| 53 | } |
| 54 | |
| 55 | public function replace( $search, $replace ) { |
| 56 | $this->result = str_replace( $search, $replace, $this->result ); |
| 57 | $this->results = array_map( function( $result ) use ( $search, $replace ) { |
| 58 | return str_replace( $search, $replace, $result ); |
| 59 | }, $this->results ); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Set the choices from OpenAI as the results. |
| 64 | * The last (or only) result is set as the result. |
| 65 | * @param array $choices ID of the model to use. |
| 66 | */ |
| 67 | public function setChoices( $choices ) { |
| 68 | $this->results = []; |
| 69 | if ( is_array( $choices ) ) { |
| 70 | foreach ( $choices as $choice ) { |
| 71 | |
| 72 | // It's chat completion |
| 73 | if ( isset( $choice['message'] ) ) { |
| 74 | $content = trim( $choice['message']['content'] ); |
| 75 | $this->results[] = $content; |
| 76 | $this->result = $content; |
| 77 | } |
| 78 | |
| 79 | // It's text completion |
| 80 | if ( isset( $choice['text'] ) ) { |
| 81 | $text = trim( $choice['text'] ); |
| 82 | $this->results[] = $text; |
| 83 | $this->result = $text; |
| 84 | } |
| 85 | |
| 86 | // It's url/image |
| 87 | if ( isset( $choice['url'] ) ) { |
| 88 | $url = trim( $choice['url'] ); |
| 89 | $this->results[] = $url; |
| 90 | $this->result = $url; |
| 91 | } |
| 92 | |
| 93 | // It's embedding |
| 94 | if ( isset( $choice['embedding'] ) ) { |
| 95 | $content = $choice['embedding']; |
| 96 | $this->results[] = $content; |
| 97 | $this->result = $content; |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | else { |
| 102 | $this->result = $choices; |
| 103 | $this->results[] = $choices; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | public function toJson() { |
| 108 | return json_encode( $this ); |
| 109 | } |
| 110 | } |