PluginProbe
WPBot – AI ChatBot for Live Support, Lead Generation, WordPress Automation, AI Services / 8.6.0
WPBot – AI ChatBot for Live Support, Lead Generation, WordPress Automation, AI Services v8.6.0
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 8.5.2 All 533 releases
chatbot / includes / openai / qcld_wp_OpenAI.php

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

82 lines 2.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 $api_key = get_option('open_ai_api_key');
15
16 $response = wp_remote_post(
17 $url,
18 array(
19 'headers' => array(
20 'Content-Type' => 'application/json',
21 'Authorization' => 'Bearer ' . $api_key,
22 ),
23 'body' => $postFields,
24 'timeout' => 60,
25 )
26 );
27
28 if ( is_wp_error( $response ) ) {
29 return null;
30 }
31
32 return json_decode( wp_remote_retrieve_body( $response ) );
33 }
34 public function complete($prompt) {
35 $max_tokens = (int)get_option( 'openai_max_tokens');
36 $temp = (float)get_option( 'openai_temperature');
37 $frequency_penalty = (float)get_option( 'frequency_penalty');
38 $presence_penalty = (float)get_option( 'presence_penalty');
39 $request_body = [
40 "prompt" => $prompt,
41 "model" => get_option( 'openai_engines'),
42 "max_tokens" => $max_tokens,
43 "temperature" => $temp,
44 "top_p" => 1,
45 "presence_penalty" => $presence_penalty,
46 "frequency_penalty"=> $frequency_penalty,
47 "best_of"=> 1,
48 "stream" => false,
49 ];
50 $postFields = wp_json_encode($request_body);
51 $result = self::get_response($postFields);
52 return wp_json_encode($result);
53 }
54 public function gptcomplete($keyword){
55 $url = 'https://api.openai.com/v1/responses';
56 $api_key = get_option('open_ai_api_key');
57
58 $response = wp_remote_post(
59 $url,
60 array(
61 'headers' => array(
62 'Content-Type' => 'application/json',
63 'Authorization' => 'Bearer ' . $api_key,
64 ),
65 'body' => wp_json_encode( array(
66 'model' => get_option( 'openai_engines'),
67 'input' => $keyword,
68 'temperature' => 0,
69 ) ),
70 'timeout' => 60,
71 )
72 );
73
74 if ( is_wp_error( $response ) ) {
75 return '';
76 }
77
78 return wp_remote_retrieve_body( $response );
79 }
80 }
81 }
82