From 290f67592f0f0bee550f9a388cf36af9a825ea8a Mon Sep 17 00:00:00 2001 From: Darin Kotter Date: Tue, 9 Jan 2024 11:03:42 -0700 Subject: [PATCH 1/2] Instead of making a request to the now deprecated completions API to validate credentials, make a request to the models API --- includes/Classifai/Providers/OpenAI/OpenAI.php | 17 +++-------------- 1 file changed, 3 insertions(+), 14 deletions(-) diff --git a/includes/Classifai/Providers/OpenAI/OpenAI.php b/includes/Classifai/Providers/OpenAI/OpenAI.php index e35c7bb89..8e4e27b4c 100644 --- a/includes/Classifai/Providers/OpenAI/OpenAI.php +++ b/includes/Classifai/Providers/OpenAI/OpenAI.php @@ -13,11 +13,11 @@ trait OpenAI { /** - * OpenAI completions URL + * OpenAI model URL * * @var string */ - protected $completions_url = 'https://api.openai.com/v1/completions'; + protected $model_url = 'https://api.openai.com/v1/models'; /** * Add our OpenAI API settings field. @@ -139,18 +139,7 @@ protected function authenticate_credentials( string $api_key = '' ) { // Make request to ensure credentials work. $request = new APIRequest( $api_key ); - $response = $request->post( - $this->completions_url, - [ - 'body' => wp_json_encode( - [ - 'model' => 'ada', - 'prompt' => 'hi', - 'max_tokens' => 1, - ] - ), - ] - ); + $response = $request->get( $this->model_url ); return ! is_wp_error( $response ) ? true : $response; } From 3529e012ac17935012bc1bbec05fc3a84b0da7d0 Mon Sep 17 00:00:00 2001 From: Darin Kotter Date: Tue, 9 Jan 2024 11:40:57 -0700 Subject: [PATCH 2/2] Mock requests to the Models API to fix tests --- tests/test-plugin/e2e-test-plugin.php | 2 ++ tests/test-plugin/models.json | 23 +++++++++++++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 tests/test-plugin/models.json diff --git a/tests/test-plugin/e2e-test-plugin.php b/tests/test-plugin/e2e-test-plugin.php index acc213ac3..aeb47bb52 100644 --- a/tests/test-plugin/e2e-test-plugin.php +++ b/tests/test-plugin/e2e-test-plugin.php @@ -19,6 +19,8 @@ function classifai_test_mock_http_requests( $preempt, $parsed_args, $url ) { if ( strpos( $url, 'http://e2e-test-nlu-server.test/v1/analyze' ) !== false ) { $response = file_get_contents( __DIR__ . '/nlu.json' ); + } elseif ( strpos( $url, 'https://api.openai.com/v1/models' ) !== false ) { + $response = file_get_contents( __DIR__ . '/models.json' ); } elseif ( strpos( $url, 'https://api.openai.com/v1/completions' ) !== false ) { $response = file_get_contents( __DIR__ . '/chatgpt.json' ); } elseif ( strpos( $url, 'https://api.openai.com/v1/chat/completions' ) !== false ) { diff --git a/tests/test-plugin/models.json b/tests/test-plugin/models.json new file mode 100644 index 000000000..f6784a569 --- /dev/null +++ b/tests/test-plugin/models.json @@ -0,0 +1,23 @@ +{ + "object": "list", + "data": [ + { + "id": "model-id-0", + "object": "model", + "created": 1686935002, + "owned_by": "organization-owner" + }, + { + "id": "model-id-1", + "object": "model", + "created": 1686935002, + "owned_by": "organization-owner" + }, + { + "id": "model-id-2", + "object": "model", + "created": 1686935002, + "owned_by": "openai" + } + ] +}