64bit / async-openai

Rust library for OpenAI
https://docs.rs/async-openai
MIT License
1.11k stars 166 forks source link

FIleInput from memory #144

Open nodir-t opened 10 months ago

nodir-t commented 10 months ago

I am trying to understand how to upload a file with contents from memory and can't figure out how. It appears that file input is only from a file? Do I have to save my bytes to file system to read them back?

My overall goal is to upload a file my program receives, from an incoming network request to an assistant thread.

64bit commented 10 months ago

In current design files are only read from filesystem.

File uploads from in-memory sounds like a good feature to add.

prosammer commented 10 months ago

@64bit I am looking for the ability to add audio data vecs to AudioInput instead of having to write them to a file first, would this be something you'd be interested in a PR for?

64bit commented 10 months ago

Absolutely interested!I think having this feature would work same for bunch of inputs: FileInput, ImageInput, AudioInput. Looks like they need to be enum to support paths and bytes?

Regarding PR: for non trivial features like this one it really helps to have a new self contained example because it becomes easy to test new feature and old examples.

nodir-t commented 10 months ago

Could you accept io::Read instead, not a Vec? I'd like to be able to pipe data from a network request to OpenAI without having it buffer it neither in the file system, nor memory.

prosammer commented 10 months ago

Hey @nodir-t @64bit , I put up a PR which works for in-memory files (Bytes and vec[u8] as that's what reqwest::Body accepts), but I wasn't able to figure out a way to get these inputs to take an io::Read as it seems reqwest::Body wants AsyncRead. I tried having these Inputs accept a generic that was AsyncRead but got confused as to how that would work with the derive_builder builder which would require the AsyncRead instance to be Clone as well. I'm fairly new to Rust and open source PRs so any feedback would be much appreciated!

64bit commented 9 months ago

hey @Prosammer , thank you I was able to check it out today and it looks good given that its backward compatible change for consumers of library happy to merge it.

I'll keep this issue open for the context and if someone wants to continue work on AsyncRead and io:Read suggested in this thread.

ifsheldon commented 9 months ago

This seems coincidentally solve some issues for wasm support, as I was planning to do this here. Thanks @Prosammer! If you have some time and interest, you can take a look at the experiment branch and help mature the wasm support.

prosammer commented 9 months ago

@ifsheldon haha awesome, I'm not very familiar with wasm but I will take a look!

nodir-t commented 9 months ago

AsyncRead is even better. Thank you