https://github.com/ranfysvalle02/mdb-gpt
https://github.com/ranfysvalle02/mdb-gpt
Last synced: 6 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/ranfysvalle02/mdb-gpt
- Owner: ranfysvalle02
- Created: 2023-05-22T01:57:05.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2023-05-22T05:32:54.000Z (over 2 years ago)
- Last Synced: 2025-02-16T04:45:04.045Z (8 months ago)
- Language: Python
- Size: 1.61 MB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Atlas Vector Search + GPT Prompt
```
QUESTION:What steps should banks take to modernize their business?ANSWER:
Banks should take an iterative approach to modernization, using an operational data layer (ODL) as a bridge between their existing systems and new ones.
This approach can be broken into five phases: Simple ODL, Data Migration, Application Re-architecture, Application Modernization, and Continuous Improvement.
This approach allows banks to offload traffic away from costly mainframe systems, rearchitect monolithic applications into a suite of microservices, build new business functionality faster, and scale new services to millions of users.
```Using vector-search powered prompt engineering for your favorite LLM
## Story:
encode.py => takes PDF, splits it into chunks, encodes chunks as vectorsquery.py => grabs question, turns it into vectors, uses cosine similarity to compare the vectors, grabs chunks to "enhance" the context, passes question/context to GPT.
Provides both the response from the enhanced context and the response without enhanced context.
As you can imagine:
QUESTION = "What steps should banks take to modernize their business?"
without context will lead to responses that are unlikely to include MongoDB. By using vector search through a PDF on Bank Modernization, we can pull out relevant chunks of content and pass them as context to GPT to have a direct influence on the response
## Inspired by:
https://github.com/wbleonard/atlas-vector-search-pdf## Disclaimer:
knnBeta is in early access and should only be used for evaluation purposes only.
https://www.mongodb.com/docs/atlas/atlas-search/knn-beta/### knnBeta
***This feature is in early access and available only for evaluation purposes, to validate functionality, and to gather feedback from a small closed group of early access users. It is not recommended for production deployments as we may introduce breaking changes. This feature doesn't include formal consulting, SLAs, or technical support obligations.***## Introduction
This demo is a prototype of how Atlas Vector Search could be used to enhance queries sent to GPT models by providing context relevant to the question.To begin, the text from the PDFs are extracted, split into chunks, and mapped into a 1024 dimensional dense vector space. The PDF chunks of text along with their vectors are stored into MongoDB Atlas.
An Atlas Vector Search index then allows the PDFs to be queried, finding the PDFs that are relevant to the query.

### Extract and Encode the PDFs
Install the requirements. This implementation uses:
* [PyPDF2](https://github.com/py-pdf/PyPDF2) Python library for text extraction
* [pymongo](https://pypi.org/project/pymongo/) - the Python driver for MongoDB
* few othersRun the [encode.py](encode.py)
```python
python3 encode.py
```### Create Search Index
Create a default search index on the collection:
```json
{
"mappings": {
"dynamic": true,
"fields": {
"embeddings": {
"dimensions": 1024,
"similarity": "cosine",
"type": "knnVector"
}
}
}
}
```## Cosine Similarity
https://www.youtube.com/watch?v=e9U0QAFbfLI## Demo
You are now ready to search your vast PDF library for the PDFs that may hold the answers to your questions.Your query will be mapped using the same sentence transformer that was used to encode the data and then submitted to Atlas Search, returning the top 3 matches.
For example:
```zsh
✗ python3 query.py
```## The Search Query
This is the simple query passed to MongoDB:```json
[
{
"$search": {
"knnBeta": {
"vector": ,
"path": "embeddings",
"k": 5 // Number of neareast neighbors (nn) to return
}
}
},
{
"$limit": 5
}
]
```The knnBeta operator uses the [Hierarchical Navigable Small Worlds](https://arxiv.org/abs/1603.09320) algorithm to perform semantic search. You can use Atlas Search support for kNN query to search similar to a selected product, search for images, etc.