datadabllp

Generate and use vector embeddings

Jul 8th, 2024
436
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.86 KB | None | 0 0
  1. from sentence_transformers import SentenceTransformer
  2. from sklearn.metrics.pairwise import cosine_similarity
  3.  
  4. # Load a pre-trained model
  5. model = SentenceTransformer('all-MiniLM-L6-v2')
  6.  
  7. # Generate embeddings for our documents
  8. documents = [
  9.     "The new EU AI regulations focus on transparency and accountability.",
  10.     "Our trading algorithms use advanced machine learning techniques.",
  11.     "Recent market volatility has impacted our APAC operations."
  12. ]
  13. document_embeddings = model.encode(documents)
  14.  
  15. # Generate embedding for a query
  16. query = "Impact of EU AI regulations on trading algorithms"
  17. query_embedding = model.encode([query])[0]
  18.  
  19. # Find the most similar document
  20. similarities = cosine_similarity([query_embedding], document_embeddings)[0]
  21. most_similar_index = similarities.argmax()
  22. print(f"Most relevant document: {documents[most_similar_index]}")
  23.  
Advertisement
Add Comment
Please, Sign In to add comment