Skip to content

Commit

Permalink
Merge pull request #654 from 10up/fix/ada-model-deprecation
Browse files Browse the repository at this point in the history
Switch from the deprecated Completions API to the Models API
  • Loading branch information
dkotter authored Jan 10, 2024
2 parents 7fb61a0 + 3529e01 commit 94dc845
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 14 deletions.
17 changes: 3 additions & 14 deletions includes/Classifai/Providers/OpenAI/OpenAI.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
}
Expand Down
2 changes: 2 additions & 0 deletions tests/test-plugin/e2e-test-plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand Down
23 changes: 23 additions & 0 deletions tests/test-plugin/models.json
Original file line number Diff line number Diff line change
@@ -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"
}
]
}

0 comments on commit 94dc845

Please sign in to comment.