Skip to content

Example proposal: Using ResponseInputFile with PDF #440

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

Closed
nuclear-bean opened this issue Apr 15, 2025 · 1 comment
Closed

Example proposal: Using ResponseInputFile with PDF #440

nuclear-bean opened this issue Apr 15, 2025 · 1 comment

Comments

@nuclear-bean
Copy link
Contributor

A follow-up to the now-closed issue #352

While working with the ResponseInputFile API, I found that embedding a PDF file as input was a bit unintuitive.

I feel that this repository could benefit from an example demonstrating how to do this. Below is a sample class I propose adding to the /examples package:

import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.models.ChatModel;
import com.openai.models.responses.ResponseCreateParams;
import com.openai.models.responses.ResponseInputFile;
import com.openai.models.responses.ResponseInputItem;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Base64;
import java.util.List;

public final class ResponseInputFileExample {

    private ResponseInputFileExample() {}

    public static void main(String[] args) throws IOException {
        // Configures using the `OPENAI_API_KEY`, `OPENAI_ORG_ID` and `OPENAI_PROJECT_ID` environment variables
        OpenAIClient client = OpenAIOkHttpClient.fromEnv();

        // Read file content and construct data URI String
        File file = new File("/absolute/path/to/your/file.pdf");
        byte[] fileContent = Files.readAllBytes(file.toPath());
        String base64String = Base64.getEncoder().encodeToString(fileContent);
        String dataUri = "data:application/pdf;base64," + base64String;

        ResponseInputFile inputFile = ResponseInputFile.builder()
                .filename(file.getName())
                .fileData(dataUri)
                .build();

        ResponseInputItem messageInputItem = ResponseInputItem.ofMessage(ResponseInputItem.Message.builder()
                .role(ResponseInputItem.Message.Role.USER)
                .addInputTextContent("Describe this file.")
                .addContent(inputFile)
                .build());

        ResponseCreateParams createParams = ResponseCreateParams.builder()
                .inputOfResponse(List.of(messageInputItem))
                .model(ChatModel.GPT_4O)
                .build();

        client.responses().create(createParams).output().stream()
                .flatMap(item -> item.message().stream())
                .flatMap(message -> message.content().stream())
                .flatMap(content -> content.outputText().stream())
                .forEach(outputText -> System.out.println(outputText.text()));
    }

}

Let me know if there's interest in including this in the examples - I'd be happy to open a PR or update the snippet further if needed.

@TomerAberbach
Copy link
Collaborator

PR welcome!

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

No branches or pull requests

2 participants