Working Lunch - Corpus Transform Example
Hello Kirill - lunchtime in the Bay Area. Instead of taking an ergonomic walk I wanted to provide an example of public domain documents in the energy industry that could be seen as a corpus of information, transformed to vectors using tools such as word2vec, paragraph transformer, sentence transformers to make the corpus more available for “industry specific” generative search. (please comment)
In California there has been an upsurge of developers installing large renewable generation systems. The key for the developer is to determine how to interconnect to the existing grid, the costs and prices involved, and determining the buyer (offtaker) of the power generated by their renewable system.
For very large systems one “corpus” of documents they need to read and understand are located here: https://www.caiso.com/Documents/Website_Notes.pdf
My goal is to “Transform” the corpus of PDF/text to vector and make it more user friendly via “generative search” for the user.
I asked Chat GPT: “How to encode PDF document using paragraph Transformer and sentence transformer?”
Bing Chat GPT Answer response: To encode a PDF document using paragraph Transformer and sentence transformer, you can follow the steps below:
Install the required libraries: sentence-transformers, PyPDF2, and pdfminer.six.
Import the necessary modules: ParagraphTransformer, SentenceTransformer, PdfFileReader, and pdfminer.high_level.
Load the PDF document using PdfFileReader.
Extract the text from the PDF document using pdfminer.high_level.extract_text.
Split the text into paragraphs using split function.
Encode the paragraphs using ParagraphTransformer and SentenceTransformer respectively.
Concatenate the encoded paragraphs to get the final encoding.
Here is an example code snippet that demonstrates the above steps:
Python
from sentence_transformers import SentenceTransformer, ParagraphTransformer
from PyPDF2 import PdfFileReader
import pdfminer.high_level
# Load the PDF document
pdf = PdfFileReader(open('document.pdf', 'rb'))
# Extract the text from the PDF document
text = pdfminer.high_level.extract_text('document.pdf')
# Split the text into paragraphs
paragraphs = text.split('\n\n')
# Encode the paragraphs using ParagraphTransformer and SentenceTransformer respectively
paragraph_transformer = ParagraphTransformer('paraphrase-MiniLM-L6-v2')
sentence_transformer = SentenceTransformer('msmarco-distilbert-base-v2')
paragraph_embeddings = paragraph_transformer.encode(paragraphs)
sentence_embeddings = sentence_transformer.encode(paragraphs)
# Concatenate the encoded paragraphs to get the final encoding
final_encoding = np.concatenate((paragraph_embeddings, sentence_embeddings), axis=1)
“Please note that the above code is just an example and you may need to modify it based on your specific requirements. Also, make sure to choose the appropriate models for ParagraphTransformer and SentenceTransformer based on your use case.”
Next question: Can I download word2vec to my google chromebook computer and convert the 5000 page text of the CAISO internnection process and tariffs using Paragraph transformers and sentence transformers to enable readers to do generative search? How do I encode the text to vectors?
Answer from Bing Chat GPT: “Yes, you can download a pre-trained word2vec map from the internet and use it in your Python code. One such pre-trained map is Google’s word2vec model trained with about 100 billion words from various news articles1. You can download this model and use it in your Python code by following the instructions provided in the link1.
To convert the 5000 page text of the CAISO interconnection process and tariffs using Paragraph transformers and sentence transformers, you can follow the instructions provided in the documentation of the transformers library2. The transformers library provides a simple API to convert text into embeddings using pre-trained models such as BERT, GPT-2, and more. You can use the pre-trained models to generate embeddings for the text and then use these embeddings to perform generative search as part of Large Language Models.
To encode the text to vectors, you can use pre-trained models such as BERT, GPT-2, and more. These models are trained on large amounts of text data and can generate embeddings for the text. The embeddings can then be used to represent the text as vectors in a high-dimensional space. Vectors are mathematical objects that have both magnitude and direction. They are used to represent quantities that have both a size and a direction, such as velocity or force 34. Vectors are not just numbers, but they can be represented as numbers in a coordinate system. These numbers can be digitized and converted to bits and bytes for storage and processing.
I hope this helps!”
Conclusion: There is an opportunity to assist people and make things easier! I wish I could do this full time! Please email me your comments and any additional coursework that I should take. Have a nice day. I had a great working lunch :)