0% found this document useful (0 votes)
120 views

GAI4

The document outlines a program that uses word embeddings to enhance prompts for a Generative AI model by retrieving similar words. It includes functions to load pre-trained word vectors, find similar words, enrich prompts, and generate AI responses. The main function compares the outputs of the original and enriched prompts to evaluate the detail and relevance of the responses.

Uploaded by

Vaishnavi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
120 views

GAI4

The document outlines a program that uses word embeddings to enhance prompts for a Generative AI model by retrieving similar words. It includes functions to load pre-trained word vectors, find similar words, enrich prompts, and generate AI responses. The main function compares the outputs of the original and enriched prompts to evaluate the detail and relevance of the responses.

Uploaded by

Vaishnavi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Program No.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.

import gensim
import random
from gensim.models import KeyedVectors

def load_word_embeddings():
# Load pre-trained word vectors from a local file (ensure it's available)

model_path = r"C:\Users\Exam\Downloads\GoogleNews-vectors-negative300.bin"
model = KeyedVectors.load_word2vec_format(model_path, binary=True)
return model

def get_similar_words(model, word, top_n=3):


try:
similar_words = model.most_similar(word, topn=top_n)
return [w[0] for w in similar_words]
except KeyError:
return [word] # Return the original word if not found in vocabulary

def enrich_prompt(prompt, model):


words = prompt.split()
enriched_words = []
for word in words:
similar = get_similar_words(model, word)
enriched_words.append(random.choice(similar)) # Pick a random similar word

return " ".join(enriched_words)

def generate_response(prompt):
# Placeholder function for AI model response (mocked for now)
return f"AI Response: {prompt}... (Generated Text)"

def main():
model = load_word_embeddings()
original_prompt = "Create a beautiful landscape painting description"
enriched_prompt = enrich_prompt(original_prompt, model)
original_response = generate_response(original_prompt)
enriched_response = generate_response(enriched_prompt)

print("Original Prompt:", original_prompt)


print("Generated Response:", original_response)
print("\nEnriched Prompt:", enriched_prompt)
print("Generated Response:", enriched_response)

if __name__ == "__main__":
main()

Output:
Original Prompt: Create a beautiful landscape painting description
Generated Response: AI Response: Create a beautiful landscape painting
description...
(Generated Text)

Enriched Prompt: Creating a gorgeous cityscape watercolor descriptions


Generated Response: AI Response: Creating a gorgeous cityscape watercolor
descriptions... (Generated Text)

You might also like