This file contains 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 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") |
This file contains 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
def cosine_similarity(a, b): | |
return np.dot(a, b)/(norm(a)*norm(b)) |
This file contains 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
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 |
This file contains 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 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, |
This file contains 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
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 |
This file contains 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
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", |
OlderNewer