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

cant upload files #53

Open
ghost opened this issue Dec 6, 2024 · 0 comments
Open

cant upload files #53

ghost opened this issue Dec 6, 2024 · 0 comments

Comments

@ghost
Copy link

ghost commented Dec 6, 2024

// openai
// To use dotenv, you need to add the "io.github.cdimascio:dotenv-kotlin:version"
// dependency. Then you can add a .env file in your project directory.
//String key = Dotenv.load().get("OPENAI_TOKEN");
openai = OpenAI.builder()
        .apiKey("API_KEY")
        .build();

messages = new ArrayList<>();
messages.add(ChatMessage.toSystemMessage("Help the user with their problem."));

// Here you can change the model's settings, add tools, and more.
request = ChatRequest.builder()
        .model("gpt-4o")
        .messages(messages)
        .build();
private byte[] downloadFile(String url) throws ClientProtocolException, IOException {
    HttpGet request = new HttpGet(url.replace(" ", "%20"));
    request.addHeader("sec-ch-ua",
        "\" Not A;Brand\";v=\"99\", \"Chromium\";v=\"130\", \"Microsoft Edge\";v=\"130\"");
    request.addHeader("X-Requested-With", "XMLHttpRequest");
    request.addHeader("sec-ch-ua-mobile", "?0");
    request.addHeader(
        "User-Agent",
        "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36 Edg/130.0.0.0");
    request.addHeader("sec-ch-ua-platform", "\"Windows\"");
    request.addHeader("Origin", url); // to be fixed
    request.addHeader("Sec-Fetch-Site", "same-origin");
    request.addHeader("Sec-Fetch-Mode", "cors");
    request.addHeader("Sec-Fetch-Dest", "empty");
    request.addHeader("Referer", url);
    request.addHeader("Accept-Language", "en-US,en;q=0.9");
    request.addHeader("sec-gpc", "1");
    DefaultHttpClient httpClient = new DefaultHttpClient();
    request.addHeader(
            "sec-ch-ua",
            "\" Not A;Brand\";v=\"99\", \"Chromium\";v=\"100\", \"Microsoft Edge\";v=\"100\"");
    request.addHeader("sec-ch-ua-mobile", "?0");
    request.addHeader(
            "User-Agent",
            "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4863.0 Safari/537.36 Edg/100.0.1163.1");
    CloseableHttpResponse response = httpClient.execute(request);
    InputStream is = response.getEntity().getContent();
    byte[] b = new byte[1024];
    int r;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    while ((r = is.read(b)) != -1) {
        baos.write(Arrays.copyOf(b, r));
        baos.flush();
    }
    baos.close();
    return baos.toByteArray();
}
if (label.equalsIgnoreCase("chatgpt")) {
    if (args.length == 0) {
        sender.sendMessage('\r' + "Unknown translation key: command.unknown.argument");
        sender.sendMessage('\r' + label + " " + String.join(" ", args)
                + "Unknown translation key: command.context.here");
        return;
    } else if (args[0].equalsIgnoreCase("upload") && args.length >= 2) {
        String url = args[1];
        String fn = url.substring(url.lastIndexOf("/") + 1, url.length());
        System.out.println(fn);
        byte[] b = null;
        try {
            b = downloadFile(url);
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        UploadFileRequest ufr = UploadFileRequest.builder().byteArray(fn, b).purpose(FilePurpose.ASSISTANTS).build();
        FileObject fo = openai.files().upload(ufr);
        System.out.println(fo);
        return;
    }
    String input = String.join(" ", args);
    messages.add(ChatMessage.toUserMessage(input));
    ChatResponse response = openai.createChatCompletion(request);
    sender.sendMessage('\r' + response.get(0).getMessage().getContent());
    messages.add(response.get(0).getMessage());
}

when running command chatgpt upload https://litter.catbox.moe/f4a2f1.jpg it results in this error

java.io.IOException: Unexpected code Response{protocol=http/1.1, code=415, message=Unsupported Media Type, url=https://api.openai.com/v1/files}, received: {
  "error": {
    "message": "Invalid Content-Type header (application/json; charset=utf-8), expected multipart/form-data. (HINT: If you're using curl, you can pass -H 'Content-Type: multipart/form-data')",
    "type": "invalid_request_error",
    "param": null,
    "code": null
  }
}

	at com.cjcrafter.openai.RequestHelper.executeRequest(RequestHelper.kt:66)
	at com.cjcrafter.openai.RequestHelper.executeRequest(RequestHelper.kt:77)
	at com.cjcrafter.openai.files.FileHandlerImpl.upload(FileHandlerImpl.kt:11)

and yes im fully aware im not doing my code right if i intend on actually using the uploaded file in the given chat thread to talk to gpt-4o, this is just a mere test of the file upload api for ChatGPT-Java-API which seems to be failing
Invalid Content-Type header (application/json; charset=utf-8), expected multipart/form-data. (HINT: If you're using curl, you can pass -H 'Content-Type: multipart/form-data') seems to be a error in ChatGPT-Java-API and not my code

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

0 participants