Query my tweets
35 | 44 |Try some sample questions like...
50 |-
51 |
- What television shows does the author watch? 52 |
- Does the author like dogs? 53 |
- How does the author feel about web frameworks? 54 |
├── .gitignore ├── 1_import.py ├── 2_load_and_index.py ├── 3_query.py ├── README.md ├── docs ├── 1_signup.png ├── 2_connection_string.png ├── 3_vectors_in_db.png ├── 4_search_tab.png ├── 5_json_editor.png ├── 6_index_config.png ├── 7_index_created.png ├── 8_env_vars.png └── 9_ip_addresses.png ├── flask_app ├── __pycache__ │ └── app.cpython-311.pyc ├── app.py └── requirements.txt ├── next_app ├── .gitignore ├── README.md ├── app │ ├── favicon.ico │ ├── globals.css │ ├── layout.js │ ├── page.js │ └── page.module.css ├── jsconfig.json ├── next.config.js ├── package-lock.json ├── package.json └── public │ ├── next.svg │ └── vercel.svg ├── requirements.txt └── tinytweets.json /.gitignore: -------------------------------------------------------------------------------- 1 | .venv 2 | .env 3 | tweets.json 4 | fewertweets.json 5 | -------------------------------------------------------------------------------- /1_import.py: -------------------------------------------------------------------------------- 1 | ## This script imports the tinytweets.json file into your mongo database 2 | ## It will work for any json file containing a single array of objects 3 | ## There's nothing specific to llamaindex going on here 4 | ## You can get your data into mongo any way you like. 5 | 6 | json_file = 'tinytweets.json' 7 | 8 | # Load environment variables from local .env file 9 | from dotenv import load_dotenv 10 | load_dotenv() 11 | 12 | import os 13 | import json 14 | from pymongo.mongo_client import MongoClient 15 | from pymongo.server_api import ServerApi 16 | 17 | # Load the tweets from a local file 18 | with open(json_file, 'r') as f: 19 | tweets = json.load(f) 20 | 21 | # Create a new client and connect to the server 22 | client = MongoClient(os.getenv('MONGODB_URI'), server_api=ServerApi('1')) 23 | db = client[os.getenv("MONGODB_DATABASE")] 24 | collection = db[os.getenv("MONGODB_COLLECTION")] 25 | 26 | # Insert the tweets into mongo 27 | collection.insert_many(tweets) 28 | -------------------------------------------------------------------------------- /2_load_and_index.py: -------------------------------------------------------------------------------- 1 | ## This script loads data from a mongo database into an index 2 | ## This will convert all the documents in the database into vectors 3 | ## which requires a call to OpenAI for each one, so it can take some time. 4 | ## Once the data is indexed, it will be stored as a new collection in mongodb 5 | ## and you can query it without having to re-index every time. 6 | from dotenv import load_dotenv 7 | load_dotenv() 8 | 9 | # This will turn on really noisy logging if you want it, but it will slow things down 10 | # import logging 11 | # import sys 12 | # logging.basicConfig(stream=sys.stdout, level=logging.DEBUG) 13 | # logging.getLogger().addHandler(logging.StreamHandler(stream=sys.stdout)) 14 | 15 | import os 16 | from llama_index.readers.mongo import SimpleMongoReader 17 | from pymongo.mongo_client import MongoClient 18 | from pymongo.server_api import ServerApi 19 | from llama_index.vector_stores.mongodb import MongoDBAtlasVectorSearch 20 | from llama_index.indices.vector_store.base import VectorStoreIndex 21 | from llama_index.storage.storage_context import StorageContext 22 | 23 | # load objects from mongo and convert them into LlamaIndex Document objects 24 | # llamaindex has a special class that does this for you 25 | # it pulls every object in a given collection 26 | query_dict = {} 27 | reader = SimpleMongoReader(uri=os.getenv("MONGODB_URI")) 28 | documents = reader.load_data( 29 | os.getenv("MONGODB_DATABASE"), 30 | os.getenv("MONGODB_COLLECTION"), # this is the collection where the objects you loaded in 1_import got stored 31 | field_names=["full_text"], # these is a list of the top-level fields in your objects that will be indexed 32 | # make sure your objects have a field called "full_text" or that you change this value 33 | query_dict=query_dict # this is a mongo query dict that will filter your data if you don't want to index everything 34 | ) 35 | 36 | # Create a new client and connect to the server 37 | client = MongoClient(os.getenv("MONGODB_URI"), server_api=ServerApi('1')) 38 | 39 | # create Atlas as a vector store 40 | store = MongoDBAtlasVectorSearch( 41 | client, 42 | db_name=os.getenv('MONGODB_DATABASE'), 43 | collection_name=os.getenv('MONGODB_VECTORS'), # this is where your embeddings will be stored 44 | index_name=os.getenv('MONGODB_VECTOR_INDEX') # this is the name of the index you will need to create 45 | ) 46 | 47 | # now create an index from all the Documents and store them in Atlas 48 | storage_context = StorageContext.from_defaults(vector_store=store) 49 | index = VectorStoreIndex.from_documents( 50 | documents, storage_context=storage_context, 51 | show_progress=True, # this will show you a progress bar as the embeddings are created 52 | ) 53 | 54 | # you can't query your index yet because you need to create a vector search index in mongodb's UI now 55 | -------------------------------------------------------------------------------- /3_query.py: -------------------------------------------------------------------------------- 1 | ## This shows how to load your pre-indexed data from mongo and query it 2 | ## Note that you MUST manually create a vector search index before this will work 3 | ## and you must pass in the name of that index when connecting to Mongodb below 4 | from dotenv import load_dotenv 5 | load_dotenv() 6 | 7 | # Turns on really noisy logging 8 | import logging 9 | import sys 10 | logging.basicConfig(stream=sys.stdout, level=logging.DEBUG) 11 | logging.getLogger().addHandler(logging.StreamHandler(stream=sys.stdout)) 12 | 13 | import os 14 | from pymongo.mongo_client import MongoClient 15 | from pymongo.server_api import ServerApi 16 | from llama_index.vector_stores.mongodb import MongoDBAtlasVectorSearch 17 | from llama_index.indices.vector_store.base import VectorStoreIndex 18 | 19 | # Create a new client and connect to the server 20 | client = MongoClient(os.getenv("MONGODB_URI"), server_api=ServerApi('1')) 21 | 22 | # connect to Atlas as a vector store 23 | store = MongoDBAtlasVectorSearch( 24 | client, 25 | db_name=os.getenv('MONGODB_DATABASE'), # this is the database where you stored your embeddings 26 | collection_name=os.getenv('MONGODB_VECTORS'), # this is where your embeddings were stored in 2_load_and_index.py 27 | index_name=os.getenv('MONGODB_VECTOR_INDEX') # this is the name of the index you created after loading your data 28 | ) 29 | index = VectorStoreIndex.from_vector_store(store) 30 | 31 | # query your data! 32 | # here we have customized the number of documents returned per query to 20, because tweets are really short 33 | query_engine = index.as_query_engine(similarity_top_k=20) 34 | response = query_engine.query("What does the author think of web frameworks?") 35 | print(response) 36 | 37 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # LlamaIndex retrieval augmented generation 2 | ## with MongoDB, Flask and Next.js 3 | 4 | See this demo [in action](https://mongodb-demo-frontend.onrender.com/)! 5 | 6 | ## What is this? 7 | 8 | LlamaIndex is an open-source framework that lets you build AI applications powered by large language models (LLMs) like OpenAI's GPT-4. This application is a demonstration of how to do that, starting from scratch to a fully deployed web application. We'll show you how to run everything in this repo and then start customizing it for your own needs. 9 | 10 | In this example, we'll be using MongoDB as a data store and a vector store. Our back end will be a Python API powered by Flask, and our front end will be a JavaScript web app written in Next.js. 11 | 12 | The app will load a series of tweets (loaded from a Twitter archive export) and then index them to answer questions about the author and their opinions. 13 | 14 | ## What we'll be doing 15 | 16 | The basic steps of this demo are: 17 | * Get data from a JSON file into a Mongo database (this step is optional if you already have data in Mongo) 18 | * Index the data using LlamaIndex. This will use OpenAI's gpt-3.5-turbo under the hood and convert your text into [vector embeddings](https://docs.llamaindex.ai/en/stable/understanding/indexing/indexing.html#what-is-an-embedding), so you'll need an OpenAI API key, and it can take some time depending how much data you have 19 | * Store the embedded data back into MongoDB. LlamaIndex will do this for you automatically. 20 | * Create a Vector Search Index in MongoDB. This is a manual step you'll need to perform in the MongoDB UI. 21 | * Query the data! This will demonstrate that the data is now queryable. Then you'll want to build an app on top of it. 22 | * Set up a Flask API in Python to answer questions about the data, hosted on [Render](https://render.com). 23 | * Set up a Next.js front-end in JavaScript, also hosted on Render. This will accept user questions, pass them to the API, and display the results. 24 | 25 | ## Instructions 26 | 27 | You can either use these instructions as a tutorial and rebuild the application from scratch, or clone the repo and use it as a template. 28 | 29 | ### Before you begin 30 | 31 | We'll assume you have the current version of Python installed (3.11.6 or better), as well as Node.js for the front-end (version 20 or better) and git for source control. 32 | 33 | ### Get the code 34 | 35 | First clone this repo 36 | 37 | ``` 38 | git clone git@github.com:run-llama/mongodb-demo.git 39 | ``` 40 | 41 | ### Sign up for MongoDB Atlas 42 | 43 | We'll be using MongoDB's hosted database service, [MongoDB Atlas](https://www.mongodb.com/cloud/atlas/register). You can sign up for free and get a small hosted cluster for free: 44 | 45 |  46 | 47 | The signup process will walk you through the process of creating your cluster and ensuring it's configured for you to access. Once the cluster is created, choose "Connect" and then "Connect to your application". Choose Python, and you'll be presented with a connection string that looks like this: 48 | 49 |  50 | 51 | ### Set up environment variables 52 | 53 | Copy the connection string (make sure you include your password) and put it into a file called `.env` in the root of this repo. It should look like this: 54 | 55 | ``` 56 | MONGODB_URI=mongodb+srv://seldo:xxxxxxxxxxx@llamaindexdemocluster.xfrdhpz.mongodb.net/?retryWrites=true&w=majority 57 | ``` 58 | 59 | You will also need to choose a name for your database, and the collection where we will store the tweets, and also include them in .env. They can be any string, but this is what we used: 60 | 61 | ``` 62 | MONGODB_DATABASE=tiny_tweets_db 63 | MONGODB_COLLECTION=tiny_tweets_collection 64 | ``` 65 | 66 | ### Set up a python virtual environment and install dependencies 67 | 68 | To avoid colliding with other Python dependencies, it's a good idea to create a python virtual environment to work in. There are lots of ways to do this, but the way we did it is to run this in the root of the repo: 69 | 70 | ```bash 71 | python3 -m venv .venv 72 | source .venv/bin/activate 73 | ``` 74 | 75 | Now we'll install all the dependencies we need in one go with pip: 76 | 77 | ```bash 78 | pip install -r requirements.txt 79 | ``` 80 | 81 | This installs the MongoDB drivers, LlamaIndex itself, and some utility libraries. 82 | 83 | ### Import tweets into MongoDB 84 | 85 | You are now ready to import our ready-made data set into Mongo. This is the file `tinytweets.json`, a selection of approximately 1000 tweets from @seldo on Twitter in mid-2019. With your environment set up you can do this by running 86 | 87 | ``` 88 | python 1_import.py 89 | ``` 90 | 91 | If you're curious, the code is below. If you don't want to use tweets, you can replace `json_file` with any other array of JSON objects, but you will need to modify some code later to make sure the correct field gets indexed. There is no LlamaIndex-specific code here; you can load your data into Mongo any way you want to. 92 | 93 | ```python 94 | json_file = 'tinytweets.json' 95 | 96 | # Load environment variables from local .env file 97 | from dotenv import load_dotenv 98 | load_dotenv() 99 | 100 | import os 101 | import json 102 | from pymongo.mongo_client import MongoClient 103 | from pymongo.server_api import ServerApi 104 | 105 | # Load the tweets from a local file 106 | with open(json_file, 'r') as f: 107 | tweets = json.load(f) 108 | 109 | # Create a new client and connect to the server 110 | client = MongoClient(os.getenv('MONGODB_URI'), server_api=ServerApi('1')) 111 | db = client[os.getenv("MONGODB_DATABASE")] 112 | collection = db[os.getenv("MONGODB_COLLECTION")] 113 | 114 | # Insert the tweets into mongo 115 | collection.insert_many(tweets) 116 | ``` 117 | 118 | ### Load and index your data 119 | 120 | Now we're ready to index our data. To do this, LlamaIndex will pull your text out of Mongo, split it into chunks, and then send those chunks to OpenAI to be turned into [vector embeddings](https://docs.llamaindex.ai/en/stable/understanding/indexing/indexing.html#what-is-an-embedding). The embeddings will then be stored in a new collection in Mongo. This will take a while depending how much text you have, but the good news is that once it's done you will be able to query quickly without needing to re-index. 121 | 122 | We'll be using OpenAI to do the embedding, so now is when you need to [generate an OpenAI API key](https://platform.openai.com/account/api-keys) if you haven't already and add it to your `.env` file like this: 123 | 124 | ``` 125 | OPENAI_API_KEY=sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 126 | ``` 127 | 128 | You'll also need to pick a name for the new collection where the embeddings will be stored, and add it to `.env`, along with the name of a vector search index (we'll be creating this in the next step, after you've indexed your data): 129 | 130 | ``` 131 | MONGODB_VECTORS=tiny_tweets_vectors 132 | MONGODB_VECTOR_INDEX=tiny_tweets_vector_index 133 | ``` 134 | 135 | If the data you're indexing is the tweets we gave you, you're ready to go: 136 | 137 | ```bash 138 | python 2_load_and_index.py 139 | ``` 140 | 141 | You can view the full source code of this script, but let's highlight a few important parts: 142 | 143 | ```python 144 | query_dict = {} 145 | reader = SimpleMongoReader(uri=os.getenv("MONGODB_URI")) 146 | documents = reader.load_data( 147 | os.getenv("MONGODB_DATABASE"), 148 | os.getenv("MONGODB_COLLECTION"), 149 | field_names=["full_text"], 150 | query_dict=query_dict 151 | ) 152 | ``` 153 | 154 | What you're doing here is creating a Reader which loads the data out of Mongo in the collection and database specified. It looks for text in a set of specific keys in each object. In this case we've given it just one key, "full_text". The final parameter is a mongo [query document](https://www.mongodb.com/docs/manual/tutorial/query-documents/), a JSON object you can use to filter your objects down to a subset. We're leaving it empty because we want all the tweets in the collection. 155 | 156 | ```python 157 | # Create a new client and connect to the server 158 | client = MongoClient(os.getenv("MONGODB_URI"), server_api=ServerApi('1')) 159 | 160 | # create Atlas as a vector store 161 | store = MongoDBAtlasVectorSearch( 162 | client, 163 | db_name=os.getenv('MONGODB_DATABASE'), 164 | collection_name=os.getenv('MONGODB_VECTORS'), 165 | index_name=os.getenv('MONGODB_VECTOR_INDEX') 166 | ) 167 | ``` 168 | 169 | Now you're creating a vector search client for Mongo. In addition to a MongoDB client object, you again tell it what database everything is in. This time you give it the name of the collection where you'll store the vector embeddings, and the name of the vector search index you'll create in the next step. 170 | 171 | This process can take a while, so when we kick it off we set the `show_progress` parameter to `True`, which prints a convenient little progress bar: 172 | 173 | ```python 174 | storage_context = StorageContext.from_defaults(vector_store=store) 175 | index = VectorStoreIndex.from_documents( 176 | documents, storage_context=storage_context, 177 | show_progress=True 178 | ) 179 | ``` 180 | 181 | ### Create a vector search index 182 | 183 | Now if all has gone well you should be able to log in to the Mongo Atlas UI and see two collections in your database: the original data in `tiny_tweets_collection`, and the vector embeddings in `tiny_tweets_vectors`. 184 | 185 |  186 | 187 | Now it's time to create the vector search index so that you can query the data. First, click the Search tab, and then click "Create Search Index": 188 | 189 |  190 | 191 | It's not yet possible to create a vector search index using the Visual Editor, so select JSON editor: 192 | 193 |  194 | 195 | Now under "database and collection" select `tiny_tweets_db` and within that select `tiny_tweets_vectors`. Then under "Index name" enter `tiny_tweets_vector_index` (or whatever value you put for MONGODB_VECTOR_INDEX in `.env`). Under that, you'll want to enter this JSON object: 196 | 197 | ```json 198 | { 199 | "fields": [ 200 | { 201 | "numDimensions": 1536, 202 | "path": "embedding", 203 | "similarity": "cosine", 204 | "type": "vector" 205 | } 206 | ] 207 | } 208 | ``` 209 | 210 | This tells Mongo that the `embedding` field in each document (in the `tiny_tweets_vectors` collection) is a vector of 1536 dimensions (this is the size of embeddings used by OpenAI), and that we want to use cosine similarity to compare vectors. You don't need to worry too much about these values unless you want to use a different LLM to OpenAI entirely. 211 | 212 | The UI will ask you to review and confirm your choices, then you need to wait a minute or two while it generates the index. If all goes well, you should see something like this screen: 213 | 214 |  215 | 216 | Now you're ready to query your data! 217 | 218 | ### Run a test query 219 | 220 | You can do this by running 221 | 222 | ```bash 223 | python 3_query.py 224 | ``` 225 | 226 | This sets up a connection to Atlas just like `2_load_and_index.py` did, then it creates a [query engine](https://docs.llamaindex.ai/en/stable/understanding/querying/querying.html#getting-started) and runs a query against it: 227 | 228 | ```python 229 | query_engine = index.as_query_engine(similarity_top_k=20) 230 | response = query_engine.query("What does the author think of web frameworks?") 231 | print(response) 232 | ``` 233 | 234 | If all is well, you should get a nuanced opinion about web frameworks. 235 | 236 | ### Set up a new repo 237 | 238 | Now we have a way to quickly query our data using an LLM. But we want an app! To do that, we're going to set up a Python Flash API as a backend and a JavaScript Next.js app as a front-end. We're going to deploy both of them to [Render](https://render.com), and to do that we need them to be in a GitHub repo. So let's do that: 239 | 240 | 1. Create a new public GitHub repository 241 | 2. Clone it to your local machine 242 | 3. Copy all the files from this repo to the new repo (make sure you don't include the `.git` folder) 243 | 4. Commit and push the files to GitHub 244 | 245 | For the rest of this tutorial we're going to assume you're working in the folder you just created, attached to a repo you control. 246 | 247 | ### Run the Flask API 248 | 249 | The details of creating a Flask app are out of scope for this tutorial, but you can find one already set up for you in `flask_app` in the repo. It sets up a Mongo Atlas client just like we did in `3_query.py`, and it has one real method, `process_form`, which accepts a `query` parameter: 250 | 251 | ```python 252 | @app.route('/process_form', methods=['POST']) 253 | @cross_origin() 254 | def process_form(): 255 | query = request.form.get('query') 256 | if query is not None: 257 | # here we have customized the number of documents returned per query to 20, because tweets are really short 258 | query_engine = index.as_query_engine(similarity_top_k=20) 259 | response = query_engine.query(query) 260 | return jsonify({"response": str(response)}) 261 | else: 262 | return jsonify({"error": "query field is missing"}), 400 263 | ``` 264 | 265 | (The `@cross_origin()` decorator is necessary to allow the front-end to make requests to the API.) 266 | 267 | You can run it locally by running 268 | 269 | ```bash 270 | flask run 271 | ``` 272 | 273 | And you can check it's running by going to [http://127.0.0.1:5000](http://127.0.0.1:5000) in your browser. You should get a "Hello, world!" response. 274 | 275 | ### Deploy the Flask API to Render 276 | 277 | Set up a Render account (we recommend logging in with your GitHub account, to simplify things) and create a new web service: 278 | 279 | 1. Select "build and deploy from a github repository" then select the repo you created above. 280 | 2. Give the service a unique name 281 | 3. Set the root directory to `flask_app` 282 | 4. Set the runtime to Python 3 283 | 5. Select the Free tier 284 | 285 | **Important: set `PYTHON_VERSION`**. Your first deploy will fail because some packages will not be found, to fix this, set your python version to the same one you're using locally: 286 | 287 | 1. Go to "Environment" 288 | 2. Select "Add environment variable" 289 | 3. Set the `key` to `PYTHON_VERSION` and the value to `3.11.6` (or whatever version you're using locally) 290 | 4. Click "save changes" 291 | 5. Go to the "Deploy" button in the top right and select "deploy latest commit". It should now deploy successfully. 292 | 293 | ### Add your `.env` environment variables to Render 294 | 295 | In the same way that you set `PYTHON_VERSION` you should now set all the other environment variables from your `.env` file in your Render environment. Your code needs to know where to connect to Mongo, etc.. So it should eventually look like this: 296 | 297 |  298 | 299 | ### Add your app IPs to MongoDB Atlas 300 | 301 | To allow your API to connect to MongoDB, you need to add its IP addresses to the list of IPs allowed to connect by Mongo. You can find the IPs in the "Connect" button in the top right of Render. 302 | 303 |  304 | 305 | Go to MongoDB's UI and select "Network Access" from under "Security" in the menu on the left. Click "Add IP address" three times and add one of the IPs supplied by Render each time. 306 | 307 | With all this done, your API should now be up and running and able to connect to MongoDB. Time to build a frontend! 308 | 309 | ### Run the Next.js app 310 | 311 | Just like the Flask app, we've already done the heavy lifting for you and you can find the app in `next_app`. To get it going locally, run these in the root of the Next app: 312 | 313 | ```bash 314 | npm install 315 | npm run dev 316 | ``` 317 | 318 | This will give you a local server on [http://127.0.0.1:3000](http://127.0.0.1:3000). If you're already running the Flask API, you should be able to run queries! 319 | 320 | ### Deploy the Next.js app to Render 321 | 322 | Just like the Flask app, we're going to deploy the Next.js app to Render on the free plan. The steps are very similar: 323 | 324 | 1. Select "build and deploy from a github repository" then select the repo you created above. 325 | 2. Give the service a unique name 326 | 3. Set the root directory to `next_app` 327 | 4. Set the runtime to Node.js 328 | 5. Select the Free tier 329 | 330 | ### Set environment variables for Next.js 331 | 332 | Just as with Python, you're going to need to set an environment variable called `NODE_VERSION` to `20` and rebuild your first deploy. 333 | 334 | You're also going to need to tell it where to find the Flask API. To do this, create an environment variable called `NEXT_PUBLIC_API_HOST` and set it to the hostname on Render of your Flask API (in our case, that was `https://mongodb-demo-zpxu.onrender.com/`). 335 | 336 | You don't need to set any of the other environment variables, only your Flask API needs to know how to connect to Mongo. 337 | 338 | Redeploy your Next.js application. 339 | 340 | ### Celebrate! 341 | 342 | If all is well, you should now have a demo app just like ours! You can begin customizing it to your use-case. 343 | -------------------------------------------------------------------------------- /docs/1_signup.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/run-llama/mongodb-demo/59f4d4aee16117a2e00e64abd0ebaed8763e2f0a/docs/1_signup.png -------------------------------------------------------------------------------- /docs/2_connection_string.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/run-llama/mongodb-demo/59f4d4aee16117a2e00e64abd0ebaed8763e2f0a/docs/2_connection_string.png -------------------------------------------------------------------------------- /docs/3_vectors_in_db.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/run-llama/mongodb-demo/59f4d4aee16117a2e00e64abd0ebaed8763e2f0a/docs/3_vectors_in_db.png -------------------------------------------------------------------------------- /docs/4_search_tab.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/run-llama/mongodb-demo/59f4d4aee16117a2e00e64abd0ebaed8763e2f0a/docs/4_search_tab.png -------------------------------------------------------------------------------- /docs/5_json_editor.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/run-llama/mongodb-demo/59f4d4aee16117a2e00e64abd0ebaed8763e2f0a/docs/5_json_editor.png -------------------------------------------------------------------------------- /docs/6_index_config.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/run-llama/mongodb-demo/59f4d4aee16117a2e00e64abd0ebaed8763e2f0a/docs/6_index_config.png -------------------------------------------------------------------------------- /docs/7_index_created.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/run-llama/mongodb-demo/59f4d4aee16117a2e00e64abd0ebaed8763e2f0a/docs/7_index_created.png -------------------------------------------------------------------------------- /docs/8_env_vars.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/run-llama/mongodb-demo/59f4d4aee16117a2e00e64abd0ebaed8763e2f0a/docs/8_env_vars.png -------------------------------------------------------------------------------- /docs/9_ip_addresses.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/run-llama/mongodb-demo/59f4d4aee16117a2e00e64abd0ebaed8763e2f0a/docs/9_ip_addresses.png -------------------------------------------------------------------------------- /flask_app/__pycache__/app.cpython-311.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/run-llama/mongodb-demo/59f4d4aee16117a2e00e64abd0ebaed8763e2f0a/flask_app/__pycache__/app.cpython-311.pyc -------------------------------------------------------------------------------- /flask_app/app.py: -------------------------------------------------------------------------------- 1 | from dotenv import load_dotenv 2 | load_dotenv() 3 | 4 | from flask import Flask, request, jsonify 5 | from flask_cors import CORS, cross_origin 6 | import os 7 | from pymongo.mongo_client import MongoClient 8 | from pymongo.server_api import ServerApi 9 | from llama_index.vector_stores.mongodb import MongoDBAtlasVectorSearch 10 | from llama_index.indices.vector_store.base import VectorStoreIndex 11 | 12 | # Create a new client and connect to the server 13 | client = MongoClient(os.getenv("MONGODB_URI"), server_api=ServerApi('1')) 14 | 15 | # connect to Atlas as a vector store 16 | store = MongoDBAtlasVectorSearch( 17 | client, 18 | db_name=os.getenv('MONGODB_DATABASE'), # this is the database where you stored your embeddings 19 | collection_name=os.getenv('MONGODB_VECTORS'), # this is where your embeddings were stored in 2_load_and_index.py 20 | index_name=os.getenv('MONGODB_VECTOR_INDEX') # this is the name of the index you created after loading your data 21 | ) 22 | index = VectorStoreIndex.from_vector_store(store) 23 | 24 | app = Flask(__name__) 25 | cors = CORS(app) 26 | app.config['CORS_HEADERS'] = 'Content-Type' 27 | 28 | # This is just so you can easily tell the app is running 29 | @app.route('/') 30 | def hello_world(): 31 | return 'Hello, World!' 32 | 33 | @app.route('/process_form', methods=['POST']) 34 | @cross_origin() 35 | def process_form(): 36 | query = request.form.get('query') 37 | if query is not None: 38 | # query your data! 39 | # here we have customized the number of documents returned per query to 20, because tweets are really short 40 | query_engine = index.as_query_engine(similarity_top_k=20) 41 | response = query_engine.query(query) 42 | return jsonify({"response": str(response)}) 43 | else: 44 | return jsonify({"error": "query field is missing"}), 400 45 | -------------------------------------------------------------------------------- /flask_app/requirements.txt: -------------------------------------------------------------------------------- 1 | aiohttp==3.8.6 2 | aiosignal==1.3.1 3 | aiostream==0.5.2 4 | annotated-types==0.6.0 5 | anyio==3.7.1 6 | async-timeout==4.0.3 7 | attrs==23.1.0 8 | certifi==2023.7.22 9 | charset-normalizer==3.3.1 10 | click==8.1.7 11 | dataclasses-json==0.5.14 12 | Deprecated==1.2.14 13 | dnspython==2.4.2 14 | Flask==2.2.5 15 | Flask-Cors==4.0.0 16 | frozenlist==1.4.0 17 | fsspec==2023.10.0 18 | greenlet==3.0.1 19 | gunicorn==21.2.0 20 | idna==3.4 21 | itsdangerous==2.1.2 22 | Jinja2==3.1.2 23 | joblib==1.3.2 24 | jsonpatch==1.33 25 | jsonpointer==2.4 26 | langchain==0.0.325 27 | langsmith==0.0.53 28 | llama-index==0.8.55 29 | MarkupSafe==2.1.3 30 | marshmallow==3.20.1 31 | multidict==6.0.4 32 | mypy-extensions==1.0.0 33 | nest-asyncio==1.5.8 34 | nltk==3.8.1 35 | numpy==1.26.1 36 | openai==0.28.1 37 | packaging==23.2 38 | pandas==2.1.2 39 | pydantic==2.4.2 40 | pydantic_core==2.10.1 41 | pymongo==4.5.0 42 | python-dateutil==2.8.2 43 | python-dotenv==1.0.0 44 | pytz==2023.3.post1 45 | PyYAML==6.0.1 46 | regex==2023.10.3 47 | requests==2.31.0 48 | six==1.16.0 49 | sniffio==1.3.0 50 | SQLAlchemy==2.0.22 51 | tenacity==8.2.3 52 | tiktoken==0.5.1 53 | tqdm==4.66.1 54 | typing-inspect==0.9.0 55 | typing_extensions==4.8.0 56 | tzdata==2023.3 57 | urllib3==1.26.18 58 | Werkzeug==2.2.3 59 | wrapt==1.15.0 60 | yarl==1.9.2 61 | -------------------------------------------------------------------------------- /next_app/.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. 2 | 3 | # dependencies 4 | /node_modules 5 | /.pnp 6 | .pnp.js 7 | .yarn/install-state.gz 8 | 9 | # testing 10 | /coverage 11 | 12 | # next.js 13 | /.next/ 14 | /out/ 15 | 16 | # production 17 | /build 18 | 19 | # misc 20 | .DS_Store 21 | *.pem 22 | 23 | # debug 24 | npm-debug.log* 25 | yarn-debug.log* 26 | yarn-error.log* 27 | 28 | # local env files 29 | .env*.local 30 | 31 | # vercel 32 | .vercel 33 | 34 | # typescript 35 | *.tsbuildinfo 36 | next-env.d.ts 37 | -------------------------------------------------------------------------------- /next_app/README.md: -------------------------------------------------------------------------------- 1 | This is a [Next.js](https://nextjs.org/) project bootstrapped with [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app). 2 | 3 | ## Getting Started 4 | 5 | First, run the development server: 6 | 7 | ```bash 8 | npm run dev 9 | # or 10 | yarn dev 11 | # or 12 | pnpm dev 13 | # or 14 | bun dev 15 | ``` 16 | 17 | Open [http://localhost:3000](http://localhost:3000) with your browser to see the result. 18 | 19 | You can start editing the page by modifying `app/page.js`. The page auto-updates as you edit the file. 20 | 21 | This project uses [`next/font`](https://nextjs.org/docs/basic-features/font-optimization) to automatically optimize and load Inter, a custom Google Font. 22 | 23 | ## Learn More 24 | 25 | To learn more about Next.js, take a look at the following resources: 26 | 27 | - [Next.js Documentation](https://nextjs.org/docs) - learn about Next.js features and API. 28 | - [Learn Next.js](https://nextjs.org/learn) - an interactive Next.js tutorial. 29 | 30 | You can check out [the Next.js GitHub repository](https://github.com/vercel/next.js/) - your feedback and contributions are welcome! 31 | 32 | ## Deploy on Vercel 33 | 34 | The easiest way to deploy your Next.js app is to use the [Vercel Platform](https://vercel.com/new?utm_medium=default-template&filter=next.js&utm_source=create-next-app&utm_campaign=create-next-app-readme) from the creators of Next.js. 35 | 36 | Check out our [Next.js deployment documentation](https://nextjs.org/docs/deployment) for more details. 37 | -------------------------------------------------------------------------------- /next_app/app/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/run-llama/mongodb-demo/59f4d4aee16117a2e00e64abd0ebaed8763e2f0a/next_app/app/favicon.ico -------------------------------------------------------------------------------- /next_app/app/globals.css: -------------------------------------------------------------------------------- 1 | :root { 2 | --max-width: 1100px; 3 | --border-radius: 12px; 4 | --font-mono: ui-monospace, Menlo, Monaco, 'Cascadia Mono', 'Segoe UI Mono', 5 | 'Roboto Mono', 'Oxygen Mono', 'Ubuntu Monospace', 'Source Code Pro', 6 | 'Fira Mono', 'Droid Sans Mono', 'Courier New', monospace; 7 | 8 | --foreground-rgb: 0, 0, 0; 9 | --background-start-rgb: 214, 219, 220; 10 | --background-end-rgb: 255, 255, 255; 11 | 12 | --primary-glow: conic-gradient( 13 | from 180deg at 50% 50%, 14 | #16abff33 0deg, 15 | #0885ff33 55deg, 16 | #54d6ff33 120deg, 17 | #0071ff33 160deg, 18 | transparent 360deg 19 | ); 20 | --secondary-glow: radial-gradient( 21 | rgba(255, 255, 255, 1), 22 | rgba(255, 255, 255, 0) 23 | ); 24 | 25 | --tile-start-rgb: 239, 245, 249; 26 | --tile-end-rgb: 228, 232, 233; 27 | --tile-border: conic-gradient( 28 | #00000080, 29 | #00000040, 30 | #00000030, 31 | #00000020, 32 | #00000010, 33 | #00000010, 34 | #00000080 35 | ); 36 | 37 | --callout-rgb: 238, 240, 241; 38 | --callout-border-rgb: 172, 175, 176; 39 | --card-rgb: 180, 185, 188; 40 | --card-border-rgb: 131, 134, 135; 41 | } 42 | 43 | @media (prefers-color-scheme: dark) { 44 | :root { 45 | --foreground-rgb: 255, 255, 255; 46 | --background-start-rgb: 0, 0, 0; 47 | --background-end-rgb: 0, 0, 0; 48 | 49 | --primary-glow: radial-gradient(rgba(1, 65, 255, 0.4), rgba(1, 65, 255, 0)); 50 | --secondary-glow: linear-gradient( 51 | to bottom right, 52 | rgba(1, 65, 255, 0), 53 | rgba(1, 65, 255, 0), 54 | rgba(1, 65, 255, 0.3) 55 | ); 56 | 57 | --tile-start-rgb: 2, 13, 46; 58 | --tile-end-rgb: 2, 5, 19; 59 | --tile-border: conic-gradient( 60 | #ffffff80, 61 | #ffffff40, 62 | #ffffff30, 63 | #ffffff20, 64 | #ffffff10, 65 | #ffffff10, 66 | #ffffff80 67 | ); 68 | 69 | --callout-rgb: 20, 20, 20; 70 | --callout-border-rgb: 108, 108, 108; 71 | --card-rgb: 100, 100, 100; 72 | --card-border-rgb: 200, 200, 200; 73 | } 74 | } 75 | 76 | * { 77 | box-sizing: border-box; 78 | padding: 0; 79 | margin: 0; 80 | } 81 | 82 | html, 83 | body { 84 | max-width: 100vw; 85 | overflow-x: hidden; 86 | } 87 | 88 | body { 89 | color: rgb(var(--foreground-rgb)); 90 | background: linear-gradient( 91 | to bottom, 92 | transparent, 93 | rgb(var(--background-end-rgb)) 94 | ) 95 | rgb(var(--background-start-rgb)); 96 | } 97 | 98 | a { 99 | color: inherit; 100 | text-decoration: none; 101 | } 102 | 103 | @media (prefers-color-scheme: dark) { 104 | html { 105 | color-scheme: dark; 106 | } 107 | } 108 | -------------------------------------------------------------------------------- /next_app/app/layout.js: -------------------------------------------------------------------------------- 1 | import { Inter } from 'next/font/google' 2 | import './globals.css' 3 | 4 | const inter = Inter({ subsets: ['latin'] }) 5 | 6 | export const metadata = { 7 | title: 'MongoDB LlamaIndex demo', 8 | description: '', 9 | } 10 | 11 | export default function RootLayout({ children }) { 12 | return ( 13 | 14 |
{children} 15 | 16 | ) 17 | } 18 | -------------------------------------------------------------------------------- /next_app/app/page.js: -------------------------------------------------------------------------------- 1 | 'use client' 2 | 3 | import styles from './page.module.css' 4 | import React, { useState } from 'react'; 5 | 6 | export default function Home() { 7 | const [query, setQuery] = useState(''); 8 | const [responseText, setResponseText] = useState(''); 9 | 10 | const handleSubmit = async (event) => { 11 | event.preventDefault(); 12 | setResponseText('Thinking...') 13 | 14 | const formData = new FormData(); 15 | formData.append('query', query); 16 | 17 | const response = await fetch(process.env.NEXT_PUBLIC_API_HOST + '/process_form', { 18 | method: 'POST', 19 | body: formData, 20 | }); 21 | 22 | if (response.ok) { 23 | const responseData = await response.json(); 24 | console.log(responseData); 25 | setResponseText(responseData.response) 26 | } else { 27 | console.error('Failed to submit:', response.statusText); 28 | } 29 | }; 30 | 31 | return ( 32 |