Simple Python Example for using Assistant API

Hello.
I have build an assistant in the playground, but how can I use this assistant in python? I have read the API documentation and the example, but to be honest I don’t really understand it.
Thanks!

1 Like

is it possible to build GPT to use python for use ?

Sorry, I don’t understant your question. Im running the OpenAI APIs via Streamlit without any problem. But how to call the custom assistant with the API in Streamlit is unclear for me.

1 Like

Hi,
If I am not mistaken you’re supposed to copy your settings into the API.

assistant = client.beta.assistants.create(
  name="Joke teller",
  description="You are a famous comedian, who is known for making jokes about dogs.",
  model="gpt-4-1106-preview",
  tools=[],
  file_ids=[]
)

then you create a thread with an initial message:

thread = client.beta.threads.create(
  messages=[
    {
      "role": "user",
      "content": "Tell a joke about cat",
      "file_ids": []
    }
  ]
)

and finally you run your assistant

run = client.beta.threads.runs.create(
  thread_id=thread.id,
  assistant_id=assistant.id
)

Edit:
I was wrong, it is possible to retrieve existing assistance using its id.

from openai import OpenAI
client = OpenAI()

my_assistant = client.beta.assistants.retrieve("asst_abc123")
print(my_assistant)
3 Likes

OK. Thank you.
But what I mean is, I have created a assistant in Playgorund, with files for Retrieval/Knowledge. I want to use this special assistant via the API to answer user queries.

1 Like

I just released a new YouTube tutorial on the Assistants API. Also, see my GitHub repository with full code for the tutorial.

7 Likes

Thanks a lot!!
The problem was with step 5, as described in your tutorial.

How I run the assistant with below code :

import openai
from openai import OpenAI

# Initialize the client
client = openai.Client(api_key='XXX')
# Memorizzazione del testo in una variabile Python
lv_prompt1 = ("MODALITA' SAP Cerca linee guida e best practices per la generazione di report in formato xlsx da dati di database in ABAP, inclusi metodi per l'invio del file xlsx risultante come allegato via e-mail.")

my_assistant = client.beta.assistants.retrieve("asst_XXX")

Thanks

thread = client.beta.threads.create(
    messages=[
        {
            "role": "user",
            "content": lv_prompt1
        }
    ]
)

run = client.beta.threads.runs.create(
  thread_id=thread.id,
  assistant_id=my_assistant.id
)

while run.status != 'completed':
    run = client.beta.threads.runs.retrieve(
      thread_id=thread.id,
      run_id=run.id
    )
    print(run.status)
    time.sleep(5)


thread_messages = client.beta.threads.messages.list(thread.id)
print(thread_messages)
4 Likes

Thank you very much Sfiluto!

i’m using php/laravel, and this is my old code for using the gpt4 api, now into this code, how can i integrate an assistant(by its name) that i have already created

Capture d'écran 2023-11-20 125219

As this topic has a noted solution, closing topic.