NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

import json
import os
import json
import numpy as np
import redis
from redis.commands.search.field import VectorField, TextField
from redis.commands.search.indexDefinition import IndexDefinition, IndexType
from redis.commands.search.query import Query
from langchain_openai import AzureOpenAIEmbeddings

class dataVectorization():

def __init__(self):
self.config_details = self.load_config()
self.embedding_model = self.config_details['EMBEDDING_MODEL']

def load_config(self):
'''
Function to load the config file(json) from a local path

return:
- config_details (dict): dictionary of configuration details

'''
with open(r"C:UsersBN117KHOneDrive - EYDesktopWorkGenAITokio Marine2 NotebooksRAGconfig.json",'r') as f:
config_details = json.load(f)

os.environ['AZURE_OPENAI_API_KEY'] = config_details["OPENAI_API_KEY"]
os.environ['AZURE_OPENAI_ENDPOINT'] = config_details["azure_endpoint_gpt3"]

return config_details

def getRedisConnection(self,strHost, port=6379, ssl=False):
'''
Function to establish connection with Redis

Parameters:
- strHost (str): redis host
- port (int): redis port

return:
- az_redis (redis.client.Redis): Redis client
each page

'''
try:
az_redis = redis.Redis(host=strHost, port=port, ssl=ssl, health_check_interval=30)
return az_redis
except Exception as e:
print(f'Error getRedisConnection(): {e}')
return None

# Store data in Redis
def generateEmbeddings(self,conn,listPageData, indexFilename):
'''
Function to generate embeddings

Parameters:
- conn : Redis client
- listPageData (list): page level data from document
- indexFilename (str): index name for the document

'''
connectionPipeline = conn.pipeline(transaction=False)

for i in range(len(listPageData)):
embeddings = AzureOpenAIEmbeddings(deployment=self.embedding_model,chunk_size=1,
openai_api_version = self.config_details["OPENAI_API_VERSION"])

vector = embeddings.embed_query(listPageData[i]["pageText"])
vector = np.array(vector).astype(np.float32).tobytes()


# Create a new hash with Answer and embedding
dictPostHash = {
"pageText": listPageData[i]["pageText"],
"embedding": vector,
"pageNnumber":listPageData[i]["pageNumber"]
}
# create hash
conn.hset(name=f"{indexFilename}:{i}", mapping=dictPostHash)

connectionPipeline.execute()

def createIndex(self,conn,indexFilename):
'''
Function to create index for the document in Redis

Parameters:
- conn : Redis client
- indexFilename (str): index name for the document

'''
SCHEMA = [
TextField("pageText"),
VectorField("embedding", "HNSW", {"TYPE": "FLOAT32", "DIM": 1536, "DISTANCE_METRIC": "COSINE"}),
TextField("pageNumber")
]

try:
conn.ft(f"{indexFilename}").info()
print("Index already exists")
except:
conn.ft(f"{indexFilename}").create_index(fields=SCHEMA, definition=IndexDefinition(prefix=[f"{indexFilename}:"], index_type=IndexType.HASH))
     
 
what is notes.io
 

Notes is a web-based application for online taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000+ notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 14 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.