Hi everyone I have this code and I want to ask a question from the assistant and then provide some additional context and ask the same question again. Based on this setup that I have the follow-up response just gives me the question that I asked. Can anyone help me with that?
Here is the thing I am trying to do:
Question 1 > Response 1 > new information > Question 1 > Response 2
import os
import pandas as pd
from openai import OpenAI
from dotenv import load_dotenv
load_dotenv('api.env')
api_key = os.getenv('api_key')
client = OpenAI(api_key=api_key)
# Questions
questions = [
"What do you expect the rate of inflation to be over the next 12 months? Please give your best guess. Over the next 12 months, I expect the rate of inflation to be",
"What do you expect the rate of inflation to be over that period? Please give your best guess. Over the 12-month period between December 2025 (24 months from now) and December 2026 (36 months from now), I expect the rate of inflation to be"
]
# Assistant IDs
assistants = {
"Assistant_P1_2023_new": "asst_8qH7In7VCTXidbKfPOj5mSsT",
"Assistant_M1_2023_new": "asst_1JRk5Q9VVm1kptFDId7pTayc"
}
results = []
for assistant_name, assistant_id in assistants.items():
for question in questions:
# Create a Thread
my_thread = client.beta.threads.create()
my_thread_message = client.beta.threads.messages.create(
thread_id=my_thread.id,
role="user",
content=question
)
# Run the Assistant
my_run = client.beta.threads.runs.create(
thread_id=my_thread.id,
assistant_id=assistant_id,
instructions="Do not use the exact inflation mentioned in the document. Use your general understanding of the document including the sentiments of the policy and all the information around it to answer. These are questions about inflation expectations and the perception of inflation, not inflation prediction. Do not answer anything. Give the number in percentage. Again, Please answer the questions with only a number based on your general understanding of the document . "
)
while True:
keep_retrieving_run = client.beta.threads.runs.retrieve(
thread_id=my_thread.id,
run_id=my_run.id
)
if keep_retrieving_run.status == "completed":
all_messages = client.beta.threads.messages.list(
thread_id=my_thread.id
)
first_response = all_messages.data[0].content[0].text.value
break
follow_up_content = "Consider the mortgage rate is 7.5% and now answer the previous question again."
client.beta.threads.messages.create(
thread_id=my_thread.id,
role="user",
content=follow_up_content
)
my_follow_up_run = client.beta.threads.runs.create(
thread_id=my_thread.id,
assistant_id=assistant_id,
instructions="Please consider the updated information about the mortgage rate being 7.5% what is you new answer to the previous question?"
)
while True:
follow_up_run_status = client.beta.threads.runs.retrieve(
thread_id=my_thread.id,
run_id=my_follow_up_run.id
)
if follow_up_run_status.status == "completed":
all_follow_up_messages = client.beta.threads.messages.list(
thread_id=my_thread.id
)
follow_up_response = all_follow_up_messages.data[-1].content[0].text.value
results.append([assistant_name, question, first_response, follow_up_response])
break
df = pd.DataFrame(results, columns=['Assistant', 'Question', 'Initial Response', 'Follow-up Response'])
df.to_excel('results_with_follow_up.xlsx', index=False)