[SOLVED] How to associate an already uploaded file to a wizard project via PHP API

Hello.

When I am in the OpenAi Playground, I see the possibility of uploading files associated with a wizard or a fine tuning.

In my case I am first experimenting with the Assistant.

However, in the API documentation I do not see how to associate a storage file (of which I already have its id and its purpose is for a wizard) to a specific wizard project.

Or even, how to do this in a single operation, which is what would seem most logical to me.

I work in PHP (Laravel) and I don’t see the doc or tips based on Guzzle for this purpose.

It’s possible? Have I missed something?

The number of posts seemed incredible to me, and even ChatGPT itself, or the Jetbrains AI Assistant, insisted that it was not possible.

It was not logical that in the OpeAi Playground, it could be done and not through the API.

My solution with my AI Service in laravel

$assistant = new CastrisParseAssistant($projectID);

 $result = $assistant->educate('/full/path/file');

// $result is a OpenAI\Responses\Assistants\Files\AssistantFileResponse with id, object, createAt, assistantId
// $result->toArray()
// array:4 [▶
//  "id" => "file-DaSWkXtG9rZ3pmJUNnE6hL7A"
 // "object" => "assistant.file"
 //  "created_at" => 1709666347
 //  "assistant_id" => "asst_DNypMyxxwELHDnU8c6H1vcLY"
// ]

class CastrisParseAssistant
{
    public AssistantResponse $assistant;

    protected string $threadId;

    protected OpenAIClient $client;

    public function __construct(string $assistantId, ?AIClient $client = null)
    {
        $this->client = $client ?? new OpenAIClient();
        $this->assistant = $this->client->retrieveAssistant($assistantId);
    }

public function educate(string $file): AssistantFileResponse
    {
        return $this->client->uploadFile($file, $this->assistant);
    }

// Other code
class OpenAIClient implements AIClient 
{
 public function uploadFile(string $file, AssistantResponse $assistant): AssistantFileResponse
    {
        $file = OpenAI::files()->upload([
            'purpose' => 'assistants',
            'file'    => fopen($file, 'rb')
        ]);

        return OpenAI::assistants()
            ->files()
            ->create($assistant->id, ['file_id' => $file->id]);
    }
   // Other code
}

That’s is all folks

1 Like