Ulploading any text file to chatGpt and ask questions related to file in flutter?//Expected file have JSONL format (HINT: line starts with: \%PDF...\)

Hi,
I am building a chatGpt app in flutter that answers to user queries. I have added functionality to upload any text based file and then ask question from chatgpt(model = gpt-4)related to that file. But i have stuck on code that how can i do this.
Does any one has made code for this in flutter?

Here is my code:
//picks files and returns path of file as string
static Future<String?> pickFile() async {
final file = await FilePicker.platform.pickFiles();
if (file == null || file.files.isEmpty) {
return null;
} else {
logg.log(‘file is not empty’);
return file.files.single.path;
}
}

here is method for uploading file to chatgpt api :
static Future uploadFile(String filePath) async {
final url = Uri.parse(‘https://api.openai.com/v1/files’);
final file = File(filePath);
final fileName = path.basename(filePath);
final fileStream = http.ByteStream(file.openRead());
final fileLength = await file.length();

final request = await http.MultipartRequest('POST', url);
final multipartFile = http.MultipartFile(
  'file',
  fileStream,
  fileLength,
  filename: fileName,
);
request.files.add(multipartFile);

request.headers['Authorization'] = 'Bearer $api';
request.fields['purpose'] = 'fine-tune';

final response = await request.send();
logg.log(await response.stream.bytesToString());

if (response.statusCode == 200) {
  log('File uploaded successfully.');
} else {
  log('File upload failed with status ${response.statusCode}.');
}

}

and i am calling these functions on button as when user taps on button upload a file:
_uploadFile() async {
final String? file = await ApiServices.pickFile();
if (file == null) {
showToast(msg: ‘file not picked’, toastLength: Toast.LENGTH_LONG);
} else {
await ApiServices.uploadFile(file);
}
}

It’s because you’re calling the fine-tune endpoint, which isn’t meant for this purpose.

Read this QnA FAQ

1 Like

Thanks, but that is deprecated.
I am a wonder to know that can I upload any file(pdf, txt, word, excel, ppt, csv) to upload file in chatbot OpenAI API

Ah, I see. Just curious how do you know its deprecated?