How to optimize the prompt and make it a single prompt to reduce the api request

I am using a multiple prompts to fetch the answer from the openai gpt 4 model. How can I optimize it to make it a single APi request instead of hitting the API multiple times.

static async Task ProcessAndStorePrompts(IConfiguration configuration, string jsonData, string tenantId, Database databaseHandler)
{
    var prompts = new Dictionary<string, Action<OpenAIModel, string>>()
{
    { "What is the total profit? Give the overview.", (OpenAIModel, answer) => OpenAIModel.Profit = answer },
    { "What is the total cost of sales? Give the overview.", (OpenAIModel, answer) => OpenAIModel.NetIncome = answer },
    { "What is the total administrative cost? Give detailed overview of each and every parameters of administrative cost.", (OpenAIModel, answer) => OpenAIModel.Overheads = answer },
    { "What is the total operational cost? Give the overview.", (OpenAIModel, answer) => OpenAIModel.Expenses = answer },
    { "What is the total income? Give the overview.", (OpenAIModel, answer) => OpenAIModel.Revenue = answer }
};

    // Get current month and year
    string currentMonth = DateTime.Now.ToString("MMMM yyyy");

    // Create an OpenAI model instance
    var aiModel = new OpenAIModel
    {
        Tenant = tenantId,
        Month = DateTime.Now,

    };

    foreach (var prompt in prompts)
    {
        string answer = await AskQuestionAboutJsonAsync(configuration, prompt.Key, jsonData);
        if (!string.IsNullOrEmpty(answer))
        {
            // Assign the answer to the appropriate field in the model
            prompt.Value(aiModel, answer);
        }
    }

    // Store the data in the database
    databaseHandler.JsontoAI(aiModel);
}
1 Like

Welcome to the community!

One common way to deal with this is to use JSON output. You can define a schema that the model is supposed to fill, and then parse the JSON once you receive it.

There are also tools out there to parse JSON in real time, if that’s a requirement.

The API supports both JSON mode and structured outputs for this use-case, you can find more information here https://platform.openai.com/docs/guides/structured-outputs :slight_smile:

Good luck!

3 Likes