0

Below is the python code I have. The output is exactly what I am looking for however I cannot figure out how to put the multiple responses that is printed out by the last line of python code to return back all responses into a new column in a dataframe. I get None values when I have tried in the past. Please help! I am using open.ai and pandas and hoping to export to an excel file.

for question in df['Question']:
    response = openai.Completion.create(
        model="text-davinci-003",
        prompt="Write a detailed, lengthy, and authoratative response to the question: {}".format(question),
        temperature=0.7,
        max_tokens=20,
        top_p=1,
        frequency_penalty=0,
        presence_penalty=0
    )
    print(response['choices'][0]['text'].strip())
print(response['choices'][0]['text'].strip())

This line gives me the correct output but how do I join these strings and put them in a new column in excel. Stumped!

Bijay Regmi
  • 1,187
  • 2
  • 11
  • 25
Fox
  • 1
  • I think you want to look into `df.apply()`. Something like `df["Answer"] = df["Question"].apply(get_answer)` and the function `get_answer` is something like `return openai.Completion.create( model="text-davinci-003", prompt="Write a detailed, lengthy, and authoratative response to the question: {}".format(question), temperature=0.7, max_tokens=20, top_p=1, frequency_penalty=0, presence_penalty=0 )` – Bijay Regmi Dec 05 '22 at 22:26
  • Thanks but I cannot seem to make that work unfortunately as a beginner. – Fox Dec 06 '22 at 01:53

2 Answers2

0

Create a function that returns answer for your questions, just like how you have done like this:

def openai_completion(question):
    response = openai.Completion.create(
    model="text-davinci-003",
    prompt="Write a detailed, lengthy, and authoratative response to the question: {}".format(question),
    temperature=0.7,
    max_tokens=20,
    top_p=1,
    frequency_penalty=0,
    presence_penalty=0
    )
    return response['choices'][0]['text'].strip()

Now we need to create a new column called Answers which gets filled by apply function which we >apply< onto your Question column:

df["Answer"] = df["Question"].apply(openai_completion)

Now your DataFrame can be exported as excel.

df.to_excel("MyExcelFile.xlsx", index=False)
Bijay Regmi
  • 1,187
  • 2
  • 11
  • 25
0

First creat an empty list before the for loop. Then append the empty list with the generated outputs. Finally write the appended list to exisiting excel and save

response_list = []

for question in df['Question']:
    response = openai.Completion.create(
        model="text-davinci-003",
        prompt="Write a detailed, lengthy, and authoratative response to the question: {}".format(question),
        temperature=0.7,
        max_tokens=20,
        top_p=1,
        frequency_penalty=0,
        presence_penalty=0
    )
    generated_response = response['choices'][0]['text'].strip()
    response_list.append(generated_response )
    print(re)

questionFile["response"] = response_list
questionFile.to_excel("question_reponse.xlsx", header=True, index=False)