PROGRAM:
!pip install sentence-transformers scikit-learn
from sentence_transformers import SentenceTransformer
from [Link] import cosine_similarity
import numpy as np
# Load the pre-trained model
model = SentenceTransformer('all-MiniLM-L6-v2')
# Example AI lab documents (you can replace these with your own data)
documents = [
"Machine learning models can be trained to recognize patterns in data.",
"AI research focuses on building intelligent systems that mimic human behavior.",
"The field of artificial intelligence has seen major advances in recent years.",
"Deep learning involves training neural networks on large datasets."
# Function to embed a list of documents
def embed_documents(documents):
return [Link](documents)
# Embedding the documents
document_embeddings = embed_documents(documents)
# Function to perform semantic search given a query
def semantic_search(query, document_embeddings, top_k=3):
# Embed the query
query_embedding = [Link]([query]
# Compute cosine similarities between the query and all document embeddings
cosine_similarities = cosine_similarity(query_embedding, document_embeddings)
# Get indices of the top_k most similar documents
top_indices = cosine_similarities[0].argsort()[-top_k:][::-1]
# Return the top_k most similar documents
return [(documents[i], cosine_similarities[0][i]) for i in top_indices]
# Example query
query = "What are the recent advancements in AI?"
# Perform semantic search
top_documents = semantic_search(query, document_embeddings)
# Print the results
print("Top similar documents for the query:", query)
for doc, score in top_documents:
print(f"Score: {score:.4f}, Document: {doc}")
OUTPUT: