I’m using dotnet with Open-AI’s official library,
I’ve wrote a service that sends an image and returns a JSON with specific structure, However, sometimes it response with a broken JSON object, which cause a server error..
How do I make it to response with a consist object structure ?
Here is the service I’ve wrote:
using System.Text.Json;
using backend.DTOs;
using OpenAI.Chat;
namespace backend.Services;
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,
ResponseFormat = ChatResponseFormat.JsonObject
};
}
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 to have a itemName itemPrice quantity totalItemPrice, return a json. ""
Make sure the following JSON response should look like:
{
""storeName"": ""string"",
""purchase"": {
""purchaseDate"": ""2024-08-14T12:40:05.638Z"",
""items"": [
{
""itemName"": ""string"",
""itemPrice"": 0,
""quantity"": 0,
""totalItemPrice"": 0
}
]
}
}."),
ChatMessageContentPart.CreateImageMessageContentPart(imageBytes, "image/png")
})
};
ChatCompletion completion = await _chatClient.CompleteChatAsync(messages, _options);
var resp = completion.Content[0].ToString().Trim();
Console.WriteLine("Raw Response: " + resp);
using var document = JsonDocument.Parse(resp);
var receiptDto = JsonSerializer.Deserialize<ReceiptDto>(document.RootElement.GetRawText());
Console.WriteLine("Deserialized Receipt: " + JsonSerializer.Serialize(receiptDto));
return receiptDto;
}
}