4.9 KiB
ComfyUI-WordEmbeddings
Utilities for exploring and composing word embeddings in ComfyUI using gensim models. Use these nodes to inspect neighborhoods, traverse between concepts, and solve free‑form word “equations”.
Included nodes (core):
- WordEmbeddingsLoader — pick a gensim model by name.
- WordEmbeddingsExplorer — list nearest neighbors to a word.
- WordEmbeddingsInterpolator — sample words along the path between two words.
- WordEmbeddingsEquation — evaluate expressions like
king - man + woman.
Default model: glove-wiki-gigaword-50 (small & fast).
Installation
If you’re on ComfyUI Windows Portable, use its embedded Python.
- Place this repo at:
ComfyUI/custom_nodes/ComfyUI-WordEmbeddings
- Install dependencies:
Windows (PowerShell)
cd .\ComfyUI_windows_portable\python_embeded
.\python.exe -m pip install --upgrade pip
.\python.exe -m pip install -r ..\ComfyUI\custom_nodes\ComfyUI-WordEmbeddings
equirements.txt
macOS / Linux
pip install -r ComfyUI/custom_nodes/ComfyUI-WordEmbeddings/requirements.txt
- Start ComfyUI. Nodes appear under “ComfyUI-WordEmbeddings”.
Model downloads: On first use,
gensimdownloads the selected model to your user cache (e.g.,~/gensim-dataor%USERPROFILE%\gensim-data). To override, setGENSIM_DATA_DIRbefore launching ComfyUI.
Quick start
- Drop WordEmbeddingsLoader → choose
glove-wiki-gigaword-50(fast). - Feed its
model_nameinto:- Explorer (type a word, get top‑k neighbors), or
- Interpolator (set
word_a,word_b, choosestops), or - Equation (enter an expression like
paris - france + italy).
Save any STRING output using your preferred text saver node.
Nodes
1) WordEmbeddingsLoader
Select a gensim model via dropdown.
Inputs
model_name(choice) — list of installed/availablegensimmodels (default:glove-wiki-gigaword-50if present).
Outputs
model_name(STRING) — pass to other nodes.
2) WordEmbeddingsExplorer
Return nearest neighbors for a single query word.
Inputs
model_name(STRING) — from Loader.word(STRING) — e.g.,king.k(INT) — neighbors to return (default 10).
Outputs
words_csv(STRING) — comma‑separated neighbors.debug_csv(STRING) —word:similaritypairs.
Example
word = king,k = 10
→words_csv:prince,queen,monarch,...
→debug_csv:prince:0.78,queen:0.77,...
3) WordEmbeddingsInterpolator
Sample along the path from word_a to word_b and report the nearest word at each interior step.
Inputs
model_name(STRING) — from Loader.word_a,word_b(STRING) — endpoints (e.g.,king,queen).stops(INT) — interior samples (no endpoints; default 12).- (optional)
topn(INT) — candidate pool size for nearest lookup (default 1000). - (optional)
method(choice) —slerp(default) orlerp.
Outputs
words_csv(STRING) — samples only, comma‑separated.debug_txt(STRING) — per‑step diagnostics (costo A/B/mid, etc.).
Notes
slerp(spherical linear interpolation) generally respects direction on the embedding hypersphere better thanlerp.
4) WordEmbeddingsEquation
Solve free‑form vector arithmetic from a text expression.
Expression syntax
+,-,plus,minus- Numeric coefficients:
2*woman,2.5 man - Ignores punctuation like
?,-> - Anything after
=orequalsis ignored
(e.g.,king - man + woman = ?works the same as without= ?)
Inputs
model_name(STRING) — from Loader (defaultglove-wiki-gigaword-50).expression(STRING) — e.g.,king - man + woman.k(INT) — number of results to return (default 10).- (optional)
topn_pool(INT) — pool size fromsimilar_by_vector(default 2000). - (optional)
normalize_terms(BOOL) — L2‑normalize each term vector before summing (defaultTrue; recommended). - (optional)
allow_inputs(BOOL) — include input words in results (defaultFalse).
Outputs
words_csv(STRING) — top‑k candidates.debug_txt(STRING) — parse details, used/missing terms, cosine scores.
Examples
king - man + woman→ oftenqueenparis - france + italy→ oftenromeson - man + woman→ oftendaughter
Tips & troubleshooting
- Speed:
glove-wiki-gigaword-50is the quickest to download and load. Larger models (e.g., GoogleNews 300) can be richer but heavier. - Case: Nodes attempt a lowercase fallback if the exact case is OOV.
- First-run slowness: The first time a model is selected,
gensimwill download it; subsequent runs use the cache. - Top‑N pools: If an interpolated sample returns odd neighbors, try raising
topn/topn_pool.
License
Released under the MIT License. See the included LICENSE file.