PluginProbe
AI Engine – The Chatbot, AI Framework & MCP for WordPress / 1.6.81
AI Engine – The Chatbot, AI Framework & MCP for WordPress v1.6.81
3.7.7 3.7.6 3.7.5 3.7.4 3.7.3 3.7.2 3.7.1 3.7.0 3.6.9 3.6.8 3.6.7 3.6.6 3.6.4 3.6.5 3.6.3 3.6.2 3.6.1 3.6.0 3.5.9 3.5.8 3.5.7 3.5.6 3.5.5 3.5.4 3.5.3 All 527 releases
ai-engine / classes / reply.php

reply.php in AI Engine – The Chatbot, AI Framework & MCP for WordPress 1.6.81, at classes/reply.php

132 lines 3.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 public function __construct( $query = null ) {
12 $this->query = $query;
13 }
14
15 public function jsonSerialize() {
16 return [
17 'class' => get_class( $this ),
18 'result' => $this->result,
19 'results' => $this->results,
20 'usage' => $this->usage
21 ];
22 }
23
24 public function setQuery( $query ) {
25 $this->query = $query;
26 }
27
28 public function setUsage( $usage ) {
29 $this->usage = $usage;
30 }
31
32 public function setType( $type ) {
33 $this->type = $type;
34 }
35
36 public function getTotalTokens() {
37 return $this->usage['total_tokens'];
38 }
39
40 public function getPromptTokens() {
41 return $this->usage['prompt_tokens'];
42 }
43
44 public function getCompletionTokens() {
45 return $this->usage['completion_tokens'];
46 }
47
48 public function getUnits() {
49 if ( isset( $this->usage['total_tokens'] ) ) {
50 return $this->usage['total_tokens'];
51 }
52 else if ( isset( $this->usage['images'] ) ) {
53 return $this->usage['images'];
54 }
55 else if ( isset( $this->usage['seconds'] ) ) {
56 return $this->usage['seconds'];
57 }
58 return null;
59 }
60
61 public function getResults() {
62 return $this->results;
63 }
64
65 public function getUsage() {
66 return $this->usage;
67 }
68
69 public function getResult() {
70 return $this->result;
71 }
72
73 public function getType() {
74 return $this->type;
75 }
76
77 public function replace( $search, $replace ) {
78 $this->result = str_replace( $search, $replace, $this->result );
79 $this->results = array_map( function( $result ) use ( $search, $replace ) {
80 return str_replace( $search, $replace, $result );
81 }, $this->results );
82 }
83
84 /**
85 * Set the choices from OpenAI as the results.
86 * The last (or only) result is set as the result.
87 * @param array $choices ID of the model to use.
88 */
89 public function setChoices( $choices ) {
90 $this->results = [];
91 if ( is_array( $choices ) ) {
92 foreach ( $choices as $choice ) {
93
94 // It's chat completion
95 if ( isset( $choice['message'] ) ) {
96 $content = trim( $choice['message']['content'] );
97 $this->results[] = $content;
98 $this->result = $content;
99 }
100
101 // It's text completion
102 if ( isset( $choice['text'] ) ) {
103 $text = trim( $choice['text'] );
104 $this->results[] = $text;
105 $this->result = $text;
106 }
107
108 // It's url/image
109 if ( isset( $choice['url'] ) ) {
110 $url = trim( $choice['url'] );
111 $this->results[] = $url;
112 $this->result = $url;
113 }
114
115 // It's embedding
116 if ( isset( $choice['embedding'] ) ) {
117 $content = $choice['embedding'];
118 $this->results[] = $content;
119 $this->result = $content;
120 }
121 }
122 }
123 else {
124 $this->result = $choices;
125 $this->results[] = $choices;
126 }
127 }
128
129 public function toJson() {
130 return json_encode( $this );
131 }
132 }