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