How to make ChatGPT fully informed even about the new information after 2021.

Fredric Cliver
3 min readJul 24, 2023

--

For the TL;DR: https://github.com/fredriccliver/knowledge-based-gpt

As of my last update in September 2021, ...

Frequently, inquiries about current events highlight a shortcoming in ChatGPT. Admittedly, ChatGPT doesn’t possess complete knowledge about every topic. However, suppose OpenAI were to update ChatGPT with the most recent news. Could it then be possible for it to process personalized data such as personal memos, corporate documents, and client experiences?

The answer is probably no.

Large language models like ChatGPT, Bard, or Llama are designed to think and connect between concepts, not to store information. It’s impractical to expect these Large Language Models (LLMs) to house every piece of data one might need to work with.

This is where the role of a knowledge base combined with a Vector Database comes in.

Text separation

The initial step involves breaking down raw documents into several sections.

These chunks could be combinations of words, individual sentences, or even passages.

The larger the chunk size, the better when it provides more context-based information and maintains connections between sentences. But mostly, it depends on the use case.

# model = SentenceTransformer('all-MiniLM-L6-v2')
model = SentenceTransformer('paraphrase-multilingual-MiniLM-L12-v2')
# all models: https://www.sbert.net/docs/pretrained_models.html

def read_and_split(file_name):
with open(file_name, 'r') as file:
text = file.read()
sentences = nltk.tokenize.sent_tokenize(text)
return sentences

file_name = 'information.txt' # replace with your file name
sentences = read_and_split(file_name)
# print(sentences)

# Generate embeddings for each sentence in the list
embeddings = model.encode(sentences)

# Get the dimension of the embeddings
d = embeddings.shape[1]

# Build the index with the correct dimension
index = faiss.IndexFlatL2(d)

# Add embeddings to the Faiss index
index.add(embeddings)

Similarity Search

The system vectorises your query when you initiate a search or pose a question. This process translates your text-based question into a vector, a mathematical representation in the system. Once this happens, the system then retrieves similar vectors from its database. These could represent words, sentences, or even whole passages.

To put it in simple terms, your text-based query is converted into a mathematical model that the system understands. It then looks for other mathematical models in its database that closely align with yours. These ‘similar vectors’ essentially represent information that matches or closely relates to your original query. This is how the system retrieves and presents information relevant to your question or search.

# Get the query vector
query_vector = model.encode([query])

# Search the Faiss index
# Returns distances and indices of k-nearest neighbors
D, I = index.search(query_vector, k=10)

# Querying

response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are a helpful assistant. You are helping a user find information about the fact that you can find in this summary.\n\nSummary: " + summary + "\n\n"},
{"role": "user", "content": query}
],
max_tokens=600
)

print("\n\n##### Answered #####")
print(response.choices[0].message.content)

Diagram

--

--

Fredric Cliver

Majored in Physics, self-taught and worked in the IT industry as a Dev/Design/Planning for 11 years. And I had run my Startup for 3 years. I fancy a ☔️ 🇬🇧