searchterm ) ) { $searchterm = sanitize_text_field( $body->searchterm ); } $response = __( "I'm afraid that I don't know how to answer that? Try asking it a different way.", 'ai-chatbot-easy-integration' ); $url = 'https://api.openai.com/v1/chat/completions'; $ch = curl_init(); $data = '{ "model": "gpt-3.5-turbo", "messages": [{"role": "user", "content": "' . esc_attr( $searchterm ) . '"}], "temperature": 0.7 }'; curl_setopt( $ch, CURLOPT_POSTFIELDS, $data ); curl_setopt( $ch, CURLOPT_URL, $url ); curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 ); curl_setopt( $ch, CURLOPT_ENCODING, '' ); curl_setopt( $ch, CURLOPT_MAXREDIRS, 10 ); curl_setopt( $ch, CURLOPT_TIMEOUT, 0 ); curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true ); curl_setopt( $ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1 ); curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, 'POST' ); curl_setopt( $ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json', 'apikey: ' . esc_attr( $openai_api_key ), 'Authorization: Bearer ' . esc_attr( $openai_api_key ), ) ); $result = curl_exec( $ch ); $code = curl_getinfo( $ch, CURLINFO_HTTP_CODE ); $error = curl_error( $ch ); if ( 200 == $code || 201 == $code ) { $json = json_decode( $result, true ); if ( isset( $json['choices'] ) ) { if ( count( $json['choices'] ) > 0 && isset( $json['choices'][0]['message']['content'] ) && 11 != $json['choices'][0]['message']['content'] ) { $response = esc_attr( $json['choices'][0]['message']['content'] ); } } $message = __( 'OpenAI Call Successful: ', 'ai-chatbot-easy-integration' ) . esc_attr( $response ); if ( function_exists( 'SimpleLogger' ) ) { SimpleLogger()->info( $message ); } } else { $message = __( 'OpenAI Call Failed', 'ai-chatbot-easy-integration' ) . esc_attr( $error ); if ( function_exists( 'SimpleLogger' ) ) { SimpleLogger()->info( $message ); } } return $response; }