In the rapidly evolving landscape of generative AI, the pivotal role of vector databases has become increasingly apparent. This article dives into the dynamic synergy between vector databases and generative AI solutions, exploring how these technological bedrocks are shaping the future of artificial intelligence creativity. Join us on a journey through the intricacies of this powerful alliance, unlocking insights into the transformative impact that vector databases bring to the forefront of innovative AI solutions.
This article helps you understand the aspects of the Vector Database below.
This article was published as a part of the Data Science Blogathon.
A vector database is a form of data collection stored in space. Still, here, it is stored in mathematical representations since the format stored in the databases makes it easier for open AI models to memorize the inputs and allows our open AI application to use cognitive search, recommendations, and text generation for various-use cases in the digitally-transformed -industries. Storing data and retrieval is called “Vector Embeddings” or “Embeddings.” Moreover, this is represented in a numerical array format. Searching is much easier than traditional databases used for AI perspectives with massive, indexed capabilities.
We must consider how Vector databases differ from traditional ones. Let’s discuss this here. One quick difference I can give is that in conventional databases. Data is stored precisely as-is; we could add some business logic to tune the data and merge or split the data based on the business requirements or demands. However, the vector database has a massive transformation, and the data becomes a complex vector representation.
Here’s a map for your understanding and clarity perspective with relational databases against vector databases. The picture below is self-explanatory for understanding vector databases with traditional databases. In short, we can execute inserts and deletes into vector databases, not update statements.
Data is automatically arranged spatially by the content similarity in the stored information. So, let’s consider the departmental store for vector database analogy; all the products are arranged on the shelf based on nature, purpose, manufacture, usage, and quantity-base. In a similar behaviour, the data are
automatically-arranged in the vector database by a similar sort, even if the genre was not well-defined while storing or accessing the data.
The vector databases allow a prominent granularity and dimensions on the specific similarities, so the customer searches for the desired product, manufacturer, and quantity and keeps the item in the cart. Vector database stores all data in a perfect storage structure; here, Machine Learning and AI engineers do not need to label or tag the stored content manually.
A vector embedding is a vector representation in terms of the numerical values. In a compressed format, embeddings capture the inherent properties and associations of the original data, making them a staple in Artificial Intelligence and Machine Learning use cases. Designing embeddings to encode pertinent information about the original data into a lower-dimensional space ensures high-retrieval speed, computational efficiency, and efficient storage.
Capturing the essence of data in a more identically structured manner is the process of vector embedding, forming an ‘Embedding Model.’ Ultimately, these models consider all data objects, extract meaningful patterns and relations within the data source, and transform them into vector embeddings. Subsequently, algorithms leverage these vector embeddings to execute various tasks. Numerous highly developed embedding models, available online as either free or pay-as-you-go, facilitate the accomplishment of vector embedding.
These embeddings are compact, contain complex information, inherit relationships among the data stored in a vector database, enable an efficient data-processing analysis to facilitate understanding and decision-making, and dynamically build various innovative data products across any organisation.
Vector embedding techniques are essential in connecting the gap between readable data and complex algorithms. With data types being numerical vectors, we were able to unlock the potential for a large variety of Generative AI applications along with available Open AI models.
This vector embedding helps us to do multiple jobs:
As we know, the index will improve the search data from the table in traditional databases, similar to vector-databases, and provision the indexing features.
Vector databases provide “Flat indices,” which are the direct representation of the vector embedding. The search capability is comprehensive, and this does not use pre-trained clusters. It performs the query vector is performed across each single vector embedding, and K distances are calculated for each pair.
We perform two different searches in vector databases: semantic and similarity searches.
The measuring methods depend on the nature of the data and the application specific. Commonly, three methods are used to measure the similarity and familiarity with Machine Learning.
In simple terms, the distance between the two vectors is the straight-line distance between the two vector points that measure the st.
This helps us understand the alignment between two vectors, indicating whether they point in the same direction, opposite directions, or are perpendicular to each other.
It assesses the similarity of two vectors by using the angle between them, as shown in the figure. In this case, the values and magnitude of the vectors are insignificant and do not affect the results; only the angle is considered in the calculation.
Traditional databases Search for exact SQL statement matches and retrieve the data in tabular format. At the same time, we deal with vector databases searching for the most similar vector to the input query in plain English using Prompt Engineering techniques. The database uses the Approximate Nearest Neighbour(ANN) search algorithm to find similar data. Always provide reasonably accurate results at high performance, accuracy, and response time.
Let’s connect with Pinecone.
You can connect to Pinecone using Google, GitHub, or Microsoft ID.
Create a new user login for your usage.
After successful login, you will land on the Index page; you can create an index for your Vector Database purposes. Click on the Create Index button.
Create your new index by providing the Name and Dimensions.
Index list page,
Index details – Name, Region, and Environment – We need all these details to connect our vector database from the model building code.
Project settings details,
You can upgrade your preferences for multiple indexes and keys for project purposes.
So far, we have discussed creating the vector database index and settings in Pinecone.
Let’s do some coding now.
Importing libraries
from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.llms import OpenAI
from langchain.vectorstores import Pinecone
from langchain.document_loaders import TextLoader
from langchain.chains.question_answering import load_qa_chain
from langchain.chat_models import ChatOpenAI
Providing API key for OpenAI and Vector database
import os
os.environ["OPENAI_API_KEY"] = "xxxxxxxx"
PINECONE_API_KEY = os.environ.get('PINECONE_API_KEY', 'xxxxxxxxxxxxxxxxxxxxxxx')
PINECONE_API_ENV = os.environ.get('PINECONE_API_ENV', 'gcp-starter')
api_keys="xxxxxxxxxxxxxxxxxxxxxx"
llm = OpenAI(OpenAI=api_keys, temperature=0.1)
Initiating the LLM
llm=OpenAI(openai_api_key=os.environ["OPENAI_API_KEY"],temperature=0.6)
Initiating Pinecone
import pinecone
pinecone.init(
api_key=PINECONE_API_KEY,
environment=PINECONE_API_ENV
index_name = "demoindex"
Loading .csv file for building vector database
from langchain.document_loaders.csv_loader import CSVLoader
loader = CSVLoader(file_path="/content/drive/My Drive/Colab_Notebooks/cereal.csv"
,source_column="name")
data = loader.load()
Split the text into Chunks
text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=20)
text_chunks = text_splitter.split_documents(data)
Finding the text in text_chunk
text_chunks
Output
[Document(page_content=’name: 100% Bran\nmfr: N\ntype: C\ncalories: 70\nprotein: 4\nfat: 1\nsodium: 130\nfiber: 10\ncarbo: 5\nsugars: 6\npotass: 280\nvitamins: 25\nshelf: 3\nweight: 1\ncups: 0.33\nrating: 68.402973\nrecommendation: Kids’, metadata={‘source’: ‘100% Bran’, ‘row’: 0}), , …..Building embedding
embeddings = OpenAIEmbeddings()
Create a Pinecone instance for vector database from ‘data’
vectordb = Pinecone.from_documents(text_chunks,embeddings,index_name="demoindex")
Create a retriever for querying the vector database.
retriever = vectordb.as_retriever(score_threshold = 0.7)
Retrieving data from vector database
rdocs = retriever.get_relevant_documents("Cocoa Puffs")
rdocs
Using Prompt and retrieve the data
from langchain.prompts import PromptTemplate
prompt_template = """Given the following context and a question,
generate an answer based on this context only.
,Please state "I don't know." Don't try to make up an answer.
CONTEXT: {context}
QUESTION: {question}"""
PROMPT = PromptTemplate(
template=prompt_template, input_variables=["context", "question"]
)
chain_type_kwargs = {"prompt": PROMPT}
from langchain.chains import RetrievalQA
chain = RetrievalQA.from_chain_type(llm=llm,
chain_type="stuff",
retriever=retriever,
input_key="query",
return_source_documents=True,
chain_type_kwargs=chain_type_kwargs)
Let’s query the data.
chain('Can you please provide cereal recommendation for Kids?')
Output from Query
{'query': 'Can you please provide cereal recommendation for Kids?',
'result': [Document(page_content='name: Crispix\nmfr: K\ntype: C\ncalories: 110\nprotein: 2\nfat: 0\nsodium: 220\nfiber: 1\ncarbo: 21\nsugars: 3\npotass: 30\nvitamins: 25\nshelf: 3\nweight: 1\ncups: 1\nrating: 46.895644\nrecommendation: Kids', metadata={'row': 21.0, 'source': '/content/drive/My Drive/Colab_Notebooks/cereal.csv'}), ..]
Hope you can understand how vector databases work, their components, architecture, and characteristics of Vector Databases in Generative AI solutions . Understand how the vector database is different from traditional database and comparison with conventional database elements. Indeed, the analogy helps you better understand the vector database. Pinecone vector database and indexing steps would help you create a vector database and bring the key for the following code implementation.
A. A vector database stores a collection of data in space. It keeps the data in mathematical representations. since the format stored in the databases makes it easier for open AI models to memorize the previous inputs and allows our open AI application to use cognitive search, recommendations, and precise text generation for various-use-cases in digitally transformed industries.
A. Some of the characteristics are: 1. It leverages the power of these vector embeddings, leading to indexing and searching across a massive dataset. 2. Compactable with structured, unstructured, and semi-structured data. 3. A vector database organises data through high-dimensional vectors containing hundreds-of-dimensions
A. Database ==> Collections
Table==> Vector Space
Row==>Cector
Column==>Dimension
Inserting and Deleting are possible in Vector databases, just like in a traditional database.
Update and Join are not in scope.
– Retrieval of Information for massive data collection quickly.
– Semantic and Similarity Search Operations from the huge size documents.
– Classification and Clustering Application.
– Recommendation and Sentiment Analysis Systems.
A5: Below are the three methods to measure the similarity:
– Euclidean Distance
– Cosine Similarity
– Dot Product
The media shown in this article is not owned by Analytics Vidhya and is used at the Author’s discretion.