# How to select an already created assistant in the program?

**URL:** <https://community.openai.com/t/how-to-select-an-already-created-assistant-in-the-program/587559>\
**Category:** API\
**Tags:** gpt-4, api, assistants, assistants-api\
**Created:** [January 15, 2024, 11:42am UTC](https://community.openai.com/t/how-to-select-an-already-created-assistant-in-the-program/587559 "2024-01-15T11:42:18Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![rasheeda.sanjeev.lil](https://avatars.discourse-cdn.com/v4/letter/r/8e7dd6/32.png) [@rasheeda.sanjeev.lil](https://community.openai.com/u/rasheeda.sanjeev.lil)\
**Post date:** [January 15, 2024, 11:42am UTC](https://community.openai.com/t/how-to-select-an-already-created-assistant-in-the-program/587559/1 "2024-01-15T11:42:18Z")

</div>

I can use code to create Assistants and Threads, and I saw the same post in the forum, which made me understand that I can see these created Assistants in the Playground page

```auto
from OpenAI import OpenAI
client = OpenAI(api_key="api_key")

assistant = client.beta.assistant.create(
    name = "",
    instructions = "",
    tools = [{"type":""}],
    model = "gpt-4-1106-preview"
)

thread = client.beta.threads.create()
print(thread)

messages = client.beta.threads.messages.create(
    thread_id = thread.id,
    role = "user",
    content = ""
)

```

My question is, if this Python script is used as an API call file to be connected to my other project programs, does that mean that every time the activation program sends a command to Assistants, a new Assistant will be created?

So can I make the program call the previously created Assistants every time it runs? Or, can you create an assistant directly in the Playground and then select the assistant directly in the Python script?

Because if you need to create a new assistant and upload reference files every time, this will become very expensive.  
🙂

---

<div class="post-metadata">

**Author:** ![alappin](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/alappin/32/282738_2.png) [@alappin](https://community.openai.com/u/alappin)\
**Post date:** [January 19, 2024, 3:48am UTC](https://community.openai.com/t/how-to-select-an-already-created-assistant-in-the-program/587559/2 "2024-01-19T03:48:45Z")

</div>

The script you posted will _always create a new assistant_ when it is executed. This is probably not what you want. Ideally you only create the assistant once and then use its ID when executing a run on either a new thread or an existing thread of messages.

The actual details will depend on how your application works. There are a couple of scenarios:

1. If your application has to reuse an assistant that it previously created, then you could just store the assistant\_id in your database after creating an assistant. Fetch and reuse this assistant\_id later on.

2. If you don’t know the assistant\_id in advance you could call the **List assistants** endpoint to list assistants and then select the one you are after by its name. Check the OpenAI _API reference_ docs for the endpoints.

I also suggest you read the **Assistants Overview** documentation on OpenAI to understand how assistants work and the nuances involved. e.g. polling for run statuses, e.t.c

---

<div class="post-metadata">

**Author:** ![rasheeda.sanjeev.lil](https://avatars.discourse-cdn.com/v4/letter/r/8e7dd6/32.png) [@rasheeda.sanjeev.lil](https://community.openai.com/u/rasheeda.sanjeev.lil)\
**Post date:** [January 23, 2024, 1:59pm UTC](https://community.openai.com/t/how-to-select-an-already-created-assistant-in-the-program/587559/3 "2024-01-23T13:59:21Z")

</div>

Therefore, when the script is first activated, it is allowed to execute the command to query the target Assistant ID. For example, in the following form？

```auto
client = OpenAI(api_key=api_key)

assistant_id = 'asst_SfkU69NBrlE3IHMRzl3kNBtl'

def check_assistant_exists(assistant_id):
    try:
        response = client.beta.assistants.retrieve(assistant_id)
        return True if response else False
    except Exception as e:
        print(f"An error occurred while checking the assistant: {e}", file=sys.stderr)
        return False

if not check_assistant_exists(assistant_id):
    # Assistant creation logic - Ensure values are from config or inputs
    assistant = client.beta.assistants.create(
        name=os.getenv("ASSISTANT_NAME", "Default Assistant Name"),
        instructions=os.getenv("ASSISTANT_INSTRUCTIONS", "Default Instructions"),
        tools=[{"type": "code_interpreter"}],
        model="gpt-4"
    )
    assistant_id = assistant["id"]

# Create a thread
thread = client.beta.threads.create()

```

🤔

---

<div class="post-metadata">

**Author:** ![jason123](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/jason123/32/449842_2.png) [@jason123](https://community.openai.com/u/jason123)\
**Post date:** [May 3, 2024, 6:40am UTC](https://community.openai.com/t/how-to-select-an-already-created-assistant-in-the-program/587559/4 "2024-05-03T06:40:02Z")

</div>

I’m sorry to bother you, but has this issue been resolved? I am facing a similar problem. If I create an assistant on the OpenAI platform ([https://platform.openai.com/assistants](https://platform.openai.com/assistants)), how can I directly invoke this specific assistant and ask a series of questions, ensuring that all these questions and the assistant’s responses are executed within the same thread, similar to how it works in the assistant playground ?

Thank you

---

<div class="post-metadata">

**Author:** ![nicknovoa](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/nicknovoa/32/359087_2.png) [@nicknovoa](https://community.openai.com/u/nicknovoa)\
**Post date:** [May 6, 2024, 8:12pm UTC](https://community.openai.com/t/how-to-select-an-already-created-assistant-in-the-program/587559/6 "2024-05-06T20:12:55Z")

</div>

Hey I was curios about this same problem and found a solution!

In the documentation for Assistants, for step 4 “Create a Run”, it details how to add the assistant id of an existing assistant.

```auto
with client.beta.threads.runs.stream(
  thread_id=thread.id,
  assistant_id=assistant.id,
  instructions="Please address the user as Jane Doe. The user has a premium account.",
  event_handler=EventHandler(),
) as stream:
  stream.until_done()

```

all you have to do is set your assistant\_id.

Here is the official Docs - [Create a Run](https://platform.openai.com/docs/assistants/overview/step-4-create-a-run)

Hope this helps!

---

<div class="post-metadata">

**Author:** ![htec](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/htec/32/471415_2.png) [@htec](https://community.openai.com/u/htec)\
**Post date:** [October 1, 2024, 6:46am UTC](https://community.openai.com/t/how-to-select-an-already-created-assistant-in-the-program/587559/7 "2024-10-01T06:46:02Z")

</div>

A little correction, it’s Step 5 now.

**[Step 5: Create a run and check the output](https://platform.openai.com/docs/assistants/tools/file-search/step-5-create-a-run-and-check-the-output)**
