PluginProbe
WPBot – AI ChatBot for Live Support, Lead Generation, WordPress Automation, AI Services / 7.1.5
WPBot – AI ChatBot for Live Support, Lead Generation, WordPress Automation, AI Services v7.1.5
8.8.4 8.8.3 8.8.2 8.8.1 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 All 540 releases
chatbot / includes / integration / openai / qcld_wp_OpenAI.php

qcld_wp_OpenAI.php in WPBot – AI ChatBot for Live Support, Lead Generation, WordPress Automation, AI Services 7.1.5, at includes/integration/openai/qcld_wp_OpenAI.php

110 lines 4.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 include_once(ABSPATH . 'wp-includes/pluggable.php');
3 if(!class_exists('qcld_wp_OpenAI')){
4 class qcld_wp_OpenAI{
5 public $baseURL = "https://api.openai.com/v1/";
6 private $defaultEngine = "davinci";
7 public function setDefaultEngine($defaultEngine){
8 $this->defaultEngine = $defaultEngine;
9 }
10
11 public function get_response($postFields){
12 $url = "https://api.openai.com/v1/completions";
13 $apt_key = "Authorization: Bearer ". get_option('open_ai_api_key');
14
15 $curl = curl_init($url);
16 curl_setopt($curl, CURLOPT_URL, $url);
17 curl_setopt($curl, CURLOPT_POST, true);
18 curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
19
20 $headers = array(
21 "Content-Type: application/json",
22 $apt_key ,
23 );
24 curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
25
26
27 curl_setopt($curl, CURLOPT_POSTFIELDS, $postFields);
28
29 curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
30 $response = curl_exec($curl);
31 curl_close($curl);
32
33
34 return json_decode($response);
35 }
36 public function complete($prompt) {
37 $max_tokens = (int)get_option( 'openai_max_tokens');
38 $temp = (float)get_option( 'openai_temperature');
39 $frequency_penalty = (float)get_option( 'frequency_penalty');
40 $presence_penalty = (float)get_option( 'presence_penalty');
41 $request_body = [
42 "prompt" => $prompt,
43 "model" => get_option( 'openai_engines'),
44 "max_tokens" => $max_tokens,
45 "temperature" => $temp,
46 "top_p" => 1,
47 "presence_penalty" => $presence_penalty,
48 "frequency_penalty"=> $frequency_penalty,
49 "best_of"=> 1,
50 "stream" => false,
51 ];
52 $postFields = json_encode($request_body);
53 $result = self::get_response($postFields);
54 return json_encode($result);
55 }
56 public function gptcomplete($keyword){
57 $ch = curl_init();
58 $url = 'https://api.openai.com/v1/responses';
59 $api_key = get_option('open_ai_api_key');
60
61 $post_fields = array(
62 "model" => get_option( 'openai_engines'),
63 "input" => $keyword,
64 // "max_tokens" => $max_tokens,
65 "temperature" => 0
66 );
67 $header = [
68 'Content-Type: application/json',
69 'Authorization: Bearer ' . $api_key
70 ];
71 curl_setopt($ch, CURLOPT_URL, $url);
72 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
73 curl_setopt($ch, CURLOPT_POST, 1);
74 curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post_fields));
75 curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
76 $result = curl_exec($ch);
77 if (curl_errno($ch)) {
78 // phpcs:ignore
79 echo 'Error: ' . curl_error($ch);
80 }
81 curl_close($ch);
82 // $response = json_decode($result);
83 return $result;
84 }
85 public function models_list(){
86 $api_key = get_option('open_ai_api_key');
87 $url = 'https://api.openai.com/v1/models';
88
89 $ch = curl_init();
90 curl_setopt($ch, CURLOPT_URL, $url);
91 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
92 curl_setopt($ch, CURLOPT_HTTPHEADER, array(
93 'Authorization: Bearer ' . $api_key
94 ));
95
96 $result = curl_exec($ch);
97
98 if (curl_errno($ch)) {
99 // phpcs:ignore
100 echo 'Error: ' . curl_error($ch);
101 curl_close($ch);
102 return false;
103 }
104
105 curl_close($ch);
106
107 return $result;
108 }
109 }
110 }