Building Your First AI Program in Python: A Step-by-Step Guide
In this tutorial, you will learn how to create a Python program that communicates with a local AI model (Ollama) to ask a question and receive an intelligent response. To get started, you should first complete the Free AI Development Setup: Install Python, VS Code & Ollama Tools guide if you haven't already. We will break down the code line by line so you understand each component.
Step 1: Import the Necessary Libraries
The first three lines of your program are critical for enabling communication with the AI model.
from openai import OpenAI: This imports the OpenAI library, which acts as a telephone line allowing Python to talk to the AI model (Ollama). Without this, there is no communication.from dotenv import load_dotenv: This imports theload_dotenvfunction, which reads configuration values (like API keys and model names) from a separate.envfile. This is a best practice to keep sensitive settings out of your main code.import os: The OS library gives Python access to the operating system, enabling it to read environment variables and run commands.
Step 2: Load Environment Variables
load_dotenv()
This single line loads all values from your .env file (base URL, API key, model name) into your Python program. For a deeper dive into this step, see How to Configure AI Model Settings for Your First Python Program. After this, your program knows where to connect and what model to use.
Step 3: Create a Client to Connect to the AI Model
client = OpenAI(base_url=os.getenv("BASE_URL"), api_key=os.getenv("API_KEY"))
This creates a client object that establishes a connection to the Ollama AI model. Think of it as dialing a phone number, now Python knows exactly where to send your questions.
Step 4: Send a Question to the AI Model
response = client.chat.completions.create(
model=os.getenv("MODEL_NAME"),
messages=[
{"role": "user", "content": "What is artificial intelligence?"}
]
)
- The function
client.chat.completions.create()sends your question to the AI. - Arguments:
model: Specifies which AI model to use (e.g., "llama3").messages: A list containing a dictionary with:"role": Identifies who is speaking (here, "user")."content": The actual question text.
- The AI processes the question and stores the complete response in the
responsevariable.
Step 5: Display the Answer
print(response.choices[0].message.content)
The AI returns a large response object with extra information. To extract just the answer, navigate to response.choices[0].message.content. This is where the generated text lives.
How to Run Your Program
- Save your file (e.g.,
hello_ai.py). - Open a terminal in VS Code.
- Run the command:
python hello_ai.py.
If everything is configured correctly, you will see the AI's answer, like a definition of artificial intelligence.
Congratulations!
You have just built your first AI-powered application. Your Python program successfully communicated with a local AI model and received an intelligent response. This simple pattern is the foundation of every AI assistant you will build in this course.
Key Takeaways
- The OpenAI library enables Python-AI communication.
- Use dotenv to securely manage configuration.
- The client object connects your program to the model.
- Questions are sent in a structured format with
roleandcontent. - The answer is extracted from
response.choices[0].message.content.
Common Pitfalls to Avoid
- Forgetting to create the
.envfile or misspelling variable names. - Not installing the required libraries (
openai,python-dotenv). - Running the program without Ollama installed or running. Make sure you've followed Install Ollama and Run Qwen3 4B Locally – AI Model Setup Guide to have the model running.
This foundational program will be the basis for all future AI projects. Practice by changing the question and seeing how the model responds. Happy coding!
So now, we will write our first AI program. So open this particular file, which is
hello_ai.py, and type the following code. You can see the code here.
So you have to import the libraries, then you have to load the configuration from the .environment file.
Then you have to create a client. That means you have to create a client which communicates with the AI model.
Then you send a request to the AI model like this, and then you have to display the response. Do you remember those
steps? That we send the question to the AI model, then we get the response. So I think, since I moved quickly, you
were not able to understand this, and it looks difficult. Do not worry.
Let's understand it one step at a time. From OpenAI, import OpenAI. This is the first line written in your program. Just
see here. This is the first line written in your program. So we will try to understand
line by line. So what this line does is, this imports the library that allows Python to
communicate with an AI model. So you have to communicate with the AI model. That means the Ollama. You have
to communicate that. For that communication, for establishment of communication, you require a library. So
this is the name of the library, OpenAI, and that is way of writing in the Python to get a library in a
Python code. So, think of it as installing a telephone on your program. Without it,
Python cannot talk to the AI model. So, if you will not import this library, your Python program will not be able to
talk to the AI model. So, that is the importance of this. Then this line,
from dot environment import load underscore dot environment. Do you remember right now that we have
installed configuration three lines you must be remembering in the dot env file. So, that is a separate file and this is
a Python file. Now, those environment information has to be accessed from that file to this Python program.
For that purpose, you need a library which is called dot env and within this library we have this function which is
available to access the dot env file. So, this allows the Python to read values from the dot env file.
Instead of hard coding setting inside the program, we keep them separate. We already know we have kept them separate
in the dot env file. All professional developers almost always do this.
Next line we have to add is import OS. OS means operating system. So, this is the library through which your Python
program can access the operating system. For example, accessing any folder, running any commands that can be done
through this library. This is basically operating system packages.
Then this line load underscore dot env. So, this is basically the function which
we have used to access the dot env file. So, all three lines will come into now your Python program because of just this
line. Because you are loading the environment. So, the environment is written environment information is
written in dot env file and you are loading that information into your Python program.
This loads all the values from dot env into your program. After this line, once this line is
executed or run, your Python program knows what is base URL, what is API key, what is the model name because these
three information is written in this file. Next is step is that we have to create a
client that communicates with Ollama. So, see your Python program is separate, your Ollama model is separate. However,
it is available on your local computer, but you have to connect to this so that you are able to send the information and
get back the information. So, for that, you have to use open AI, which is
name of the function. In which you will provide two information. One is the base URL and
another is the API key. So, how to access the base URL? os.getenv
and then you will provide the property name. For API key, similarly OS.getenv
and you provide the property name. So, these two information will allow you to connect with the Ollama.
So, this creates a connection to the AI model. Think of like dialing a phone number.
Now, Python knows where to send your questions because it has been able to connect. So, once it has connected, now
the question can be sent on that connection. So, the next line is
very important. It sends your question to the AI model. What we write? client. What is client?
Here, see. This is the client. So, using that client, you used
client.chat.completions.create. This is the function which you'll use. The model reads the question, thinks for
a moment, and generate an answer. That answer is stored in the variable response. So, you send the question
through this function, and this function returns the response in this variable. So, you get the response where
the answer is is stored. Now, this function will also take some
arguments. So, we'll see what arguments. So, this is the argument which is taken by that particular function.
So, you provide two information. One is the who is sending that information. Because
remember, sometimes it is also possible that AI is sending the information
or the information is given by AI which you are sending back to the AI model. So, you have to tell who is sending the
information. So, role is the keyword and you will provide the value as user. Then, what is the question? So, content
is the keyword. These two you cannot change. These two you can change. So, the value is what is artificial
intelligence. It is called key value format. Key and this is called value.
So, this is the key. That means what What is the role? The role is user. Then, what is the content? This is the
content and this makes the entire message. Right? So, this is used for the dictionary and
this is used for the list. If you have already know the Python programming, you will
know what is dictionary and what is the list. So, this tells the model the following
text is coming from the user. This tells that following text is coming from the user.
Now, this entire thing we can see. So, this This I have already told you. This is the function. This function
takes two argument. One is the name of the model and the second is the message or the question.
So, you are sending two information. After you have established the connection, you are sending two
information. One is the name of the model and the second is the question which you have to send to the AI model.
Question is framed in this way where you provide that who is asking the question and the information of the question
itself. After this function is run, the response is installed into the response object.
Now you have to display the response. So the AI model returns a large response object containing various pieces of
information. So it just it does not just returns the answer. It returns many more things which we'll see later on. Only
thing right now we have to understand it where is the answer. The answer is response.choices.message.content.
So within the response object, this is the place where the answer is. So you just print that. And once you
print that, the response is printed on the screen. The your actual answer is stored inside this. I already told you
the response contains many things, but we want only the answer which is stored here.
So this particular line just extracts that particular text and prints it onto the
screen. Now how will you run this program? You save the file.
Your file is saved. Then open the terminal inside VS [clears throat] code and run python hello_ai.py.
If everything is configured correctly, you will see some similar to this. Since you asked what is artificial
intelligence, it will give something like this kind of response to you. That means what is artificial intelligence.
Great. Congratulations. We have just built your first AI-powered applications.
Take a moment to appreciate what happened. Your Python program successfully
communicated with an AI model running [clears throat] on your own computer and received an intelligent response.
That is the foundation of every AI assistant which we will build in this course.
So, I think it is now clear to you. We will meet in the next session.
To communicate with Ollama, you need three libraries: openai (as the client to interact with the AI model), dotenv (to securely load API keys and configuration from a .env file), and os (to access environment variables). Install them via pip before running your code.
Create a .env file in the same directory as your Python script. Include variables like BASE_URL (e.g., 'http://localhost:11434/v1'), API_KEY (e.g., 'ollama'), and MODEL_NAME (e.g., 'llama3'). The load_dotenv() function reads these variables, keeping sensitive configuration separate from your code.
The Client object, created with OpenAI(base_url=..., api_key=...), establishes a connection between your Python program and the Ollama model. It acts like a dial tone for communication, allowing you to send questions and receive responses through the client's methods.
Use client.chat.completions.create() with parameters like model (specifying which AI model to use) and messages (a list containing a dictionary with role as 'user' and content as your question). The AI processes your input and stores the full response in the response variable.
The response contains nested data. To get just the text answer, access response.choices[0].message.content. This drills down to the first choice's message content, giving you the plain text output (e.g., the AI's answer to your question).
Common pitfalls include: not creating a .env file or misspelling variable names, failing to install required libraries (openai and python-dotenv), and running the code without having Ollama installed or the service running locally. Always verify each component is set up correctly.
The program demonstrates a foundational pattern: import libraries, load configuration with load_dotenv(), create a client, send a structured request via client.chat.completions.create(), and extract the answer from response.choices[0].message.content. This reusable pattern scales to build chatbots, content generators, or any AI-driven tool.
Keep this summary
Save it to LunaNotes and it becomes a real note in your library — editable, searchable, and ready to turn into flashcards or a diagram. Free to start.
Save to LunaNotesOr summarise for another video.
32K+ people have turned 65K+ videos into notes
“Very useful tool. I'm using it for the last couple weeks and I'm loving it so far. Makes learning on YouTube a great experience.”
This summary and transcript were automatically generated using AI with the Free YouTube transcript summary tool by LunaNotes.
Related summaries
Install Python, VS Code, and Ollama for AI Agent Development
This video provides a step-by-step guide to setting up your development environment for building AI agents. Learn how to install Python, download and configure Visual Studio Code, and set up Ollama with large language models.
Install Ollama and Run Qwen3 4B Locally – AI Model Setup Guide
Learn how to install the Ollama AI framework on your computer, download the lightweight Qwen3 4B model, and run it locally via terminal. This step-by-step tutorial lets you chat with an open-source LLM right on your own laptop—no cloud or GPU required.
Free AI Development Setup: Install Python, VS Code & Ollama Tools
Learn how to set up a complete local AI development environment using three free tools. This guide covers Python installation, Visual Studio Code setup, and Ollama for running AI models on your own computer — no GPU or cloud costs required.
How to Configure AI Model Settings for Your First Python Program
Learn to set up a configuration file (.env) for your AI program, including base URL, API key, and model name. This tutorial shows how to configure Ollama locally so your Python code can communicate with an AI model without hardcoding settings.
How AI Models Work: Cloud vs. Local (Free Ollama Setup)
Discover how an AI assistant processes questions like 'What is machine learning?' in four simple steps. Learn the difference between cloud-based AI models and free local alternatives like Ollama, perfect for hands-on learning without API costs.
Most viewed summaries
A Comprehensive Guide to Using Stable Diffusion Forge UI
Explore the Stable Diffusion Forge UI, customizable settings, models, and more to enhance your image generation experience.
Kolonyalismo at Imperyalismo: Ang Kasaysayan ng Pagsakop sa Pilipinas
Tuklasin ang kasaysayan ng kolonyalismo at imperyalismo sa Pilipinas sa pamamagitan ni Ferdinand Magellan.
Mastering Inpainting with Stable Diffusion: Fix Mistakes and Enhance Your Images
Learn to fix mistakes and enhance images with Stable Diffusion's inpainting features effectively.
Pamamaraan at Patakarang Kolonyal ng mga Espanyol sa Pilipinas
Tuklasin ang mga pamamaraan at patakaran ng mga Espanyol sa Pilipinas, at ang epekto nito sa mga Pilipino.
How to Install and Configure Forge: A New Stable Diffusion Web UI
Learn to install and configure the new Forge web UI for Stable Diffusion, with tips on models and settings.
Found this summary useful?
Take it with you. One click puts it in your own LunaNotes library.
Save to LunaNotes