hello,
Im trying to to image processing using the new official open ai library for dotnet,
I get an error about image is not being supportive type or size, when I checked the file itself, I’ve found out the that image is 2mb and jpg type, which is supported, so what am i doing wrong ?
I’ve wrote a service that connect to open ai (it works), but some how i recive a message that image is not supported…
error:
"title": "HTTP 400 (invalid_request_error: image_parse_error)\n\nYou uploaded an unsupported image. Please make sure your image is below 20 MB in size and is of one the following formats: ['png', 'jpeg', 'gif', 'webp'].",
here is my service:
public class OpenAiService
{
private readonly ChatClient _chatClient;
private readonly ChatCompletionOptions _options;
public OpenAiService(IConfiguration config)
{
var apiKey = config.GetValue<string>("OpenAI:Key");
_chatClient = new ChatClient("gpt-4o", apiKey);
_options = new ChatCompletionOptions()
{
MaxTokens = 300,
};
}
public async Task<ReceiptDto> ExtreactListOfItems(Stream imageStream)
{
var imageBytes = await BinaryData.FromStreamAsync(imageStream);
var messages = new List<ChatMessage>
{
new UserChatMessage(new List<ChatMessageContentPart>()
{
ChatMessageContentPart.CreateTextMessageContentPart(
"Extract a list of items from the following image, then return a list with each item having a title, price, quantity, total price. Return a JSON object structured as: { store:'x', purchase:{ date:'yyyy-mm-dd', items:[{ title:'', price:10, quantity:2, total: price * quantity }] } }"
),
ChatMessageContentPart.CreateImageMessageContentPart(imageBytes, "image/png")
})
};
ChatCompletion completion = await _chatClient.CompleteChatAsync(messages, _options);
var responseContent = completion.Content[0].Text;
var receipt = JsonSerializer.Deserialize<ReceiptDto>(responseContent);
return receipt;
}
}
and my controller
[ApiController]
[Route("api/image-processing")]
public class ImageProcessingController : ControllerBase
{
private readonly OpenAiService _openAiService;
public ImageProcessingController(OpenAiService openAiService)
{
_openAiService = openAiService;
}
[HttpPost("process")]
public async Task<ActionResult<ReceiptDto>> ProcessImage([FromForm] IFormFile image)
{
if (image.Length == 0)
{
return BadRequest("No image provided.");
}
await using var imageStream = new MemoryStream();
await image.CopyToAsync(imageStream);
imageStream.Position = 0;
var result = await _openAiService.ExtreactListOfItems(imageStream);
return Ok(result);
}
}
what am i doing wrong ?
here is some info about the image:
and here is how i use it in postman: