Run AoAi thread on different assistants

Mohamed Hussein 650 Reputation points
2025-03-04T21:02:21.05+00:00

Good Day,

At Azure Open Ai Assistants, is that possible to run a Thread at different Assistant?

By other words, is the thread coupled with the Assistant?

If so, tokens will differe because of context window?!

Azure OpenAI Service
Azure OpenAI Service
An Azure service that provides access to OpenAI’s GPT-3 models with enterprise capabilities.
3,777 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Manas Mohanty 1,610 Reputation points Microsoft External Staff
    2025-03-05T12:43:42.16+00:00

    Hi Mohamed Hussein

    I am able to use same thread_id for multiple assistants in below code.

    But I think we have to pass the message from one assistant to another transfer the context and leverage different knowledge resource.

    import os
    import json
    import requests
    import time
    from openai import AzureOpenAI
    
    # Initialize client
    client = AzureOpenAI(
      azure_endpoint="<endpointid>",
      api_key="<endpointkey>",
      api_version="2024-05-01-preview"
    )
    
    # Create the first assistant
    assistant_1 = client.beta.assistants.create(
      model="gpt-4o",  # replace with model deployment name.
      instructions="Assistant 1 instructions",
      tools=[{"type": "code_interpreter"}],
      tool_resources={"code_interpreter": {"file_ids": ["assistant-FhS84mSrTHuzoNimJPF3s3"]}},
      temperature=1,
      top_p=1
    )
    
    # Create the thread
    thread = client.beta.threads.create()
    print("thread id for assistant is ", thread.id)
    
    # Add a user question to the thread (for Assistant 1)
    message = client.beta.threads.messages.create(
      thread_id=thread.id,
      role="user",
      content="Hi from Assistant 1!"  # Replace this with your actual input
    )
    
    # Run the thread for Assistant 1
    run_1 = client.beta.threads.runs.create(
      thread_id=thread.id,
      assistant_id=assistant_1.id
    )
    
    # Wait for the first assistant to complete
    while run_1.status in ['queued', 'in_progress', 'cancelling']:
      time.sleep(1)
      run_1 = client.beta.threads.runs.retrieve(
        thread_id=thread.id,
        run_id=run_1.id
      )
    
    # Output results from Assistant 1
    if run_1.status == 'completed':
      messages= client.beta.threads.messages.list(thread_id=thread.id)
      print("Assistant 1 Messages:", messages)
    elif run_1.status == 'requires_action':
      print("Assistant 1 requires action.")
    else:
      print("Assistant 1 status:", run_1.status)
    
    # Create the second assistant
    assistant_2 = client.beta.assistants.create(
      model="gpt-4o",  # replace with model deployment name.
      instructions="Assistant 2 instructions",
      tools=[{"type": "file_search"}],
      tool_resources={"file_search": {"vector_store_ids": []}},
      temperature=0.8,
      top_p=1
    )
    
    
    # Add another message to the same thread for Assistant 2
    message_2 = client.beta.threads.messages.create(
      thread_id=thread.id,
      role="user",
      content="Hi from Assistant 2!"  # Replace this with your actual input
    )
    
    print("thread id used for assistant 2 is", thread.id)
    # Run the thread for Assistant 2
    run_2 = client.beta.threads.runs.create(
      thread_id=thread.id,
      assistant_id=assistant_2.id
    )
    
    # Wait for the second assistant to complete
    while run_2.status in ['queued', 'in_progress', 'cancelling']:
      time.sleep(1)
      run_2 = client.beta.threads.runs.retrieve(
        thread_id=thread.id,
        run_id=run_2.id
      )
    
    # Output results from Assistant 2
    if run_2.status == 'completed':
      messages_2 = client.beta.threads.messages.list(thread_id=thread.id)
      print("Assistant 2 Messages:", messages_2)
    elif run_2.status == 'requires_action':
      print("Assistant 2 requires action.")
    else:
      print("Assistant 2 status:", run_2.status)
    
    

    Thank you

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.