I am trying to connect to an existing chat thread with a thread id I have stored in a json file, but I am getting an error using the retrieve command. I am doing this so that it doesn't spam and create threads every time I run my code.
This is the error message produced: Failed to connect to the GPT thread: 'Thread' object is not subscriptable
Here is the relevant function:
def connect_helmsman_gpt():
config = load_config()
api_key = config.get("api_key")
thread_id = config.get("thread_id")
if thread_id is None:
print("No thread ID found in config")
return None, None, None
try:
client = OpenAI(api_key=api_key)
thread = client.beta.threads.retrieve(thread_id)
assistant = client.beta.assistants.retrieve(thread["assistant"])
return assistant, client, thread
except Exception as e:
print(f"Failed to connect to the GPT thread: {e}")
return None, None, None
Problem
The following line caused the
'Thread' object is not subscriptableerror:Solution
Change this...
...to this.
Note: That will solve the error, but you want to use the same assistant and the same thread every time you run the code, right? Well, you "merge" the assistant and thread when you run the assistant by passing the
thread_idandassistant_idparameters. I suggest you take a look at my YouTube tutorial and code on my GitHub profile regarding this step.Full code
If you run
test.py, the OpenAI API will return the following:test.py