Assistants API - Access to multiple assistants

This may or may not be what you are looking for. I have two assistants using the same thread. One is a fiction writer the other is a critic. The writer creates a first draft of chapter 1, then has the critic provide feedback, the writer then rewrites the first draft. It may not be what you are looking for, but perhaps it moves you closer to what you are after.

import time

from openai import OpenAI

# gets API Key from environment variable OPENAI_API_KEY
client = OpenAI(
    api_key="your API key",
)

assistantWriter = client.beta.assistants.create(
    name="Writer",
    instructions="You are an expert writer of fictional stories",
    model="gpt-4-1106-preview",
)
assistantCritic = client.beta.assistants.create(
    name="Critic",
    instructions="You are an expert critic of fictional stories. You provide positive and constructive feedback",
    model="gpt-4-1106-preview",
)

thread = client.beta.threads.create()

message = client.beta.threads.messages.create(
    thread_id=thread.id,
    role="user",
    content="""Write a single chapter about a young girl that meets a centaur in the forest. 
    Describe how she feels, what she sees, hears and even smells""",
)


def runAssistant(assistant_id,thread_id,user_instructions):
    run = client.beta.threads.runs.create(
        thread_id=thread_id,
        assistant_id=assistant_id,
        instructions=user_instructions,
    )

    while True:
        run = client.beta.threads.runs.retrieve(thread_id=thread.id, run_id=run.id)

        if run.status == "completed":
            print("This run has completed!"

            break
        else:
            print("in progress...")
            time.sleep(5)
                  
# Run the Writer Assistant to create a first draft                      
runAssistant(assistantWriter.id,thread.id,"Write the first chapter")
                  
# Run the Critic Assistant to provide feedback 
runAssistant(assistantCritic.id,thread.id,"""Provide constructive feedback to what 
the Writer assistant has written""")
                  
# Have the Writer Assistant rewrite the first chapter based on the feedback from the Critic        
runAssistant(assistantWriter.id,thread.id,"""Using the feedback from the Critic Assistant 
rewrite the first chapter""")

# Show the final results 

messages = client.beta.threads.messages.list(
  thread_id=thread.id
)

for thread_message in messages.data:
    # Iterate over the 'content' attribute of the ThreadMessage, which is a list
    for content_item in thread_message.content:
        # Assuming content_item is a MessageContentText object with a 'text' attribute
        # and that 'text' has a 'value' attribute, print it
        print(content_item.text.value) or paste code here
22 Likes