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
ui.php
3 years ago
answer.php
77 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 replace( $search, $replace ) { |
| 27 | $this->result = str_replace( $search, $replace, $this->result ); |
| 28 | $this->results = array_map( function( $result ) use ( $search, $replace ) { |
| 29 | return str_replace( $search, $replace, $result ); |
| 30 | }, $this->results ); |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Set the choices from OpenAI as the results. |
| 35 | * The last (or only) result is set as the result. |
| 36 | * @param array $choices ID of the model to use. |
| 37 | */ |
| 38 | public function setChoices( $choices ) { |
| 39 | $this->results = []; |
| 40 | if ( is_array( $choices ) ) { |
| 41 | foreach ( $choices as $choice ) { |
| 42 | |
| 43 | // It's chat completion |
| 44 | if ( isset( $choice['message'] ) ) { |
| 45 | $content = trim( $choice['message']['content'] ); |
| 46 | $this->results[] = $content; |
| 47 | $this->result = $content; |
| 48 | } |
| 49 | |
| 50 | // It's text completion |
| 51 | if ( isset( $choice['text'] ) ) { |
| 52 | $text = trim( $choice['text'] ); |
| 53 | $this->results[] = $text; |
| 54 | $this->result = $text; |
| 55 | } |
| 56 | |
| 57 | // It's url/image |
| 58 | if ( isset( $choice['url'] ) ) { |
| 59 | $url = trim( $choice['url'] ); |
| 60 | $this->results[] = $url; |
| 61 | $this->result = $url; |
| 62 | } |
| 63 | |
| 64 | // It's embedding |
| 65 | if ( isset( $choice['embedding'] ) ) { |
| 66 | $content = $choice['embedding']; |
| 67 | $this->results[] = $content; |
| 68 | $this->result = $content; |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | else { |
| 73 | $this->result = $choices; |
| 74 | $this->results[] = $choices; |
| 75 | } |
| 76 | } |
| 77 | } |