
- Python 3.11 is already installed and your project’s virtual environment is active.
- Install the official SDK:
It’s safer to keep your API key out of source files. Use an environment variable such as
OPENAI_API_KEY and load it from os.environ instead of pasting the key in code.- Create a file named
hello_llm.py. - Paste the short program below, save the file, and run it with
python3 hello_llm.py.
- Create an SDK client
- Send a chat completion request
- Print the returned message
- client creation:
OpenAI(api_key="...")— creates an authenticated client instance. model: selects which model to use (here"gpt-4o").temperature: controls randomness (0.0 = deterministic, higher = more diverse).messages: the chat-style conversation (system -> assistant behavior, user -> request).response.choices[0].message.content: the assistant’s returned text.
Change the prompt and experiment with temperature
- Modify the
usermessage to:"Write a one-sentence story about a robot."
- Set
temperatureto0.0and run the script twice. With0.0the model is deterministic (the same prompt returns the same output):
- Set
temperatureto1.0and run multiple times to see more diverse outputs:
0.0 = deterministic (repeatable), higher values = more varied creativity.
Security reminder
Do not commit API keys to source control. Use environment variables (for example,
OPENAI_API_KEY) or a secrets manager and never paste keys into public repositories.- OpenAI Python SDK documentation: https://platform.openai.com/docs/api-reference
- Learn more about temperatures and sampling: search for “temperature sampling in language models”