4. Use word embeddings to improve prompts for Generative AI model. Retrieve similar words using word embeddings. Use the similar words to enrich a GenAI prompt. Use the AI model to generate responses for the original and enriched prompts. Compare the outputs in terms of detail and relevance.
NOTE: Please add your Cohere API key before running the program (COHERE_API_KEY variable).
PROGRAM:
# Module or library install command (run this in terminal before running the script)
# pip install sentence-transformers langchain-cohere cohere
import os
import re
from sentence_transformers import SentenceTransformer, util
from langchain_cohere import ChatCohere
# =========================
# CONFIG
# =========================
COHERE_API_KEY = "Kus9gGNNg3xkyZUz0wxbLSgTHlaiyuuMHrutNX8G"
os.environ["COHERE_API_KEY"] = COHERE_API_KEY
# =========================
# MODELS
# =========================
embedding_model = SentenceTransformer("all-MiniLM-L6-v2")
llm = ChatCohere(
model="command-a-03-2025",
temperature=0.3
)
# =========================
# DYNAMIC KEYWORD EXTRACTION
# =========================
def extract_keywords(prompt_text):
words = re.findall(r"\b[a-zA-Z]{4,}\b", prompt_text.lower())
return list(dict.fromkeys(words))
# =========================
# DYNAMIC EMBEDDING ENRICHMENT
# =========================
def enrich_prompt(original_prompt):
keywords = extract_keywords(original_prompt)
if not keywords:
return original_prompt, []
# Create embeddings for all keywords
keyword_embeddings = embedding_model.encode(keywords, convert_to_tensor=True)
# Find most relevant keywords among themselves
scores = util.cos_sim(keyword_embeddings, keyword_embeddings)
enriched_words = set()
for i, keyword in enumerate(keywords):
top_indices = scores[i].topk(k=min(3, len(keywords))).indices
for idx in top_indices:
enriched_words.add(keywords[int(idx)])
enriched_words = list(enriched_words)
enriched_prompt = f"""
Original Prompt:
{original_prompt}
Related concepts (auto-generated using embeddings):
{", ".join(enriched_words)}
Now provide a detailed and context-rich answer.
"""
return enriched_prompt, enriched_words
# =========================
# MAIN PROGRAM
# =========================
original_prompt = input("Enter your prompt: ")
enriched_prompt, enriched_words = enrich_prompt(original_prompt)
original_response = llm.invoke(original_prompt)
enriched_response = llm.invoke(enriched_prompt)
# =========================
# OUTPUT
# =========================
print("\n=========== RELATED WORDS (AUTO) ===========\n")
print(enriched_words)
print("\n=========== ORIGINAL RESPONSE ===========\n")
print(original_response.content)
print("\n=========== ENRICHED RESPONSE ===========\n")
print(enriched_response.content)
print("\n=========== RESULT ===========\n")
print("Enriched response is usually more detailed and context-aware.")OUTPUT:
Warning: You are sending unauthenticated requests to the HF Hub. Please set a HF_TOKEN to enable higher rate limits and faster downloads.
Loading weights: 100%|███████████████████████████████████████████████████████████████████████████████████████████████| 103/103 [00:00<00:00, 11003.06it/s]
Enter your prompt: who is the father of python programming language?
=========== RELATED WORDS (AUTO) ===========
['python', 'programming', 'father', 'language']
=========== ORIGINAL RESPONSE ===========
The "father" of the Python programming language is **Guido van Rossum**. He conceived and created Python in the late 1980s, with the first version released in 1991. Guido played a central role in its development and served as the language's **Benevolent Dictator For Life (BDFL)** until he stepped down in 2018. His vision for a simple, readable, and powerful programming language has made Python one of the most popular and widely used languages today.
=========== ENRICHED RESPONSE ===========
The "father" of the Python programming language is **Guido van Rossum**. Born in the Netherlands in 1956, Guido van Rossum is a computer programmer and software engineer who conceived and developed Python in the late 1980s. He began working on Python at the **Centrum Wiskunde & Informatica (CWI)** in the Netherlands as a successor to the ABC language, aiming to create a language that was more readable, versatile, and user-friendly.
Python was officially released in **1991**, and since then, it has grown into one of the most popular and widely used programming languages in the world. Guido van Rossum served as Python's **Benevolent Dictator For Life (BDFL)** until July 2018, when he stepped down from the role. During his tenure, he played a pivotal role in shaping the language's philosophy, design, and community-driven development process.
Python's success can be attributed to its simplicity, readability, and extensive libraries, making it suitable for a wide range of applications, including web development, data science, artificial intelligence, automation, and more. Guido van Rossum's vision and leadership have left an indelible mark on the programming world, and he remains a respected figure in the software development community.
=========== RESULT ===========
Enriched response is usually more detailed and context-aware.