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

Implement REST API #2

Merged
merged 3 commits into from
Oct 19, 2024
Merged

Implement REST API #2

merged 3 commits into from
Oct 19, 2024

Conversation

thekid
Copy link
Member

@thekid thekid commented Oct 13, 2024

Basic usage

Using the REST API, see https://platform.openai.com/docs/api-reference/making-requests

use util\cmd\Console;
use com\openai\rest\OpenAIEndpoint;

$ai= new OpenAIEndpoint('https://'.getenv('OPENAI_API_KEY').'@api.openai.com/v1');
$payload= [
  'model'    => 'gpt-4o-mini',
  'messages' => [['role' => 'user', 'content' => $prompt]],
];

Console::writeLine($ai->api('/chat/completions')->invoke($payload));

Streaming

The REST API can use server-sent events to stream responses, see https://platform.openai.com/docs/api-reference/streaming

use util\cmd\Console;
use com\openai\rest\OpenAIEndpoint;

$ai= new OpenAIEndpoint('https://'.getenv('OPENAI_API_KEY').'@api.openai.com/v1');
$payload= [
  'model'    => 'gpt-4o-mini',
  'messages' => [['role' => 'user', 'content' => $prompt]],
];

$stream= $ai->api('/chat/completions')->stream($payload);
foreach ($stream->deltas('content') as $delta) {
  Console::write($delta);
}
Console::writeLine();

@thekid thekid added the enhancement New feature or request label Oct 13, 2024
@thekid thekid merged commit 203fb79 into main Oct 19, 2024
@thekid thekid deleted the feature/rest-api branch October 19, 2024 08:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant