• Как интегрировать ChatGPT в WordPress?

    artzolin
    @artzolin Куратор тега WordPress
    php, WordPress разработка сайтов artzolin.ru
    Да, на сайте есть документация по API https://platform.openai.com/docs/guides/gpt

    Вот пример запроса и работы функции. 100% работу не гарантирую, т.к. несколько раз переписывал функцию, а у них выходят новые модели и изменения в запросах API, поэтому вам нужно будет все проверить и сделать настройки передаваемых аргументов под конкретную задачу

    if ( ! function_exists( 'get_chatgpt_responce' ) ) {
    
    	/**
    	 * Returns query result or error
    	 *
    	 * @param string $question Text API question.
    	 * @param string $promt    Text API promt (for chat).
    	 * @param string $model    API model.
    	 * @param string $api_key  API key.
    	 *
    	 * @return void
    	 */
    	function get_chatgpt_responce( $question = null, $promt = null, $model = 'gpt-3.5-turbo', $api_key = 'sk-le000000000000000000000000000000000000' ) {
    
    		if ( is_null( $question ) ) {
    			return null;
    		}
    
    		$type = 'text';
    
    		if ( $model === 'davinci-codex' ) {
    			$type = 'codex';
    		}
    
    		if ( in_array( $model, array( 'gpt-4', 'gpt-4-0314', 'gpt-4-32k', 'gpt-4-32k-0314', 'gpt-3.5-turbo', 'gpt-3.5-turbo-0301' ), true ) ) {
    			$type = 'chat';
    		}
    
    		$headers = "Content-Type: application/json\r\n" . "Authorization: Bearer " . $api_key . "\r\n";
    
    		$params = array(
    			'model'       => $model,
    			'temperature' => 0.7,
    			'top_p'       => 1,
    		);
    
    		if ( $type === 'chat' ) {
    			$url         = 'https://api.openai.com/v1/chat/completions';
    			$params['n'] = 1;
    			if ( ! is_null( $promt ) ) {
    				$params['messages'][] = array(
    					'role'    => 'system',
    					'content' => $promt,
    				);
    			}
    			$params['messages'][] = array(
    				'role'    => 'user',
    				'content' => $question,
    			);
    		} elseif( $type === 'codex' ) {
    			$url                  = 'https://api.openai.com/v1/engines/davinci-codex/completions';
    			$params['prompt']     = $question;
    			$params['max_tokens'] = 4096 - iconv_strlen( $question );
    		} else {
    			$url                  = 'https://api.openai.com/v1/completions';
    			$params['prompt']     = $question;
    			$params['max_tokens'] = 4096 - iconv_strlen( $question );
    		}
    
    		$args = array(
    			'method'       => 'POST',
    			'timeout'      => 240,
    			'redirection'  => 5,
    			'httpversion'  => '1.0',
    			'blocking'     => true,
    			'headers'      => $headers,
    			'body'         => json_encode( $params ),
    			'sslverify'    => true,
    		);
    
    		$response = wp_remote_request( $url, $args );
    
    		if ( is_wp_error( $response ) ) {
    			$notification = $response->get_error_message();
    		} elseif ( in_array( wp_remote_retrieve_response_code( $response ), array( 400, 401, 403, 404 ), true ) ) {
    			$body         = json_decode( wp_remote_retrieve_body( $response ), true );
    			$notification = $body['error']['message'];
    		} elseif ( wp_remote_retrieve_response_code( $response ) === 200 ) {
    			$body         = json_decode( wp_remote_retrieve_body( $response ), true );
    			$notification = 'Расход GPT токенов: ' . $body['usage']['total_tokens'] . '. Причина остановки: ' . $body['choices'][0]['finish_reason'];
    
    			if ( $type === 'chat' ) {
    				return $body['choices'][0]['message']['content'];
    			} else {
    				return $body['choices'][0]['text'];
    			}
    		}
    
    		if ( function_exists( 'telegram_notification' ) ) {
    			telegram_notification( $notification );
    		}
    
    		return false;
    	}
    }
    
    $query   = 'Write a list frequently asked questions about the get_the_title() WordPress function.';
    $chatgpt = get_chatgpt_responce( $query );
    var_dump( $chatgpt );
    Ответ написан
    Комментировать
  • Когда будет включен webdav на Cloud@Mail.Ru?

    Wave
    @Wave
    На два мылорушных ящика мне сегодня пришло письмо из суппорта
    Здравствуйте.

    Ранее Вы интересовались функционалом WebDAV в проекте Облако@mail.ru.

    Просим Вас поделиться с нами, зачем Вам необходим WebDAV и почему его
    функционал нельзя заменить текущим десктопным клиентом Облако@mail.ru?

    Благодарим Вас за помощь и сотрудничество.

    Команда проекта Облако@mail.ru.
    Похоже, сейчас они решают, делать ли вообще такой доступ.
    Ответ написан
    1 комментарий