Sviluppare un'app di chat basata su visione

Completato

Tip

Per altri dettagli, vedi la scheda Testo e immagini .

Per sviluppare un'app client che interagisce con chat basate su visione con un modello multifunzione, è possibile usare le stesse tecniche di base usate per le chat basate su testo. È necessaria una connessione all'endpoint in cui viene distribuito il modello e tale endpoint viene usato per inviare richieste costituite da messaggi al modello ed elaborare le risposte.

La differenza principale è che le richieste per una chat basata sulla visione includono messaggi utente in più parti che contengono sia un elemento di contenuto di testo che un elemento di contenuto di immagine.

Diagramma di una sollecitazione in più parti inviata a un modello.

Inviare un prompt basato su immagini usando l'API Risposte

Per includere un'immagine in un prompt usando l'API Risposte , specificare un URL per un file di immagine basato sul Web oppure caricare un'immagine locale e codificare i dati in formato Base64 e inviare un URL nel formato data:image/jpeg;base64,{image_data} (sostituendo "jpeg" con "png" pr altri formati appropriati).

L'esempio python seguente illustra come inviare un'immagine in un prompt usando l'API Risposte :

# Read the image data from a local file
image_path = Path("dragon-fruit.jpeg")
image_format = "jpeg"
with open(image_path, "rb") as image_file:
    image_data = base64.b64encode(image_file.read()).decode("utf-8")

data_url = f"data:image/{image_format};base64,{image_data}" # You can also use a web URL

# Send the image data in a prompt to the model
response = client.responses.create(
    model="gpt-4.1",
    input=[
        {"role": "developer", "content": "You are an AI assistant for chefs planning recipes."},
        {"role": "user", "content": [  
            { "type": "input_text", "text": "What desserts could I make with this?"},
            { "type": "input_image", "image_url": data_url}
        ] } 
    ]
)
print(response.output_text)

Inviare un prompt basato su immagini usando l'API ChatCompletions

Quando si usa l'endpoint OpenAI di Azure per inviare richieste ai modelli che non supportano l'API Risposte , è possibile usare l'API CatCompletions ; Così:

# Read the image data from a local file
image_path = Path("orange.jpeg")
image_format = "jpeg"
with open(image_path, "rb") as image_file:
    image_data = base64.b64encode(image_file.read()).decode("utf-8")

data_url = f"data:image/{image_format};base64,{image_data}" # You can also use a web URL

# Send the image data in a prompt to the model
response = client.chat.completions.create(
    model="Phi-4-multimodal-instruct",
    messages=[
        {"role": "system", "content": "You are an AI assistant for chefs planning recipes."},
        { "role": "user", "content": [  
            { "type": "text", "text": "What can I make with this fruit?"},
            { "type": "image_url", "image_url": {"url": data_url}}
        ] }
    ]
)
print(response.choices[0].message.content)