Securely store LLM API keys inside bash environments, install LangChain orchestration modules, deploy ChromaDB/FAISS vector databases locally, and verify semantic search queries inside a VM.
Environment
Conda Env / LangChain / Chroma
Difficulty
Intermediate (2/5)
Course Module
Generative AI & AI Agents
Deliverables
Local Vector Search Logs & API Completion Output
API key security notice: Never hardcode your API keys (e.g. OpenAI, Anthropic, or Gemini) inside your source code or notebooks. This guide details how to store your credentials securely inside an environment variable (in ~/.bashrc) and access them using Python's os.environ package.
1. System Architecture & Process Workflow
The diagram below displays the RAG similarity search architecture. Raw documents are converted into dense float arrays (embeddings) and registered in ChromaDB. When a semantic query is sent, the query itself is embedded, compared against stored document vectors, and the closest match is retrieved.
2. Part 1: Step-by-Step Action Items & Key Execution Steps
STEP 1
Activate Python Virtual Environment
Point the terminal execution environment to the course conda sandbox environment.
$ conda activate ds_ai_ml
This action points python commands to the course sandbox, enabling isolated module installations.
STEP 2
Install LangChain, OpenAI and Vector Store Libraries
Install PyTorch framework alongside NLTK, spaCy, and the Hugging Face Transformers module library.
This command downloads LangChain integration endpoints, OpenAI bridges, and local vector search database engines.
STEP 3
Obtain OpenAI Platform API Key
Register for a developer profile and generate a secret key to authenticate API requests.
Launch Firefox -> Navigate to https://platform.openai.com -> Sign in -> Click "API Keys" in left menu -> Click "Create new secret key" -> Name: course_key -> Click Create -> Copy secret key
This GUI sequence generates a secret credential token, enabling you to communicate with OpenAI's large language models.
STEP 4
Configure Bash User Profile and Export Key
Open the shell configuration script and define the variable pointer to persist the API key credential.
$ nano ~/.bashrc
This opens the nano editor. Scroll to the bottom of the file, add this line:
export OPENAI_API_KEY="sk-your-openai-api-key-here" Press **Ctrl + O** and **Enter** to save, and **Ctrl + X** to exit.
STEP 5
Reload the Shell Configurations
Re-initialize bash parameters to load variables without closing active terminal panels.
$ source ~/.bashrc && echo $OPENAI_API_KEY
This command reloads `.bashrc` settings and prints the active key token to verify it loaded correctly.
STEP 6
Save Python Vector Search Diagnostic Script
Open a document editor inside the terminal and write your verification script code.
$ nano ~/verify_genai.py
This opens nano editor. Paste the verification script from Part 2 below, press **Ctrl + O** and **Enter** to save, and **Ctrl + X** to exit.
STEP 7
Execute verification script
Run the validation script using the python engine to verify vector database operations work.
$ python ~/verify_genai.py
This runs the script. You should see validation outputs confirming the local vector index was built, search was resolved, and the LLM mock connection works.
3. Operational Pipeline Architecture
The flowchart below outlines the GenAI database setup pipeline. It shows the steps from installing dependencies and configuring environment variables to querying local indices and executing completions.
4. Part 2: Complete Deliverable Assets & Production Templates
To verify the GenAI stack, we will write a Python script that loads documents, embeds them, runs local vector searches, and tests API configurations. Below is a line-by-line explanation of the code, followed by the combined script.
Step-by-Step Code Construction
Lines 1 - 5
Import Framework Modules
Include system packages, LangChain document classes, and the FAISS vector index engine in the script.
import os
import sys
from langchain_core.documents import Document
from langchain_community.vectorstores import FAISS
from langchain_community.embeddings import FakeEmbeddings
These imports check system paths, fetch LangChain abstractions, load the FAISS vector database, and initialize fake embedding engines for local testing.
Lines 6 - 9
Verify API Key Environment Status
Retrieve the environment variable to ensure the API key is configured correctly.
api_key = os.getenv("OPENAI_API_KEY")
if not api_key:
print("WARNING: 'OPENAI_API_KEY' not found in system environment variables!")
print("Verify step 4 shell configurations to set keys correctly.")
This checks key variables in bash memory, printing troubleshooting logs if path variables are missing.
Lines 10 - 15
Initialize Document Collections
Create lists of document strings to load into the vector store database.
document_list = [
Document(page_content="Conda is a python package manager used to install scientific libraries.", metadata={"source": "course_docs"}),
Document(page_content="PostgreSQL is an open-source object-relational SQL database engine.", metadata={"source": "course_docs"}),
Document(page_content="Antigravity is a principal AI coding assistant designed by DeepMind.", metadata={"source": "course_docs"})
]
This loads unstructured text entries, wrapping them into standard LangChain Document blocks with metadata sources.
Lines 16 - 22
Build Local Vector Search Index
Embed the documents and initialize a local FAISS vector search database.
embedding_engine = FakeEmbeddings(size=1536)
print("Building local FAISS vector store index...")
vector_db = FAISS.from_documents(document_list, embedding_engine)
print("Vector database built successfully!")
This compiles local embeddings and registers them in the FAISS index database to query documents semantically.
Lines 23 - 27
Run Semantic Similarity Search
Query the local database to find the document that best matches the query topic.