Introduzione agli assistenti di Azure OpenAI (anteprima)

Gli assistenti OpenAI di Azure (anteprima) consentono di creare assistenti di intelligenza artificiale personalizzati in base alle proprie esigenze tramite istruzioni personalizzate e ottimizzati da strumenti avanzati come l'interprete del codice e le funzioni personalizzate. In questo articolo viene fornita una procedura dettagliata per iniziare a usare l’API degli assistenti.

Nota

  • Ricerca file può inserire fino a 10.000 file per assistente, 500 volte più di prima. È veloce, supporta query parallele tramite ricerche multithread e include la riclassificazione avanzata e la riscrittura di query.
    • L’archivio di vettori è un nuovo oggetto nell’API. Dopo l’aggiunta a un archivio di vettori, un file viene analizzato, suddiviso in blocchi, incorporato e preparato per essere sottoposto a ricerca. Gli archivi di vettori possono essere usati in diversi assistenti e thread, semplificando quindi la gestione dei file e la fatturazione.
  • È stato aggiunto il supporto per il parametro tool_choice, che può essere usato per imporre l’uso di uno strumento specifico, ad esempio ricerca file, interprete di codice o una funzione, in una determinata esecuzione.

Supporto degli assistenti

Supporto di area e modelli

L’interprete di codice è disponibile in tutte le aree supportate dagli assistenti di Azure OpenAI. La pagina modelli contiene le informazioni più aggiornate su aree/modelli in cui sono attualmente supportati gli assistenti.

Versioni dell’API

  • 2024-02-15-preview
  • 2024-05-01-preview

Tipi di file supportati

File format Tipo MIME Interprete di codice
c. text/x-c
.cpp text/x-c++
.csv application/csv
DOCX application/vnd.openxmlformats-officedocument.wordprocessingml.document
.html text/html
java. text/x-java
.json application/json
.md text/markdown
PDF applicazione/PDF
.php text/x-php
PPTX application/vnd.openxmlformats-officedocument.presentationml.presentation
.py text/x-python
.py text/x-script.python
.rb text/x-ruby
.tex text/x-tex
.txt text/plain
.css text/css
.jpeg image/jpeg
JPG image/jpeg
js text/javascript
.gif image/gif
.png image/png
.tar application/x-tar
.ts application/typescript
XLSX application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
.xml application/xml or "text/xml"
.zip application/zip

Strumenti

Suggerimento

È stato aggiunto il supporto per il parametro tool_choice, che può essere usato per imporre l’uso di uno strumento specifico, ad esempio file_search, code_interpreter o function, in una determinata esecuzione.

Un singolo assistente può accedere a un massimo di 128 strumenti, tra cui interprete di codice e ricerca file, ma è anche possibile definire strumenti personalizzati tramite funzioni.

File

I file possono essere caricati tramite Studio o a livello di codice. Il parametro file_ids è necessario per fornire a strumenti come code_interpreter l’accesso ai file. Quando si usa l’endpoint di caricamento file, è necessario che purpose sia impostato su assistenti da usare con l’API degli assistenti.

Playground degli assistenti

Viene descritta una procedura dettagliata del playground degli assistenti nell’avvio rapido. In questo modo è disponibile un ambiente senza codice per testare le funzionalità degli assistenti.

Componenti degli assistenti

Componente Descrizione
Assistente Intelligenza artificiale personalizzata che usa i modelli Azure OpenAI insieme agli strumenti.
Thread Sessione di conversazione tra un assistente e un utente. I thread archiviano messaggi e gestiscono automaticamente il troncamento per adattare il contenuto al contesto di un modello.
Messaggio Messaggio creato da un assistente o da un utente. I messaggi possono includere testo, immagini e altri file. I messaggi vengono archiviati come elenco nel thread.
Run Attivazione di un assistente per iniziare l’esecuzione in base al contenuto del thread. L’assistente usa la configurazione e i messaggi del thread per eseguire attività chiamando modelli e strumenti. Nell’ambito di un’esecuzione, l’assistente aggiunge messaggi al thread.
Eseguire il passaggio Elenco dettagliato dei passaggi eseguiti dall’assistente come parte di un’esecuzione. Un assistente può chiamare strumenti o creare messaggi durante l’esecuzione. L’analisi dei passaggi di esecuzione consente di comprendere come l’assistente ottiene i risultati finali.

Creare il primo assistente

Creare un assistente

Per questo esempio verrà creato un assistente che scrive codice per generare visualizzazioni usando le funzionalità dello strumento code_interpreter. Gli esempi seguenti devono essere eseguiti in sequenza in un ambiente come Jupyter Notebook.

import os
import json
from openai import AzureOpenAI
    
client = AzureOpenAI(
    api_key=os.getenv("AZURE_OPENAI_API_KEY"),  
    api_version="2024-05-01-preview",
    azure_endpoint = os.getenv("AZURE_OPENAI_ENDPOINT")
    )

# Create an assistant
assistant = client.beta.assistants.create(
    name="Data Visualization",
    instructions=f"You are a helpful AI assistant who makes interesting visualizations based on data." 
    f"You have access to a sandboxed environment for writing and testing code."
    f"When you are asked to create a visualization you should follow these steps:"
    f"1. Write the code."
    f"2. Anytime you write new code display a preview of the code to show your work."
    f"3. Run the code to confirm that it runs."
    f"4. If the code is successful display the visualization."
    f"5. If the code is unsuccessful display the error message and try to revise the code and rerun going through the steps from above again.",
    tools=[{"type": "code_interpreter"}],
    model="gpt-4-1106-preview" #You must replace this value with the deployment name for your model.
)

La configurazione precedente contiene alcuni dettagli da tenere presenti:

  • Questo assistente può accedere all’interprete di codice con la riga tools=[{"type": "code_interpreter"}],. In questo modo, il modello può accedere a un ambiente Python sandbox per eseguire codice per aiutare a formulare le risposte alla domanda di un utente.
  • Nelle istruzioni viene ricordato al modello che può eseguire il codice. A volte il modello necessita di supporto per orientarsi verso lo strumento corretto per risolvere una determinata query. Se si sa di voler usare una particolare libreria per generare una determinata risposta che si sa essere parte dell’interprete di codice, può essere utile fornire indicazioni come “Usare Matplotlib per fare x”.
  • Poiché si tratta di Azure OpenAI, il valore immesso per model= deve corrispondere al nome della distribuzione.

A questo punto si stamperà il contenuto dell’assistente appena creato per verificare che la creazione sia riuscita:

print(assistant.model_dump_json(indent=2))
{
  "id": "asst_7AZSrv5I3XzjUqWS40X5UgRr",
  "created_at": 1705972454,
  "description": null,
  "file_ids": [],
  "instructions": "You are a helpful AI assistant who makes interesting visualizations based on data.You have access to a sandboxed environment for writing and testing code.When you are asked to create a visualization you should follow these steps:1. Write the code.2. Anytime you write new code display a preview of the code to show your work.3. Run the code to confirm that it runs.4. If the code is successful display the visualization.5. If the code is unsuccessful display the error message and try to revise the code and rerun going through the steps from above again.",
  "metadata": {},
  "model": "gpt-4-1106-preview",
  "name": "Data Visualization",
  "object": "assistant",
  "tools": [
    {
      "type": "code_interpreter"
    }
  ]
}

Creare un thread

A questo punto si creerà un thread.

# Create a thread
thread = client.beta.threads.create()
print(thread)
Thread(id='thread_6bunpoBRZwNhovwzYo7fhNVd', created_at=1705972465, metadata={}, object='thread')

Un thread è essenzialmente il record della sessione di conversazione tra l’assistente e l’utente. È simile all’array/elenco di messaggi in una tipica chiamata API di completamento della chat. Una delle differenze principali è che: a differenza di una matrice di messaggi di completamento della chat, non è necessario tenere traccia dei token con ogni chiamata per assicurarsi di rimanere al di sotto della lunghezza del contesto del modello. I thread rimuovono questo dettaglio di gestione senza autorizzazione e comprimeranno la cronologia dei thread in base alle esigenze per consentire la continuazione della conversazione. La possibilità per i thread di eseguire questa operazione con conversazioni più grandi è migliorata quando si usano i modelli più recenti, che hanno lunghezze di contesto più grandi e supporto per le funzionalità più recenti.

Creare quindi la prima domanda dell’utente da aggiungere al thread.

# Add a user question to the thread
message = client.beta.threads.messages.create(
    thread_id=thread.id,
    role="user",
    content="Create a visualization of a sinewave"
)

Elencare i messaggi del thread

thread_messages = client.beta.threads.messages.list(thread.id)
print(thread_messages.model_dump_json(indent=2))
{
  "data": [
    {
      "id": "msg_JnkmWPo805Ft8NQ0gZF6vA2W",
      "assistant_id": null,
      "content": [
        {
          "text": {
            "annotations": [],
            "value": "Create a visualization of a sinewave"
          },
          "type": "text"
        }
      ],
      "created_at": 1705972476,
      "file_ids": [],
      "metadata": {},
      "object": "thread.message",
      "role": "user",
      "run_id": null,
      "thread_id": "thread_6bunpoBRZwNhovwzYo7fhNVd"
    }
  ],
  "object": "list",
  "first_id": "msg_JnkmWPo805Ft8NQ0gZF6vA2W",
  "last_id": "msg_JnkmWPo805Ft8NQ0gZF6vA2W",
  "has_more": false
}

Eseguire il thread

run = client.beta.threads.runs.create(
  thread_id=thread.id,
  assistant_id=assistant.id,
  #instructions="New instructions" #You can optionally provide new instructions but these will override the default instructions
)

È anche possibile passare un parametro instructions qui, ma questa operazione sostituisce le istruzioni esistenti già fornite per l’assistente.

Recuperare lo stato del thread

# Retrieve the status of the run
run = client.beta.threads.runs.retrieve(
  thread_id=thread.id,
  run_id=run.id
)

status = run.status
print(status)
completed

A seconda della complessità della query eseguita, l’esecuzione del thread potrebbe richiedere più tempo. In tal caso è possibile creare un ciclo per monitorare lo stato di esecuzione del thread con codice simile all’esempio seguente:

import time
from IPython.display import clear_output

start_time = time.time()

status = run.status

while status not in ["completed", "cancelled", "expired", "failed"]:
    time.sleep(5)
    run = client.beta.threads.runs.retrieve(thread_id=thread.id,run_id=run.id)
    print("Elapsed time: {} minutes {} seconds".format(int((time.time() - start_time) // 60), int((time.time() - start_time) % 60)))
    status = run.status
    print(f'Status: {status}')
    clear_output(wait=True)

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

print(f'Status: {status}')
print("Elapsed time: {} minutes {} seconds".format(int((time.time() - start_time) // 60), int((time.time() - start_time) % 60)))
print(messages.model_dump_json(indent=2))

Quando un’esecuzione è nello stato in_progress o in altri stati non finali, il thread è bloccato. Quando un thread è bloccato, non è possibile aggiungere nuovi messaggi e non è possibile creare nuove esecuzioni.

Elencare i messaggi di thread dopo l’esecuzione

Quando lo stato dell’esecuzione indica il completamento, è possibile elencare di nuovo il contenuto del thread per recuperare la risposta del modello e qualsiasi risposta degli strumenti:

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

print(messages.model_dump_json(indent=2))
{
  "data": [
    {
      "id": "msg_M5pz73YFsJPNBbWvtVs5ZY3U",
      "assistant_id": "asst_eHwhP4Xnad0bZdJrjHO2hfB4",
      "content": [
        {
          "text": {
            "annotations": [],
            "value": "Is there anything else you would like to visualize or any additional features you'd like to add to the sine wave plot?"
          },
          "type": "text"
        }
      ],
      "created_at": 1705967782,
      "file_ids": [],
      "metadata": {},
      "object": "thread.message",
      "role": "assistant",
      "run_id": "run_AGQHJrrfV3eM0eI9T3arKgYY",
      "thread_id": "thread_ow1Yv29ptyVtv7ixbiKZRrHd"
    },
    {
      "id": "msg_oJbUanImBRpRran5HSa4Duy4",
      "assistant_id": "asst_eHwhP4Xnad0bZdJrjHO2hfB4",
      "content": [
        {
          "image_file": {
            "file_id": "assistant-1YGVTvNzc2JXajI5JU9F0HMD"
          },
          "type": "image_file"
        },
        {
          "text": {
            "annotations": [],
            "value": "Here is the visualization of a sine wave: \n\nThe wave is plotted using values from 0 to \\( 4\\pi \\) on the x-axis, and the corresponding sine values on the y-axis. I've also added grid lines for easier reading of the plot."
          },
          "type": "text"
        }
      ],
      "created_at": 1705967044,
      "file_ids": [],
      "metadata": {},
      "object": "thread.message",
      "role": "assistant",
      "run_id": "run_8PsweDFn6gftUd91H87K0Yts",
      "thread_id": "thread_ow1Yv29ptyVtv7ixbiKZRrHd"
    },
    {
      "id": "msg_Pu3eHjM10XIBkwqh7IhnKKdG",
      "assistant_id": null,
      "content": [
        {
          "text": {
            "annotations": [],
            "value": "Create a visualization of a sinewave"
          },
          "type": "text"
        }
      ],
      "created_at": 1705966634,
      "file_ids": [],
      "metadata": {},
      "object": "thread.message",
      "role": "user",
      "run_id": null,
      "thread_id": "thread_ow1Yv29ptyVtv7ixbiKZRrHd"
    }
  ],
  "object": "list",
  "first_id": "msg_M5pz73YFsJPNBbWvtVs5ZY3U",
  "last_id": "msg_Pu3eHjM10XIBkwqh7IhnKKdG",
  "has_more": false
}

Recuperare l’ID file

È stato richiesto che il modello generi un’immagine di un’onda sinusoidale. Per scaricare l’immagine, è prima necessario recuperare l’ID file di immagini.

data = json.loads(messages.model_dump_json(indent=2))  # Load JSON data into a Python object
image_file_id = data['data'][0]['content'][0]['image_file']['file_id']

print(image_file_id)  # Outputs: assistant-1YGVTvNzc2JXajI5JU9F0HMD

Download immagine

content = client.files.content(image_file_id)

image= content.write_to_file("sinewave.png")

Aprire l’immagine in locale dopo il download:

from PIL import Image

# Display the image in the default image viewer
image = Image.open("sinewave.png")
image.show()

Screenshot dell’onda sinusoidale generata dall’interprete del codice.

Porre una domanda di completamento sul thread

Poiché l’assistente non ha seguito le istruzioni e include il codice eseguito nella parte di testo della risposta, richiedere esplicitamente tali informazioni.

# Add a new user question to the thread
message = client.beta.threads.messages.create(
    thread_id=thread.id,
    role="user",
    content="Show me the code you used to generate the sinewave"
)

Anche in questo caso è necessario eseguire e recuperare lo stato del thread:

run = client.beta.threads.runs.create(
  thread_id=thread.id,
  assistant_id=assistant.id,
  #instructions="New instructions" #You can optionally provide new instructions  but these will override the default instructions
)

# Retrieve the status of the run
run = client.beta.threads.runs.retrieve(
  thread_id=thread.id,
  run_id=run.id
)

status = run.status
print(status)

completed

Una volta che lo stato dell’esecuzione è completato, verranno elencati nuovamente i messaggi nel thread che ora dovrebbero includere la risposta all’ultima domanda.

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

print(messages.model_dump_json(indent=2))
{
  "data": [
    {
      "id": "msg_oaF1PUeozAvj3KrNnbKSy4LQ",
      "assistant_id": "asst_eHwhP4Xnad0bZdJrjHO2hfB4",
      "content": [
        {
          "text": {
            "annotations": [],
            "value": "Certainly, here is the code I used to generate the sine wave visualization:\n\n```python\nimport numpy as np\nimport matplotlib.pyplot as plt\n\n# Generating data for the sinewave\nx = np.linspace(0, 4 * np.pi, 1000)  # Generate values from 0 to 4*pi\ny = np.sin(x)  # Compute the sine of these values\n\n# Plotting the sine wave\nplt.plot(x, y)\nplt.title('Sine Wave')\nplt.xlabel('x')\nplt.ylabel('sin(x)')\nplt.grid(True)\nplt.show()\n```\n\nThis code snippet uses `numpy` to generate an array of x values and then computes the sine for each x value. It then uses `matplotlib` to plot these values and display the resulting graph."
          },
          "type": "text"
        }
      ],
      "created_at": 1705969710,
      "file_ids": [],
      "metadata": {},
      "object": "thread.message",
      "role": "assistant",
      "run_id": "run_oDS3fH7NorCUVwROTZejKcZN",
      "thread_id": "thread_ow1Yv29ptyVtv7ixbiKZRrHd"
    },
    {
      "id": "msg_moYE3aNwFYuRq2aXpxpt2Wb0",
      "assistant_id": null,
      "content": [
        {
          "text": {
            "annotations": [],
            "value": "Show me the code you used to generate the sinewave"
          },
          "type": "text"
        }
      ],
      "created_at": 1705969678,
      "file_ids": [],
      "metadata": {},
      "object": "thread.message",
      "role": "user",
      "run_id": null,
      "thread_id": "thread_ow1Yv29ptyVtv7ixbiKZRrHd"
    },
    {
      "id": "msg_M5pz73YFsJPNBbWvtVs5ZY3U",
      "assistant_id": "asst_eHwhP4Xnad0bZdJrjHO2hfB4",
      "content": [
        {
          "text": {
            "annotations": [],
            "value": "Is there anything else you would like to visualize or any additional features you'd like to add to the sine wave plot?"
          },
          "type": "text"
        }
      ],
      "created_at": 1705967782,
      "file_ids": [],
      "metadata": {},
      "object": "thread.message",
      "role": "assistant",
      "run_id": "run_AGQHJrrfV3eM0eI9T3arKgYY",
      "thread_id": "thread_ow1Yv29ptyVtv7ixbiKZRrHd"
    },
    {
      "id": "msg_oJbUanImBRpRran5HSa4Duy4",
      "assistant_id": "asst_eHwhP4Xnad0bZdJrjHO2hfB4",
      "content": [
        {
          "image_file": {
            "file_id": "assistant-1YGVTvNzc2JXajI5JU9F0HMD"
          },
          "type": "image_file"
        },
        {
          "text": {
            "annotations": [],
            "value": "Here is the visualization of a sine wave: \n\nThe wave is plotted using values from 0 to \\( 4\\pi \\) on the x-axis, and the corresponding sine values on the y-axis. I've also added grid lines for easier reading of the plot."
          },
          "type": "text"
        }
      ],
      "created_at": 1705967044,
      "file_ids": [],
      "metadata": {},
      "object": "thread.message",
      "role": "assistant",
      "run_id": "run_8PsweDFn6gftUd91H87K0Yts",
      "thread_id": "thread_ow1Yv29ptyVtv7ixbiKZRrHd"
    },
    {
      "id": "msg_Pu3eHjM10XIBkwqh7IhnKKdG",
      "assistant_id": null,
      "content": [
        {
          "text": {
            "annotations": [],
            "value": "Create a visualization of a sinewave"
          },
          "type": "text"
        }
      ],
      "created_at": 1705966634,
      "file_ids": [],
      "metadata": {},
      "object": "thread.message",
      "role": "user",
      "run_id": null,
      "thread_id": "thread_ow1Yv29ptyVtv7ixbiKZRrHd"
    }
  ],
  "object": "list",
  "first_id": "msg_oaF1PUeozAvj3KrNnbKSy4LQ",
  "last_id": "msg_Pu3eHjM10XIBkwqh7IhnKKdG",
  "has_more": false
}

Per estrarre solo la risposta all’ultima domanda:

data = json.loads(messages.model_dump_json(indent=2))
code = data['data'][0]['content'][0]['text']['value']
print(code)

Certamente, ecco il codice usato per generare la visualizzazione dell’onda sinusoidale:

import numpy as np
import matplotlib.pyplot as plt

# Generating data for the sinewave
x = np.linspace(0, 4 * np.pi, 1000)  # Generate values from 0 to 4*pi
y = np.sin(x)  # Compute the sine of these values

# Plotting the sine wave
plt.plot(x, y)
plt.title('Sine Wave')
plt.xlabel('x')
plt.ylabel('sin(x)')
plt.grid(True)
plt.show()

Modalità scura

Aggiungere un’ultima domanda al thread per verificare se l’interprete di codice può scambiare il grafico in modalità scura.

# Add a user question to the thread
message = client.beta.threads.messages.create(
    thread_id=thread.id,
    role="user",
    content="I prefer visualizations in darkmode can you change the colors to make a darkmode version of this visualization."
)

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

# Retrieve the status of the run
run = client.beta.threads.runs.retrieve(
  thread_id=thread.id,
  run_id=run.id
)

status = run.status
print(status)
completed
messages = client.beta.threads.messages.list(
  thread_id=thread.id
)

print(messages.model_dump_json(indent=2))
{
  "data": [
    {
      "id": "msg_KKzOHCArWGvGpuPo0pVZTHgV",
      "assistant_id": "asst_eHwhP4Xnad0bZdJrjHO2hfB4",
      "content": [
        {
          "text": {
            "annotations": [],
            "value": "You're viewing the dark mode version of the sine wave visualization in the image above. The plot is set against a dark background with a cyan colored sine wave for better contrast and visibility. If there's anything else you'd like to adjust or any other assistance you need, feel free to let me know!"
          },
          "type": "text"
        }
      ],
      "created_at": 1705971199,
      "file_ids": [],
      "metadata": {},
      "object": "thread.message",
      "role": "assistant",
      "run_id": "run_izZFyTVB1AlFM1VVMItggRn4",
      "thread_id": "thread_ow1Yv29ptyVtv7ixbiKZRrHd"
    },
    {
      "id": "msg_30pXFVYNgP38qNEMS4Zbozfk",
      "assistant_id": null,
      "content": [
        {
          "text": {
            "annotations": [],
            "value": "I prefer visualizations in darkmode can you change the colors to make a darkmode version of this visualization."
          },
          "type": "text"
        }
      ],
      "created_at": 1705971194,
      "file_ids": [],
      "metadata": {},
      "object": "thread.message",
      "role": "user",
      "run_id": null,
      "thread_id": "thread_ow1Yv29ptyVtv7ixbiKZRrHd"
    },
    {
      "id": "msg_3j31M0PaJLqO612HLKVsRhlw",
      "assistant_id": "asst_eHwhP4Xnad0bZdJrjHO2hfB4",
      "content": [
        {
          "image_file": {
            "file_id": "assistant-kfqzMAKN1KivQXaEJuU0u9YS"
          },
          "type": "image_file"
        },
        {
          "text": {
            "annotations": [],
            "value": "Here is the dark mode version of the sine wave visualization. I've used the 'dark_background' style in Matplotlib and chosen a cyan color for the plot line to ensure it stands out against the dark background."
          },
          "type": "text"
        }
      ],
      "created_at": 1705971123,
      "file_ids": [],
      "metadata": {},
      "object": "thread.message",
      "role": "assistant",
      "run_id": "run_B91erEPWro4bZIfryQeIDDlx",
      "thread_id": "thread_ow1Yv29ptyVtv7ixbiKZRrHd"
    },
    {
      "id": "msg_FgDZhBvvM1CLTTFXwgeJLdua",
      "assistant_id": null,
      "content": [
        {
          "text": {
            "annotations": [],
            "value": "I prefer visualizations in darkmode can you change the colors to make a darkmode version of this visualization."
          },
          "type": "text"
        }
      ],
      "created_at": 1705971052,
      "file_ids": [],
      "metadata": {},
      "object": "thread.message",
      "role": "user",
      "run_id": null,
      "thread_id": "thread_ow1Yv29ptyVtv7ixbiKZRrHd"
    },
    {
      "id": "msg_oaF1PUeozAvj3KrNnbKSy4LQ",
      "assistant_id": "asst_eHwhP4Xnad0bZdJrjHO2hfB4",
      "content": [
        {
          "text": {
            "annotations": [],
            "value": "Certainly, here is the code I used to generate the sine wave visualization:\n\n```python\nimport numpy as np\nimport matplotlib.pyplot as plt\n\n# Generating data for the sinewave\nx = np.linspace(0, 4 * np.pi, 1000)  # Generate values from 0 to 4*pi\ny = np.sin(x)  # Compute the sine of these values\n\n# Plotting the sine wave\nplt.plot(x, y)\nplt.title('Sine Wave')\nplt.xlabel('x')\nplt.ylabel('sin(x)')\nplt.grid(True)\nplt.show()\n```\n\nThis code snippet uses `numpy` to generate an array of x values and then computes the sine for each x value. It then uses `matplotlib` to plot these values and display the resulting graph."
          },
          "type": "text"
        }
      ],
      "created_at": 1705969710,
      "file_ids": [],
      "metadata": {},
      "object": "thread.message",
      "role": "assistant",
      "run_id": "run_oDS3fH7NorCUVwROTZejKcZN",
      "thread_id": "thread_ow1Yv29ptyVtv7ixbiKZRrHd"
    },
    {
      "id": "msg_moYE3aNwFYuRq2aXpxpt2Wb0",
      "assistant_id": null,
      "content": [
        {
          "text": {
            "annotations": [],
            "value": "Show me the code you used to generate the sinewave"
          },
          "type": "text"
        }
      ],
      "created_at": 1705969678,
      "file_ids": [],
      "metadata": {},
      "object": "thread.message",
      "role": "user",
      "run_id": null,
      "thread_id": "thread_ow1Yv29ptyVtv7ixbiKZRrHd"
    },
    {
      "id": "msg_M5pz73YFsJPNBbWvtVs5ZY3U",
      "assistant_id": "asst_eHwhP4Xnad0bZdJrjHO2hfB4",
      "content": [
        {
          "text": {
            "annotations": [],
            "value": "Is there anything else you would like to visualize or any additional features you'd like to add to the sine wave plot?"
          },
          "type": "text"
        }
      ],
      "created_at": 1705967782,
      "file_ids": [],
      "metadata": {},
      "object": "thread.message",
      "role": "assistant",
      "run_id": "run_AGQHJrrfV3eM0eI9T3arKgYY",
      "thread_id": "thread_ow1Yv29ptyVtv7ixbiKZRrHd"
    },
    {
      "id": "msg_oJbUanImBRpRran5HSa4Duy4",
      "assistant_id": "asst_eHwhP4Xnad0bZdJrjHO2hfB4",
      "content": [
        {
          "image_file": {
            "file_id": "assistant-1YGVTvNzc2JXajI5JU9F0HMD"
          },
          "type": "image_file"
        },
        {
          "text": {
            "annotations": [],
            "value": "Here is the visualization of a sine wave: \n\nThe wave is plotted using values from 0 to \\( 4\\pi \\) on the x-axis, and the corresponding sine values on the y-axis. I've also added grid lines for easier reading of the plot."
          },
          "type": "text"
        }
      ],
      "created_at": 1705967044,
      "file_ids": [],
      "metadata": {},
      "object": "thread.message",
      "role": "assistant",
      "run_id": "run_8PsweDFn6gftUd91H87K0Yts",
      "thread_id": "thread_ow1Yv29ptyVtv7ixbiKZRrHd"
    },
    {
      "id": "msg_Pu3eHjM10XIBkwqh7IhnKKdG",
      "assistant_id": null,
      "content": [
        {
          "text": {
            "annotations": [],
            "value": "Create a visualization of a sinewave"
          },
          "type": "text"
        }
      ],
      "created_at": 1705966634,
      "file_ids": [],
      "metadata": {},
      "object": "thread.message",
      "role": "user",
      "run_id": null,
      "thread_id": "thread_ow1Yv29ptyVtv7ixbiKZRrHd"
    }
  ],
  "object": "list",
  "first_id": "msg_KKzOHCArWGvGpuPo0pVZTHgV",
  "last_id": "msg_Pu3eHjM10XIBkwqh7IhnKKdG",
  "has_more": false
}

Estrarre il nuovo ID file di immagine e scaricare e visualizzare l’immagine:

data = json.loads(messages.model_dump_json(indent=2))  # Load JSON data into a Python object
image_file_id = data['data'][0]['content'][0]['image_file']['file_id'] # index numbers can vary if you have had a different conversation over the course of the thread.

print(image_file_id)

content = client.files.content(image_file_id)
image= content.write_to_file("dark_sine.png")

# Display the image in the default image viewer
image = Image.open("dark_sine.png")
image.show()

Screenshot dell’onda sinusoidale generata dall’interprete di codice in modalità scura.

Riferimenti aggiuntivi

Definizioni degli stati di esecuzione

Stato Definizione
queued Quando le esecuzioni vengono create per la prima volta o quando si completa required_action, vengono spostate in uno stato in coda. Dovrebbero passare quasi immediatamente a in_progress.
in_progress Mentre sono nello stesso in_progress, l’assistente usa il modello e gli strumenti per eseguire i passaggi. È possibile visualizzare lo stato di avanzamento dell’esecuzione esaminando i passaggi dell’esecuzione.
completed Esecuzione completata. È ora possibile visualizzare tutti i messaggi aggiunti dall’assistente al thread e tutti i passaggi eseguiti dall’esecuzione. È anche possibile continuare la conversazione aggiungendo altri messaggi utente al thread e creando un’altra esecuzione.
requires_action Quando si usa lo strumento di chiamata della funzione, l’esecuzione passerà a uno stato required_action dopo che il modello determina i nomi e gli argomenti delle funzioni da chiamare. È quindi necessario eseguire tali funzioni e inviare gli output prima che l’esecuzione proceda. Se gli output non vengono forniti prima che il timestamp expires_at passi, circa 10 minuti dopo la creazione, l’esecuzione passerà a uno stato scaduto.
expired Ciò si verifica quando la funzione che chiama gli output non è stata inviata prima di expires_at e l’esecuzione scade. Inoltre, se le esecuzioni richiedono troppo tempo per l’esecuzione e vanno oltre il tempo indicato in expires_at, i sistemi faranno scadere l’esecuzione.
cancelling È possibile tentare di annullare un in_progress eseguito usando l’endpoint di annullamento dell’esecuzione. Quando il tentativo di annullamento avrà esito positivo, lo stato dell’esecuzione passerà ad annullato. L’annullamento viene tentato ma non garantito.
cancelled L’esecuzione è stata annullata correttamente.
failed È possibile visualizzare il motivo dell’errore esaminando l’oggetto last_error nell’esecuzione. Il timestamp per l’errore verrà registrato in failed_at.

Annotazioni di messaggio

Le annotazioni dei messaggi di assistente sono diverse dalle annotazioni di filtro del contenuto presenti nelle risposte API di completamento e completamento della chat. Le annotazioni dell’assistente possono verificarsi all’interno della matrice di contenuto dell’oggetto. Le annotazioni forniscono informazioni su come annotare il testo nelle risposte all’utente.

Quando le annotazioni sono presenti nella matrice di contenuto del messaggio, verranno visualizzate sottostringhe generate dal modello non leggibili nel testo, che dovranno essere sostituite con le annotazioni corrette. Queste stringhe potrebbero essere simili a 【13†source】 o sandbox:/mnt/data/file.csv. Ecco un frammento di codice Python di OpenAI che sostituisce queste stringhe con le informazioni presenti nelle annotazioni.


from openai import AzureOpenAI
    
client = AzureOpenAI(
    api_key=os.getenv("AZURE_OPENAI_API_KEY"),  
    api_version="2024-05-01-preview",
    azure_endpoint = os.getenv("AZURE_OPENAI_ENDPOINT")
    )

# Retrieve the message object
message = client.beta.threads.messages.retrieve(
  thread_id="...",
  message_id="..."
)

# Extract the message content
message_content = message.content[0].text
annotations = message_content.annotations
citations = []

# Iterate over the annotations and add footnotes
for index, annotation in enumerate(annotations):
    # Replace the text with a footnote
    message_content.value = message_content.value.replace(annotation.text, f' [{index}]')

    # Gather citations based on annotation attributes
    if (file_citation := getattr(annotation, 'file_citation', None)):
        cited_file = client.files.retrieve(file_citation.file_id)
        citations.append(f'[{index}] {file_citation.quote} from {cited_file.filename}')
    elif (file_path := getattr(annotation, 'file_path', None)):
        cited_file = client.files.retrieve(file_path.file_id)
        citations.append(f'[{index}] Click <here> to download {cited_file.filename}')
        # Note: File download functionality not implemented above for brevity

# Add footnotes to the end of the message before displaying to user
message_content.value += '\n' + '\n'.join(citations)

Annotazione del messaggio Descrizione
file_citation Le citazioni di file vengono create dallo strumento di recupero e definiscono riferimenti a un’offerta specifica in un file specifico, caricato e usato dall’assistente per generare la risposta.
file_path Le annotazioni del percorso file vengono create dallo strumento code_interpreter e contengono riferimenti ai file generati dallo strumento.

Vedi anche