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

Support uploading files #9

Merged
merged 4 commits into from
Oct 27, 2024
Merged

Support uploading files #9

merged 4 commits into from
Oct 27, 2024

Conversation

thekid
Copy link
Member

@thekid thekid commented Oct 20, 2024

This can be used for file uploads or to transcribe audio, for example:

use com\openai\rest\OpenAIEndpoint;
use io\File;
use util\cmd\Console;
use util\MimeType;

$ai= new OpenAIEndpoint('https://'.getenv('OPENAI_API_KEY').'@api.openai.com/v1');
$file= new File($argv[1]);

$response= $ai->api('/audio/transcriptions')
  ->open(['model' => 'whisper-1'])
  ->transfer('file', $file->in(), $file->filename, MimeType::getByFileName($file->filename))
  ->finish()
;
Console::writeLine($response->value());

See also

@thekid
Copy link
Member Author

thekid commented Oct 20, 2024

Should we abstract this into a higher-level API along the lines of the following?

$value= $ai->api('/audio/transcriptions')->upload($file, ['model' => 'whisper-1']);

@thekid
Copy link
Member Author

thekid commented Oct 21, 2024

Should we abstract this into a higher-level API

Maybe (like with transmit vs. invoke) we have both, but start with the higher-level abstraction, calling it transfer. We do need an easy way to stream the data though, like an OutputStream that can be written to.

Unsure how to implement at the moment...

@thekid
Copy link
Member Author

thekid commented Oct 27, 2024

Unsure how to implement at the moment...

How about this:

// Using transfer
$response $ai->api('/audio/transcriptions')
  ->open(['model' => 'whisper-1']);
  ->transfer('file', $file->in(), $file->filename)
  ->finish()
;

// Using streaming
$uploads= $ai->api('/audio/transcriptions')->open(['model' => 'whisper-1']);
$stream= $uploads->stream('file', $file->filename);
while ($in->available()) {
  $stream->write($in->read());
}
$response= $uploads->finish();

This mimicks the (underlying) webservices.rest.RestUpload but abstracts it into something slightly simpler.

@thekid thekid added the enhancement New feature or request label Oct 27, 2024
@thekid
Copy link
Member Author

thekid commented Oct 27, 2024

Integration tests for transcription:

  • OpenAI
  • AzureAI

@thekid thekid merged commit 1b99238 into main Oct 27, 2024
12 checks passed
@thekid thekid deleted the feature/upload branch October 27, 2024 08:18
@thekid thekid changed the title Add API::upload() to upload files to API and return response Support uploading files Oct 27, 2024
@thekid
Copy link
Member Author

thekid commented Oct 27, 2024

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