Skip to content

Instantly share code, notes, and snippets.

@smrati
Last active October 31, 2024 07:53
Show Gist options
  • Select an option

  • Save smrati/7bf7221b906f594664e923603f1b77a8 to your computer and use it in GitHub Desktop.

Select an option

Save smrati/7bf7221b906f594664e923603f1b77a8 to your computer and use it in GitHub Desktop.
ollama embeddings + FAISS
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