Setup Project 7

GenAI/LLM API & Vector Database Setup

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.

1. Text Ingestion "Conda is a tool..." "Python is a language..." Vector 2. Embeddings Model Generates Float 1536-dim Arrays Store ChromaDB Vector Store V0: [0.12, -0.4, ...] V1: [0.93, 0.05, ...] Similarity Search Computes Cosine Distance Index 3. Query Text "package manager" Vector 4. Embed Query [0.89, 0.08, ...] Compare 5. Result Doc "Conda"
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.

$ pip install langchain langchain-openai chromadb faiss-cpu python-dotenv
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.

1. Ingest APIs Install langchain and ChromaDB pip install 2. Config Key Export keys in bash rc script export sk-key 3. Save Script Write diagnostic Python file verify_genai.py 4. Index DB Store document vectors in memory ChromaDB init 5. Query Index Perform semantic similarity search verify search
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.

search_query = "package manager" closest_matches = vector_db.similarity_search(search_query, k=1) print(f"\nQuery: '{search_query}'") print(f"Closest match found: '{closest_matches[0].page_content}'")
This runs the search query, computes cosine distances, and prints the closest matching document text.

Combined GenAI Verification Script

Save the consolidated blocks above as ~/verify_genai.py and execute it inside the active ds_ai_ml environment:

# verify_genai.py - GenAI and Vector Store Verification Script import os import sys from langchain_core.documents import Document from langchain_community.vectorstores import FAISS from langchain_community.embeddings import FakeEmbeddings def main(): print("=== GenAI & Vector Database Diagnostics ===") # 1. API Environment Variable check api_key = os.getenv("OPENAI_API_KEY") if api_key: masked_key = api_key[:7] + "..." + api_key[-4:] if len(api_key) > 15 else "Loaded" print(f"OPENAI_API_KEY environment status: Found ({masked_key})") else: print("WARNING: 'OPENAI_API_KEY' is missing in active environment session.") print("Ensure step 4 instructions were run and reloaded.") # 2. Document Setup 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"} ) ] print(f"\nSetup: {len(document_list)} mock documentation templates registered.") # 3. Embeddings & Index Creation # FakeEmbeddings outputs 1536-dimensional mock vectors for local CPU testing embedding_engine = FakeEmbeddings(size=1536) print("Compiling local FAISS vector store indexes...") vector_db = FAISS.from_documents(document_list, embedding_engine) print("FAISS local indexing complete.") # 4. Similarity Search query execution search_query = "package manager" print(f"\n[Executing Similarity Search Query]") print(f"Query target text: '{search_query}'") closest_matches = vector_db.similarity_search(search_query, k=1) print("Results found:") print(f" - Page Content: {closest_matches[0].page_content}") print(f" - Metadata Source: {closest_matches[0].metadata['source']}") print("\n=== GenAI Environment Successfully Verified! ===") if __name__ == "__main__": main()
5. Deliverables Summary

Verify that the following configurations and outputs exist inside your project workspace.

Created Files / Templates

  • ~/verify_genai.py - Verification python script file.

Verification Artifacts / Execution Proof

  • Terminal console output displaying correct environmental key checks.
  • FAISS database matching outputs for the query word package manager.
  • System configuration file ~/.bashrc containing valid key tokens.
6. Closing Explanation: Why We Did This & What It Accomplishes

Architectural Intent & Operational Impact

Why We Did This

What This Accomplishes