Hello everyone, I have a question about using the OpenAI assistant API with Python.
I am currently trying to split a long text into shorter segments, each containing 30 lines, to ensure they do not exceed the 4096 token limit for prompts.
Initially, I defined a system_prompt that contains guidelines on how to summarize the following input segment text.
I then plan to continuously upload these segmented texts and extract summaries based on the criteria in my system_prompt.
Here’s my current code setup, which successfully creates an assistant visible on the OpenAI website:
https://platform.openai.com/assistants
-
However, I’m unsure how to keep feeding the segmented texts to the assistant in a manner that keeps all interactions under the same thread, similar to how it works in the assistant playground: https://platform.openai.com/playground/assistants
-
Additionally, every time I run a test program, my account at https://platform.openai.com/assistants creates a new assistant. I’ve accumulated hundreds of them by now, almost all with identical content. I was wondering how I could just call an existing assistant ID, as it would be more convenient to directly modify the content of that assistant in the system.
Can anyone guide me on how to proceed with uploading the segmented texts to continue in the same thread, maintaining context as if it was a continuous conversation? Additionally reuse existing assistant created on OpenAI platform?
Thank you.
Here is the code I have written so far:
lines_per_segment = 30
system_prompt = f""guideline prompt"
"""
#API connect
with open('key.txt', 'r') as f:
OPENAI_API_KEY = f.readline()
client = OpenAI(api_key=OPENAI_API_KEY)
#step : Create assistant
assistant = client.beta.assistants.create(
name="Session Summary Analysis",
instructions="You are a professioinal assistant tasked with summarizing the interview data",
model="gpt-3.5-turbo-0125",
)
# step 3: Create thread
thread = client.beta.threads.create()
# step 4: Add user query
client.beta.threads.messages.create(
thread_id=thread.id,
role="user",
content= system_prompt
)
token_count_system = len(encoding.encode(system_prompt))
print(f"The content contains {token_count_system} tokens.")
# step 5: Run assistant
run = client.beta.threads.runs.create(
thread_id=thread.id,
assistant_id=assistant.id
)
print("Assistant ID:", assistant.id)
print("Thread ID:", thread.id)
# step 6: Check run results
while True:
run = client.beta.threads.runs.retrieve(
thread_id=thread.id,
run_id=run.id
)
if run.status == "completed":
break
if run.status == "failed":
print("failed error:", run.last_error)
time.sleep(2)
print("step 6 completed")