Abhinav Kumar
Abhinav Kumar

Reputation: 11

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);
}

Upvotes: 1

Views: 55

Answers (0)

Related Questions