#image upload during chat
1 messages · Page 1 of 1 (latest)
Hi @valid sun. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
To upload an image during a chat with the assistant you created, you can follow these steps:
- Upload the image file to OpenAI's file storage using the file upload endpoint
<https://api.openai.com/v1/files.> Here's an example of how to do this in Dart:
Future<dynamic> openAIFileUpload(String? droppedFileData) async {
String apiKey = dotenv.env['OPENAI_API_KEY'] ?? '';
final MessageController messageController = Get.find();
try {
final uri = Uri.parse('<https://api.openai.com/v1/files'>);
final request = http.MultipartRequest('POST', uri)
..headers['Authorization'] = 'Bearer $apiKey'
..fields['purpose'] = 'assistants';
if (droppedFileData != null) {
request.files.add(
http.MultipartFile.fromBytes(
'file',
base64Decode(droppedFileData),
filename: 'uploaded_file',
),
);
}
final response = await request.send();
final responseBody = await response.stream.bytesToString();
}
-
After a successful upload, extract the file-id from the response.
-
Pass the file-id as an attachment when you send the user query. Here's an example of how to do this:
# Add the user input (the actual query)
self.client.beta.threads.messages.create(
thread_id=self.thread.id,
role="user",
content=userInput['message'],
attachments=[
{
"file_id": fileID,
"tools": [
{"type": "code_interpreter"}
]
}
]
)
Please note that the file can be added at different levels (Assistant level, Thread level, Message level) depending on your specific use case. If many users will interact with the same Assistant and ask questions, it should be at the Assistant level. If the file is specific to one user or conversation, it should be at the thread or message level[^1^].
Also, remember that the Assistant API does not currently support sending base64 with a message. You either provide a URL, or you upload a file with purpose vision to storage and then use the correct file attachment method for vision[^2^].