PluginProbe
WowOptin: Next-Gen Popup Maker – Create Stunning Popups and Optins for Lead Generation / trunk
WowOptin: Next-Gen Popup Maker – Create Stunning Popups and Optins for Lead Generation vtrunk
1.4.48 1.4.47 1.4.46 1.4.45 1.4.44 1.4.43 1.4.42 1.4.41 1.4.40 1.4.39 1.4.38 1.4.37 1.4.36 1.1.2 1.2.0 1.2.1 1.2.2 1.3.0 1.3.1 1.4.0 1.4.1 1.4.10 1.4.11 1.4.12 1.4.13 All 64 releases
optin / includes / integrations / implementations / class-grok.php

class-grok.php in WowOptin: Next-Gen Popup Maker – Create Stunning Popups and Optins for Lead Generation trunk, at includes/integrations/implementations/class-grok.php

106 lines 2.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php // phpcs:ignore
2
3 namespace OPTN\Includes\Integrations\Implementations;
4
5 use OPTN\Includes\Dto\ModelInput;
6 use OPTN\Includes\Integrations\Base\BaseAiIntegration;
7 use WP_Error;
8
9 defined( 'ABSPATH' ) || exit;
10
11 /**
12 * Grok integration
13 */
14 class Grok extends BaseAiIntegration {
15
16
17 /**
18 * Generate Text
19 *
20 * @see https://docs.x.ai/docs/guides/chat
21 *
22 * @param ModelInput $input model input.
23 * @return string
24 */
25 public function generate_text( ModelInput $input ) {
26 $endpoint = 'https://api.x.ai/v1/chat/completions';
27
28 $prompt = $this->get_processed_user_prompt( $input );
29
30 if ( is_null( $prompt ) ) {
31 return new WP_Error( 'generate_text_error', 'Invalid prompt' );
32 }
33
34 $body = wp_json_encode(
35 array(
36 'model' => $this->settings['model'],
37 'messages' => array(
38 array(
39 'role' => 'system',
40 'content' => $this->system_prompt,
41 ),
42 array(
43 'role' => 'user',
44 'content' => $prompt,
45 ),
46 ),
47 'stream' => false,
48 )
49 );
50
51 $response = wp_remote_post(
52 $endpoint,
53 array(
54 'headers' => array(
55 'Content-Type' => 'application/json',
56 'Authorization' => 'Bearer ' . $this->settings['apiKey'],
57 ),
58 'body' => $body,
59 'timeout' => 60,
60 )
61 );
62
63 if ( is_wp_error( $response ) ) {
64 return $response;
65 }
66
67 $code = wp_remote_retrieve_response_code( $response );
68 $data = json_decode( wp_remote_retrieve_body( $response ), true );
69
70 if ( 200 !== $code || ! isset( $data['choices'][0]['message']['content'] ) ) {
71 return new WP_Error( 'generate_text_error', 'Unexpected API response.', $data );
72 }
73
74 $result = trim( $data['choices'][0]['message']['content'] );
75
76 $result = $this->maybe_remove_quotes( $result );
77
78 return $result;
79 }
80
81 /**
82 * Get models.
83 *
84 * @see https://docs.x.ai/docs/models
85 *
86 * @return array
87 */
88 public static function get_models() {
89
90 return array(
91 array(
92 'value' => 'grok-3-latest',
93 'label' => 'Grok 3',
94 'input' => array( 'text' ),
95 'output' => array( 'text' ),
96 ),
97 array(
98 'value' => 'grok-3-mini-latest',
99 'label' => 'Grok 3 Mini',
100 'input' => array( 'text' ),
101 'output' => array( 'text' ),
102 ),
103 );
104 }
105 }
106