Last active
October 31, 2024 07:53
-
-
Save smrati/7bf7221b906f594664e923603f1b77a8 to your computer and use it in GitHub Desktop.
ollama embeddings + FAISS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from langchain_community.embeddings import OllamaEmbeddings | |
| import faiss | |
| import numpy as np | |
| # Initialize Ollama embeddings model | |
| embedding_model = ( | |
| OllamaEmbeddings(model="llama3.2:3b") | |
| ) | |
| # Sample sentences | |
| sentences = [ | |
| "Crew ai is an agent development system, we can use it to create a multi agent system easily", | |
| "War between Ukraine and Russia is still on", | |
| "Kamla Harris and Donald Trump are in presidential race neck to neck, let see how the presidential results look like in Janurary", | |
| "Is Harry Potter the longest-running movie series yet?", | |
| "This pizza tastes terrible." | |
| ] | |
| # Generate embeddings for each sentence | |
| embeddings = embedding_model.embed_documents(sentences) | |
| # Convert embeddings to a NumPy array | |
| embeddings_array = np.array(embeddings).astype('float32') | |
| # Initialize FAISS index | |
| dimension = embeddings_array.shape[1] # embedding dimension | |
| index = faiss.IndexFlatL2(dimension) | |
| # Add embeddings to the index | |
| index.add(embeddings_array) | |
| # Search for similar sentences | |
| # Example: Find the top 2 most similar sentences | |
| query_sentence = "president election" | |
| query_embedding = embedding_model.embed_query(query_sentence) # use the first sentence as an example query | |
| query_embedding = np.array([query_embedding]).astype('float32') | |
| k = 2 # number of nearest neighbors to retrieve | |
| distances, indices = index.search(query_embedding, k) | |
| # Print the results | |
| print("Query Sentence:", query_sentence) | |
| for i in range(k): | |
| print(f"Neighbor {i+1}: {sentences[indices[0][i]]}, Distance: {distances[0][i]}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment