PluginProbe
WPBot – AI ChatBot for Live Support, Lead Generation, WordPress Automation, AI Services / 7.6.0
WPBot – AI ChatBot for Live Support, Lead Generation, WordPress Automation, AI Services v7.6.0
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 8.5.4 8.5.3 All 534 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.6.0, at includes/integration/openai/qcld_wp_OpenAI.php

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