Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch from the deprecated Completions API to the Models API #654

Merged
merged 2 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
}
]
}
Loading