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.
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.