In the previous lecture, you learned what Langchain is, what needs it fills, and
its components.
Let's dive into coding.
We'll write the code in Jupyter Notebook or Google Colab.
I showed you how to install them in the first course of the series OpenAI API
with Python.
In this video, we'll set up the
environment, we'll install the required libraries, and set up the API keys using
Python .env.
We can install the libraries using pip
install and the name of the libraries like we've done so many times or we could
write the name of the libraries in a text file called requirements .txt and then
run pip install -r requirements .txt.
I am creating a text file called
requirements .txt in the current directory.
I am adding to the file the necessary
libraries OpenAI, Langchain, Pinecone Client, Python .env, and TicToken.
We'll use them all later in this course.
I am pasting a few more libraries that
will be necessary in the projects section.
You will find the requirements .txt file
in the resources.
I am saving it.
Now, in the terminal or in a Jupyter notebook cell, run pip install -r
requirements .txt and the dash q from quiet.
It is installing the libraries specified
in requirements .txt.
It is done.
To check the version of a library, run pip show and the name of the library.
pip show Langchain.
And I am running the cell.
Or pip show OpenAI.
We are using the latest versions of these libraries.
Very well.
Let's talk about Python .env we've just installed.
Python .env is a Python module that allows you to specify environment
variables as key value pairs in a .env file within your Python project directory.
It is a convenient and secure way to load and use environment variables in your application.
You will find Python .env used in many Python applications.
We can have more than one API key and will save them all in .env.
We will have an API key for OpenAI, another one for Pinecone, a key for
Google's Gemini, Hugging Face and so on.
Let's suppose we've already created an
OpenAI account and generated an API key.
If not, go to platform .openai .com and
sign up for a new account.
Then login.
Once logged in, click the OpenAI logo in the left upper corner and the API keys.
Here you can generate a new API key or invalidate an existing one.
I am creating a new key.
This key will have all the permissions
but you can restrict some of them.
I am creating the key and copying it to Clipboard.
Using a text editor, I am creating a new text file in the projects directory.
In the file, I am adding OpenAI API key in capitals, equals and the key in double quotes.
Next, I am saving the file as .env.
I am removing the new text document.
This is the .env file that contains the OpenAI API key.
Here, I would have one remark.
On Linux and MacOS, the file names that
start with a dot are hidden and are not displayed by default in the file explorer.
Go to settings and select to see them as well.
Let's load the environment variables.
I am importing OS and from .env, I am importing load .env and find .env.
I am calling load .env to load the variables found in .env.
I can give the path to .env as an argument or I can call find .env.
Find .env tries to find the .env file.
I am adding override equals true which is
necessary to override the value of the variable if you change it in .env.
Note that find .env retries a path to the file if found or an empty string
otherwise and that's it.
Now, the environment variables are loaded
and I can use them.
To get the value of an environment
variable use os .env .get and the name of the variable.
OpenAI API key.
We've successfully set up the environment.
In this video, we installed the necessary libraries, obtained the API key for
OpenAI and loaded it as an environment variable in our application.
We'll pause here briefly.
In the next video, we'll explore language
components and demonstrate how to invoke various AI models.