Problem/Motivation

See https://platform.openai.com/docs/api-reference/files/create

Once #3546954: Normalize file api's to allow providers to use them lands, we can add the ability to upload files and use them for various purposes. Eventually this could be added automatically in some cases (chats with history, fine-tuning).

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

b_sharpe created an issue. See original summary.

b_sharpe’s picture

Status: Active » Needs work
Parent issue: » #3546954: Normalize file api's to allow providers to use them

b_sharpe’s picture

Status: Needs work » Needs review

Ok, this works now with the parent issue; however, I didn't know that openai base is in the main module. So until that changes it's likely better to move the trait there too?

Either way, I was able to do this:

    $file = 'public://2025-10/44678.pdf';
    $aiFile = $this->aiFileManager->upload($file, 1, 'openai');

Then later use that file in a chat message like so and it was able to read the file and tell me all about it:

    $context = "Can you tell me about the file";
    $message = new ChatMessage('user', $context);

    // Add the pdf.
    $message->addRemoteFile($aiFile->getRemoteId());

    $messages[] = $message;
    $input = new ChatInput($messages);

    $response = $provider->chat($input, $config->get('ai_model_id'), ['crux'])->getNormalized();
marcus_johansson’s picture

Yes, I think for 1.2.0, move the trait there. I think the methods names should be a little bit more specific, so there is no future collision with some other download, delete etc. So maybe FileDownload, FileDelete etc.

There is an Enum where we set model capabilities that we could use to set a new capability for file upload. From what I have seen not all OpenAI models supports it, so it should be on the model level.

That way we can have checks where the standard is that its not supported, and OpenAI for now supports it.

b_sharpe’s picture

Ok, moving to main module, this MR now is just supporting it in chat.

marcus_johansson’s picture

Issue tags: +priority
marcus_johansson’s picture

Issue tags: +AI Initiative Sprint
marcus_johansson’s picture

Assigned: b_sharpe » Unassigned
csakiistvan’s picture

Status: Needs review » Needs work

@b_sharpe please check the MR, for me the `FileApiTrait` class is not available on `drupal/ai` module (branch 1.2.x).

arianraeesi’s picture

Issue tags: +AI Product Development
abhisekmazumdar’s picture

Status: Needs work » Needs review

Hi @csakiistvan

As the issue mentioned, those classes or files are like:

use Drupal\ai\AiFileProviderInterface;
use Drupal\ai\Trait\OpenAi\FileApiTrait;

Are dependent on the parent issue: #3546954: Normalize file api's to allow providers to use them Check the MR

jibran’s picture

Status: Needs review » Needs work
Issue tags: +Needs tests

Let's add tests for this functionality.

abhisekmazumdar’s picture

Assigned: Unassigned » abhisekmazumdar

OK I will make it happen. Thank You

abhisekmazumdar’s picture

I see this module doesn't have any tests. Although AI cooked a few tests for this specific MR changes / issue, I'm still testing them. Meanwhile, I would like to ask if we really need tests for this issue?

abhisekmazumdar’s picture

Assigned: abhisekmazumdar » Unassigned
Status: Needs work » Needs review

Before you start reviewing this, please take a look at #3546954-15: Normalize file api's to allow providers to use them. The pipeline error will only be resolved when that issue is merged and a stable release is made for ai module.

For testing, ensure you pull those changes into the AI module as well.

jibran’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: -Needs tests

This looks good now.