Skip to content

Conversation

@thekid
Copy link
Member

@thekid thekid commented Apr 5, 2025

Streaming works differently in the reponses API; and is more comparable to the WebSocket API, sending events with types and payloads, and sending deltas as well as the final values, requiring no extra data merging. This pull request adds an events() method to yield the event types and its JSON values.

Here's an example of using it in conjunction with the responses API:

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

$ai= new OpenAIEndpoint('https://'.getenv('OPENAI_API_KEY').'@api.openai.com/v1');

$events= $ai->api('/responses')->stream([
  'model' => 'gpt-4o',
  'input' => $argv[1],
]);
foreach ($events as $type => $value) {
  if ('response.output_text.delta' === $type) {
    Console::write($value['delta']);
  }
}
Console::writeLine();

See #20 (comment)

@thekid
Copy link
Member Author

thekid commented Apr 6, 2025

The Responses API represents the future direction for building agents on OpenAI.
Source: https://platform.openai.com/docs/guides/responses-vs-chat-completions

To prioritize the responses API, we could use the stream() name for this, and rename the original, which returns "completion chunks" to completions() or chunks().

@thekid thekid changed the title Add events API Implement responses API streaming Apr 6, 2025
@thekid thekid merged commit e146322 into main Apr 6, 2025
12 checks passed
@thekid thekid deleted the feature/api-events branch April 6, 2025 10:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants