Digital Times Nigeria
  • Home
  • Telecoms
    • Broadband
  • Business
    • Banking
    • Finance
  • Editorial
    • Opinion
    • Big Story
  • TechExtra
    • Fintech
    • Innovation
  • Interview
  • Media
    • Social
    • Broadcasting
Facebook X (Twitter) Instagram
Trending
  • Samsung Unveils The Sleek And Durable Galaxy A07 In Nigeria
  • Bauchi, NITDA Partner In Bold Push For Digital Innovation, Job Creation
  • NITDA Boss Seeks Convergence Of AI And Security For Sustainable Development
  • IHS Nigeria, NCMM Partner To Launch Nigeria’s First Digital Museum Of Antiquities
  • Tech Tools Nigerian Startups Can Use To Boost Efficiency As They Scale
  • NCC Hails Presidential Appointments, Vows To Deepen Digital Economy Growth
  • Mbah Credits Oil Subsidy Removal For Enugu’s Infrastructure Drive
  • Zinox Boss Warns Poor Power Supply Is Crippling Nigeria’s Knowledge Economy
Facebook X (Twitter) Instagram
Digital Times NigeriaDigital Times Nigeria
  • Home
  • Telecoms
    • Broadband
  • Business
    • Banking
    • Finance
  • Editorial
    • Opinion
    • Big Story
  • TechExtra
    • Fintech
    • Innovation
  • Interview
  • Media
    • Social
    • Broadcasting
Digital Times Nigeria
Home » Chat With Your Documents Using AI: Cost Effective Large Language Models And Vector Embeddings
Blog

Chat With Your Documents Using AI: Cost Effective Large Language Models And Vector Embeddings

DigitalTimesNGBy DigitalTimesNG5 August 2023No Comments4 Mins Read2K Views
Facebook Twitter Pinterest Telegram LinkedIn Tumblr WhatsApp Email
Language Models
Christian Ofoefule
Share
Facebook Twitter LinkedIn Pinterest Telegram Email WhatsApp

By Christian OFOEFULE

In this new age of ChatGPT, we have seen the capabilities of large language models and how they have taken centre stage, showcasing their remarkable potential with minimal effort.

In this article, we are going to see how to build a simple application that enables us to chat with our documents. The document can be in PDF or text format.

We are going to use some important libraries like Langchain and learn the concepts of vector embeddings and why they are important both for cost savings and reading large files.

The Foundation: Understanding Embeddings and Vector Databases

Unleashing the Power of Embeddings

Vector embeddings are a way of representing textual data in the form of numerical points. With embeddings, we can efficiently capture the semantic meaning of words, phrases, or entire documents. This approach allows us to overcome the limitations of direct text processing and facilitates advanced operations like similarity searches.

The Role of Vector Databases

Vector databases simply store vector embeddings. Examples include Qdrant, Pinecone, and Weaviate as they stand as notable players. These databases provide a structured environment for storing and querying embeddings.

In essence, a cluster in Qdrant can house multiple collections, each functioning as a database. Within these collections, vectors (numerical representations) of data or text are stored as points, enabling seamless similarity searches.

Cost-Efficient AI: Storing and Querying Embeddings

With great power comes a great cost: When using powerful large language models like ChatGPT the cost associated with generating embeddings for every search query can become a significant concern, especially when utilising OpenAI API. The solution lies in the strategic use of vector databases to store and query embeddings efficiently.

READ ALSO  9 Ways AI Is Powering Google Products

Qdrant: Building Clusters and Collections

Qdrant is a powerful vector similarity search engine with a user-friendly API that enables you to effortlessly store, search, and manage vectors along with additional payloads.

In Qdrant, the process begins by creating a cluster, a high-level organizational unit. Within a cluster, multiple collections or databases can coexist. Each collection serves as a repository for vectors, enabling targeted searches within specified data sets. When you embed your text and send it to the Qdrant database, the resulting embeddings become stored as points within the designated collection.

Pinecone and Weaviate: Alternative Vector Database Solutions

Pinecone and Weaviate offer alternative avenues for storing and querying embeddings. With Pinecone’s robust infrastructure or Weaviate’s feature-rich capabilities, developers can explore diverse options based on their specific needs and preferences.

Langchain:

Langchain is a framework for developing applications powered by language models, it provides the libraries and APIs and establishes communication between the models and vector database.

You can install with pip with the following command:

pip install langchain

Language Models
Shoutout to Alejandro AO’s guide for this architectural representation.

Code Sample

We will write a simple code sample on how we can chat with our documents. The steps will be divided into 2 sections.

  1. Storing/Uploading our document as embeddings
  2. Chatting with the document embeddings using ChatGPT

Step 1: Import Libraries

import os
import qdrant_client
from qdrant_client.http import models
from langchain.vectorstores import Qdrant
from langchain.document_loaders import TextLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.document_loaders import UnstructuredPDFLoader
from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.chains import RetrievalQA
from langchain.llms import OpenAI

Step 2: Read the document and split into chunks

loader = UnstructuredPDFLoader(/path-to-pdf-document)
documents = loader.load()
//split file into small chunks
text_splitter = RecursiveCharacterTextSplitter(chunk_size=800, chunk_overlap=80)
chunks = text_splitter.split_documents(documents)

Step 3: Create Embedding and Language Model Instances

embeddings = OpenAIEmbeddings()
llm = OpenAI()

Step 4: Create Qdrant Client

client = qdrant_client.QdrantClient(
	os.environ["QDRANT_HOST"],
	api_key=os.environ["QDRANT_API_KEY"],
)

Step 5: Create Vector Store

vector_store = Qdrant(
	client=client,
	collection_name=os.environ["QDRANT_COLLECTION_NAME"],
	embeddings=embeddings,
)

Step 6: Upload the document chunks as embeddings to Qdrant vector store

vector_store.add_documents(chunks)

Now that we have successfully uploaded our documents as embeddings to a vector store, we can now chat with it. For this, we would be using langchain’s RetrievalQA API

READ ALSO  Behind The Scenes: Scaling Technology Products

Step 7: Create a RetrievalQA chain type

qa = RetrievalQA.from_chain_type(
	llm=llm,
	chain_type="stuff",
	retriever=vector_store.as_retriever()
)

Step 8: Run the Query and Return the Answer:

question = ‘enter your question about the document’
answer = qa.run(question)

As we chat with our documents using AI, the fusion of cutting-edge technologies empowers developers and businesses to navigate the evolving landscape of conversational AI.

By understanding the nuances of embeddings, harnessing the capabilities of vector databases, and preserving chat history in document databases, we embark on a journey toward AI-driven conversations that are not just intelligent but also seamlessly integrated with the wealth of information stored in our documents.

In the dynamic world of AI-powered chat applications, the convergence of embeddings, vector databases, and language models marks a paradigm shift. By utilising these tools, we open up avenues for efficient, cost-effective, and well-documented AI-powered conversations.

***Christian Ofoefule is a Software Engineer

 

#ChatGPT #Christian Ofoefule #Documents #Language Models #Vector Embeddings AI
Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
Previous Article9 Nigerians Among 20 African Female Tech Startups Picked For 2023 ‘The Future Is Female’ Mentorship Programme
Next Article FG, Wema Bank Partner To Establish Digital And SkillNovation Hubs Across Nigeria
DigitalTimesNG
  • X (Twitter)

Related Posts

Tech Tools Nigerian Startups Can Use To Boost Efficiency As They Scale

15 August 2025

Turning Pressure Into Progress: Victor Seaba’s Journey From Real-World Product Struggles To MVP Mastery

5 August 2025

Nigeria’s App Downloads Grew 320%. Here Are 7 Ways Marketers Can Capitalize

1 August 2025

Celebrating Dr. Omoniyi Ibietan’s Multilayered Impact On Strategic Communication In Africa

21 July 2025

From Lagos To The World: NerdzFactory Powers Nigeria’s Leap Into AI Education

9 July 2025

Prioritising Security: The Bedrock Of Stronger Workplace Collaboration In Nigeria

7 July 2025

Comments are closed.

Categories
About
About

Digital Times Nigeria (www.digitaltimesng.com) is an online technology publication of Digital Times Media Services.

Facebook X (Twitter) Instagram
Latest Posts

Samsung Unveils The Sleek And Durable Galaxy A07 In Nigeria

17 August 2025

Bauchi, NITDA Partner In Bold Push For Digital Innovation, Job Creation

15 August 2025

NITDA Boss Seeks Convergence Of AI And Security For Sustainable Development

15 August 2025
Popular Posts

Building Explainable AI (XAI) Dashboards For Non-Technical Stakeholders

2 May 2022

Building Ethical AI Starts With People: How Gabriel Ayodele Is Engineering Trust Through Mentorship

8 January 2024

Gabriel Tosin Ayodele: Leading AI-Powered Innovation In Web3

8 November 2022
© 2025 Digital Times NG. Designed by Max Excellence LLC.
  • Advert Rate
  • Terms of Use
  • Advertisement
  • Private Policy
  • Contact Us

Type above and press Enter to search. Press Esc to cancel.