how to automate the large number of questions to openai

test29998411 281 Reputation points
2023-09-29T18:31:38.2733333+00:00

We are building a CHAT web application in Azure WEB apps using Azure Open AI's GPT model.

In testing, we need to run a question that is per row in an EXCEL file and transpose the answers from the chat app to the column next to the question in the EXCEL file.

What tool would you typically use to test such a large number of questions as described above?

Azure OpenAI Service
Azure OpenAI Service
An Azure service that provides access to OpenAI’s GPT-3 models with enterprise capabilities.
2,255 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Saurabh Sharma 23,761 Reputation points Microsoft Employee
    2023-09-29T21:34:35.6066667+00:00

    Hi @test29998411

    Welcome to Microsoft Q&A! Thanks for posting the question.

    Which language you are using to build your web application? If you are using python, then you can leverage the openpyxl library to access excel file for your purpose.

    Please find below the sample code using openpyxl which does the same thing.

    import openai
    import time
    import os
    import openpyxl
    
    openai.api_type = "azure"
    openai.api_base = "https://{openais}.openai.azure.com/"
    openai.api_key = os.getenv("AOAIKey")
    openai.api_version = "2023-05-15"
    model_name = "gpt35turbo"
    
    def get_answer(question):
     conversation = [{"role":"system","content":"You are an AI assistant that helps people find information."}]
     conversation.append({"role": "user", "content": question})
    
     response = openai.ChatCompletion.create(
        engine=model_name,      
        n=1,
        stop=None,
        temperature=0.5,
        messages=conversation
      )
     answer = response["choices"][0]["message"]["content"]
     return answer
    
    # Load the Excel file
    workbook = openpyxl.load_workbook('C:\workspace\Queston_Answers_ForOpenAI.xlsx')
    worksheet = workbook.active
    
    # Get the maximum row number
    max_row = worksheet.max_row
    
    # Loop through each row in the column, excluding the first row
    for row in range(2, max_row + 1):
        # Get the cell value
        cell_value = worksheet.cell(row=row, column=1).value
        
        # Call OpenAI to get the answer
        answer = get_answer(cell_value)
        
        # Write the response to the second column
        worksheet.cell(row=row, column=2, value=answer)
    
        # Get the cell number
        cell_number = worksheet.cell(row=row, column=1).coordinate
        
        # Print the cell value and cell number
        #print('Cell value:', cell_value)
        #print('Cell number:', cell_number)
    
    
    # Save the updated Excel file
    workbook.save('C:\workspace\Queston_Answers_ForOpenAI.xlsx')
    

    This code assumes the questions are in the first column and answers will be written in the second column of rows having any questions (excluding the first row). Once you run the python code you can get the answers written in the file in Column B.

    User's image

    Please let me know if you have any questions.

    Thanks

    Saurabh


    Please 'Accept as answer' and Upvote if it helped so that it can help others in the community looking for help on similar topics.

    1 person found this answer helpful.