PluginProbe
WPBot – AI ChatBot for Live Support, Lead Generation, WordPress Automation, AI Services / 8.8.0
WPBot – AI ChatBot for Live Support, Lead Generation, WordPress Automation, AI Services v8.8.0
8.8.0 8.7.9 8.7.8 8.7.7 8.7.6 8.7.5 8.7.4 8.7.3 8.7.2 8.7.1 8.7.0 8.6.9 8.6.8 8.6.7 8.6.6 8.6.5 8.6.4 8.6.2 8.6.1 8.6.0 8.5.9 8.5.8 8.5.7 8.5.6 8.5.5 All 536 releases
chatbot / addons / automator / includes / actions / mistral-actions.php

mistral-actions.php in WPBot – AI ChatBot for Live Support, Lead Generation, WordPress Automation, AI Services 8.8.0, at addons/automator/includes/actions/mistral-actions.php

180 lines 4.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Mistral Actions
4 *
5 * @package WPbot_Automator
6 */
7
8 namespace WPbot_Automator\Actions;
9
10 if ( ! defined( 'ABSPATH' ) ) {
11 exit;
12 }
13
14 /**
15 * Class Mistral_Actions
16 */
17 class Mistral_Actions extends Action {
18
19 /**
20 * Constructor
21 */
22 public function __construct() {
23 $this->id = 'mistral';
24 $this->group = __( 'Mistral AI', 'wpbot-automator' );
25 }
26
27 /**
28 * Execute the action
29 */
30 public function execute( $action_data, $trigger_data ) {
31 $action_id = isset( $action_data['actionId'] ) ? $action_data['actionId'] : '';
32
33 if ( empty( $action_id ) ) {
34 return false;
35 }
36
37 switch ( $action_id ) {
38 case 'chat_completion':
39 return $this->chat_completion( $action_data, $trigger_data );
40 case 'test_connection':
41 return $this->test_connection( $action_data, $trigger_data );
42 default:
43 return false;
44 }
45 }
46
47 /**
48 * Test Connection
49 */
50 private function test_connection( $config, $trigger_data ) {
51 $config_values = isset( $config['config'] ) ? $config['config'] : array();
52 $api_key = isset( $config_values['api_key'] ) ? $this->parse_tokens( $config_values['api_key'], $trigger_data ) : '';
53
54 if ( empty( $api_key ) ) {
55 return array(
56 'success' => false,
57 'message' => 'API Key is required.',
58 );
59 }
60
61 $url = 'https://api.mistral.ai/v1/models';
62
63 $args = array(
64 'method' => 'GET',
65 'headers' => array(
66 'Authorization' => 'Bearer ' . $api_key,
67 ),
68 'timeout' => 30,
69 );
70
71 $response = wp_remote_request( $url, $args );
72
73 if ( is_wp_error( $response ) ) {
74 return array(
75 'success' => false,
76 'message' => $response->get_error_message(),
77 );
78 }
79
80 $response_code = wp_remote_retrieve_response_code( $response );
81
82 if ( $response_code >= 200 && $response_code < 300 ) {
83 return array(
84 'success' => true,
85 'message' => 'Connection successful! Your API key is valid.',
86 );
87 } else {
88 $response_body = wp_remote_retrieve_body( $response );
89 $data = json_decode( $response_body, true );
90 $error_message = isset( $data['error']['message'] ) ? $data['error']['message'] : 'Connection failed. Please check your API key.';
91 return array(
92 'success' => false,
93 'message' => $error_message,
94 'code' => $response_code,
95 );
96 }
97 }
98
99 /**
100 * Chat Completion
101 */
102 private function chat_completion( $config, $trigger_data ) {
103 $config_values = isset( $config['config'] ) ? $config['config'] : array();
104
105 $api_key = isset( $config_values['api_key'] ) ? $this->parse_tokens( $config_values['api_key'], $trigger_data ) : '';
106 $model = isset( $config_values['model'] ) ? $config_values['model'] : 'mistral-tiny';
107 $prompt = isset( $config_values['prompt'] ) ? $this->parse_tokens( $config_values['prompt'], $trigger_data ) : '';
108 $max_tokens = isset( $config_values['max_tokens'] ) ? intval( $config_values['max_tokens'] ) : 1000;
109 $temperature = isset( $config_values['temperature'] ) ? floatval( $config_values['temperature'] ) : 0.7;
110
111 if ( empty( $api_key ) ) {
112 return array(
113 'success' => false,
114 'message' => 'API Key is required.',
115 );
116 }
117
118 if ( empty( $prompt ) ) {
119 return array(
120 'success' => false,
121 'message' => 'Prompt is required.',
122 );
123 }
124
125 $url = 'https://api.mistral.ai/v1/chat/completions';
126
127 $body = array(
128 'model' => $model,
129 'messages' => array(
130 array(
131 'role' => 'user',
132 'content' => $prompt,
133 ),
134 ),
135 'max_tokens' => $max_tokens,
136 'temperature' => $temperature,
137 );
138
139 $args = array(
140 'method' => 'POST',
141 'headers' => array(
142 'Content-Type' => 'application/json',
143 'Authorization' => 'Bearer ' . $api_key,
144 ),
145 'body' => wp_json_encode( $body ),
146 'timeout' => 60,
147 );
148
149 $response = wp_remote_request( $url, $args );
150
151 if ( is_wp_error( $response ) ) {
152 return array(
153 'success' => false,
154 'message' => $response->get_error_message(),
155 );
156 }
157
158 $response_code = wp_remote_retrieve_response_code( $response );
159 $response_body = wp_remote_retrieve_body( $response );
160 $data = json_decode( $response_body, true );
161
162 if ( $response_code >= 200 && $response_code < 300 ) {
163 $content = isset( $data['choices'][0]['message']['content'] ) ? $data['choices'][0]['message']['content'] : '';
164 return array(
165 'success' => true,
166 'content' => $content,
167 'response' => $content,
168 'data' => $data,
169 );
170 } else {
171 $error_message = isset( $data['error']['message'] ) ? $data['error']['message'] : 'Unknown error';
172 return array(
173 'success' => false,
174 'message' => $error_message,
175 'code' => $response_code,
176 );
177 }
178 }
179 }
180