Welcome to the AI & Semantic Search Tutorial Module. This guide explains semantic vector search using Sentence Transformers, cosine similarity math, and provides a comprehensive guide on the best modern ways to create and manage Python virtual environments (venv).
Traditional search looks for exact keyword matches (e.g. searching for "nature" will miss sentences containing "mountains" or "waterfalls" unless explicitly written).
Semantic search converts text into dense numerical vectors (embeddings) that capture the contextual meaning of words:
- Sentences are mapped to a high-dimensional vector space (384 dimensions for
all-MiniLM-L6-v2). - Sentences with similar meanings are placed close together in this vector space.
- The search calculates Cosine Similarity between the query vector and document vectors to find the closest conceptual match.
Question: Which is the best way to add a virtual environment:
python -m venv venvor another way?
- The Modern Industry Best Practice (Recommended):
uv(uv venv) - The Universal Built-In Standard:
python -m venv .venv
| Feature | uv venv (Recommended) |
python -m venv (Built-in) |
poetry |
|---|---|---|---|
| Creation Speed | Instant (~10ms) | 1 – 3 seconds | Moderate |
| Package Installation | 10x – 100x faster (Rust-based) | Slow (standard pip) | Slow |
| External Dependency | Requires uv (pip install uv or standalone binary) |
None (Built into Python stdlib) | Requires poetry |
| Lockfile Support | Native uv.lock |
None (manual requirements.txt) |
Native poetry.lock |
| No-Activation Execution | Supported via uv run |
Requires manual activation or explicit path | Supported via poetry run |
| Cross-Platform | Linux, macOS, Windows | Linux, macOS, Windows | Linux, macOS, Windows |
Since your repository already includes uv.lock and pyproject.toml, uv is the ideal choice.
# Inside the /ai folder (or project root):
uv venv(Creates .venv/ in milliseconds).
uv syncuv run python models.py(Or activate manually if you prefer):
source .venv/bin/activate
python models.pyIf you are working on a machine where third-party CLIs like uv cannot be installed, use Python's built-in venv module.
Naming Tip: Name your virtual environment folder
.venv(with a leading dot) orvenv. Naming it.venvkeeps it hidden from directory listings and automatically matches standard.gitignorerules.
python3 -m venv .venv- Linux / macOS:
source .venv/bin/activate - Windows (Command Prompt):
.venv\Scripts\activate.bat
- Windows (PowerShell):
.venv\Scripts\Activate.ps1
python -m pip install --upgrade pip
pip install -r requirements.txt
# OR if using pyproject.toml:
pip install -e .deactivate| File | Description | Core Concept |
|---|---|---|
models.py |
Semantic search engine & SentenceTransformer |
Embeddings, cosine similarity, vector search |
test_models.py |
Automated unit test suite (unittest) |
Mathematical verification of cosine metrics & mock search |
pyproject.toml |
Project metadata & dependency declarations | Modern PEP 621 Python packaging |
uv.lock |
Pinned exact dependency graph | Deterministic, reproducible builds |
From the ai/ folder:
python3 models.pyExample output:
--- Semantic Search Demonstration ---
Query: 'nice nature place'
Best Match: 'Waterfalls in Duhok'
Score: 0.6721
Execute the test suite from the repository root:
python3 -m unittest ai/test_models.pyExpected output:
.......
----------------------------------------------------------------------
Ran 7 tests in 0.000s
OK
Note
Since this module uses pyproject.toml and uv.lock without a traditional requirements.txt, you can install all required packages in one command:
uv add huggingface-hub langchain langchain-openai langgraph python-dotenv sentence-transformersOr install them into your existing .venv using uv pip:
uv pip install huggingface-hub langchain langchain-openai langgraph python-dotenv sentence-transformershuggingface-hub: Client library to download models and interface with Hugging Face Hub.langchain: Framework for building LLM and agentic workflows.langchain-openai: OpenAI provider integration for LangChain.langgraph: Multi-agent orchestration and cyclical graph execution.python-dotenv: Loads environment variables from.envfiles.sentence-transformers: Dense vector embeddings and semantic search models.