CJCrafter / CJCrafter/ChatGPT-Java-API

cant upload files

Đang mở
#53 0 bình luận 0 reaction 0 người được giao Xem trên GitHub
Ngôn ngữ chính
Kotlin
Star
72
Fork
12
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Mô tả

```
// 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

Hướng dẫn đóng góp

Chưa lập chỉ mục được hướng dẫn đóng góp cho kho mã nguồn này

Hướng nghiên cứu

Bắt đầu với FileHandlerImpl.kt và RequestHelper.kt, sử dụng lời gọi upload đã được báo cáo cùng phản hồi 415 làm trường hợp tái hiện. Theo dõi cách request upload tệp được xây dựng và xác minh content type của nó đối với endpoint files của OpenAI. Được xem là hoàn tất khi request upload được chấp nhận thay vì được gửi dưới dạng application/json.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
java, kotlin
Lĩnh vực
api
Loại issue
Lỗi
Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Đặc tả rõ ràng
Mức phù hợp với người mới
38/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.