Chiamare un LLM da Python
Inviare prompt e analizzare le risposte
Chiamare un LLM da Python è una lezione NLP Academy gratuita su CoddyKit. Questa è la lezione 2 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento NLP Academy, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso NLP Academy include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
Talk to a Model in Code
You do not run giant models on your laptop. Instead you send text to a hosted model over an API and get a reply back. 🌐
Install a Client
Most providers ship a Python client library so you can call the model with a few clean lines instead of raw HTTP.
pip install openaiKeep Your Key Secret
Calls are authenticated with an API key. Store it in an environment variable, never hard-coded in your source files.
import os
key = os.environ["OPENAI_API_KEY"]Create the Client
You start by building a client object. It reads your key and handles the network details for every request you make.
from openai import OpenAI
client = OpenAI()Messages, Not Just Text
Chat models take a list of messages, each tagged with a role like system, user, or assistant.
The System Role
A system message sets the model's behavior up front, like telling it to answer briefly or act as a helpful tutor.
Send Your Prompt
You put your question in a user message and send the whole list to the model in a single call.
resp = client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": "Hi!"}])Parse the Response
The reply is a structured object. The text you want sits inside the first choice, ready to read or store.
text = resp.choices[0].message.content
print(text)Control With Temperature
The temperature setting controls randomness. Low values give steady answers; high values give more creative, varied ones.
Cap the Output
Setting max tokens limits how long the reply can be, which keeps responses tidy and your costs predictable.
Handle Failures
Networks fail and limits get hit, so wrap calls in try/except and retry gracefully when an error comes back.
Quick Check
Where do you find the model's text in a chat completion response?
Recap
Install a client, load your key from the environment, send role-tagged messages, then read the text from the first choice. ✅
Domande Frequenti
La lezione «Chiamare un LLM da Python» è gratuita?
Sì — il testo completo di «Chiamare un LLM da Python» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso NLP Academy, passa a CoddyKit PRO. Il corso NLP Academy include 4 lezioni in totale.
Cosa imparerò in «Chiamare un LLM da Python»?
Inviare prompt e analizzare le risposte Eserciti NLP Academy con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.
Ho bisogno di esperienza per iniziare NLP Academy?
Non è richiesta alcuna esperienza precedente. NLP Academy su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 2 di 4.
Quanto tempo richiede la lezione «Chiamare un LLM da Python»?
La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.
Posso scrivere ed eseguire codice in questa lezione NLP Academy?
Sì. Ogni lezione NLP Academy include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.
Tutte le lezioni di questo corso
- Che cosa rende grande un modello
- Chiamare un LLM da Python
- Prompting zero-shot e few-shot
- Output strutturato e guardrail