Exercise#1 Setup key file

Objective

In order to simplify the task of managing API keys for hosted models, you will create a common key file that you will store locally on our machine. Make sure not to store this file in any location that can be accidently put in the cloud or in GitHub repository. In this course you will be using multiple 3rd party hosted models and services. Each of the providers would provide access to their models/service by way of secret API key.

In this exercise you will setup a key file with the given template. API keys will be added to the key file as and when you will create keys for models (starting with next exercise).

To use a model/service in the code provided in the project repository, you would need to:

api-key-setup-steps

The API key you create for the model/service MUST be placed in a secure location. A theft of the key may lead to unauthorized use of the model/service for which you may end up paying. Be aware tha hackers are known to scan public repositories (GitHub) for API keys, so do not hard code your API keys in code !!!

Steps

1. Create a key file under any folder of your choice

Example : C:\Users\raj\.jupyter\.env is where I have created my key file

I have saved my key file with the name .env to a secure folder named .jupyter

2. Copy the content below to the clipboard

# This is a template file for securely managing your Model API keys.
# 
# How to use:
## 1. Create a key file under any folder of your choice e.g., 'C:\\Users\\raj\\.jupyter\\.env' is where I have created my key file
## 2. Copy the content of this file and paste it in your key file
## 3. Add keys to the file as you create accounts with the model providers
##    E.g., HUGGINGFACEHUB_API_TOKEN=ABCDED141414141414
##    E.g., OPENAI_API_KEY=1425AGSFSF%$$DDD


# Hugging Face Token
# https://huggingface.co/docs/hub/en/security-tokens
HUGGINGFACEHUB_API_TOKEN=<<<<Add your Hugging Face Token Here>>>>

# Open AI Key
# https://auth.openai.com/authorize
OPENAI_API_KEY=<<<<Add your open AI API key Here>>>>

# Google Gemini
# https://ai.google.dev/gemini-api/docs/api-key
GOOGLE_API_KEY=<<<<Google API Key>>>>

# Cohere Key
# https://dashboard.cohere.com/welcome/register
COHERE_API_KEY=<<<<Add your Cohere API key Here>>>>

# AI21
# https://studio.ai21.com/login
AI21_API_KEY=<<<<Add your AI21 API key Here>>>>

# Anthropic
# https://console.anthropic.com/login
ANTHROPIC_API_KEY=<<<<Add your Anthropic API key>>>>

# Pinecone
# https://login.pinecone.io/login
PINECONE_API_KEY=<<<<Pinecone API Key>>>>
PINECONE_HOST=<<<<Add the host Here>>>>

# SerpAPI
# https://serpapi.com/users/sign_up
SERPAPI_API_KEY=<<<<Search API Key>>>>

# Tavily
# https://tavily.us.auth0.com/u/signup
TAVILY_API_KEY=<<<<Serach API Key>>>

# This is just for the key file verification
# Test key file setup
TEST_API_KEY=API_KEYS_IN_KEY_FILE_AVAILABLE

3. Paste the content to the key file

Save the file :-)

4. Add API Keys to the file

Everytime you would create a new API key, you would add it to this key file.

Example:

HUGGINGFACEHUB_API_TOKEN=hf_XXXXXXXXXXXXXXXXXXXXXXXXXXXX

How you would use the key file?

At runtime, key file will be read into environment variables. The python code will extract the API key using an appropriate key name.

Python dotenv package can be used loading the environment variables from the key file. The code below shows how to do it.

Note: Package is installed as part of the dev environment setup

pip install python-dotenv

Note: Change the path of the key file in the code

To try out the code below, copy/paste code to a file (test-env.py) and run it. It should print the content of the environment variable TEST_API_KEY. A blank value will mean one of the 2 things:

  1. Key file path is incorrect
  2. The key file was found but it did not have the key TEST_API_KEY

from dotenv import load_dotenv
import os

# Load the file that contains the API keys
# CHANGE THE PATH TO THE KEY FILE
load_dotenv('C:\\Users\\raj\\.jupyter\\.env')

# Get the test API Key from env variable
TEST_API_KEY=os.getenv("TEST_API_KEY")

# Print the retrieved key
print("TEST_API_KEY: ", TEST_API_KEY)

Google colab

The main objective of this exercise was to have you setup the Key file on your local machine. The key file will not be available on Google colab as a result when you will run the code, you will be promted to

Open In Colab