Skip to content

Instantly share code, notes, and snippets.

@Spartee
Spartee / query_vector.py
Created August 11, 2022 19:08
Creating the query vector using sentence transformers
from sentence_transformers import SentenceTransformer
model = SentenceTransformer('sentence-transformers/all-MiniLM-L6-v2')
# create the vector embedding for the query
query_embedding = model.encode("That is a happy person")
@Spartee
Spartee / cosine_similarity.py
Created August 11, 2022 19:09
Cosine similarity formula in Python
def cosine_similarity(a, b):
return np.dot(a, b)/(norm(a)*norm(b))
@Spartee
Spartee / semantic_sim_results.txt
Created August 11, 2022 19:12
results of semantic similarity search
Query: That is a happy person
That is a very happy person -> similarity score = 0.94291496
That is a happy dog -> similarity score = 0.69457746
Today is a sunny day -> similarity score = 0.25687605
@Spartee
Spartee / redis_vector_search_index_creation.py
Created August 11, 2022 19:15
Example of creating a flat index in Redis with redis-py
from redis import Redis
from redis.commands.search.field import VectorField, TagField
# Function to create a flat (brute-force) search index with Redis/RediSearch
# Could also be a HNSW index
def create_flat_index(redis_conn: Redis, number_of_vectors: int, distance_metric: str='COSINE'):
image_field = VectorField("img_vector",
"FLAT", {"TYPE": "FLOAT32",
"DIM": 512,
@Spartee
Spartee / arxiv_search.py
Created August 27, 2023 06:47
Arxiv search for papers with Redis and Langchain==0.0.272 (master)
import os
import openai
from typing import List, Tuple
openai.api_key = os.environ["OPENAI_API_KEY"]
from langchain.document_loaders import ArxivLoader
from langchain.docstore.document import Document
from langchain.embeddings import OpenAIEmbeddings
@Spartee
Spartee / semantic_similarity.py
Created August 11, 2022 19:10
Semantic Similarity in Python with Huggingface sentence transformers
import numpy as np
from numpy.linalg import norm
from sentence_transformers import SentenceTransformer
# Define the model we want to use (it'll download itself)
model = SentenceTransformer('sentence-transformers/all-MiniLM-L6-v2')
sentences = [
"That is a very happy person",