Actually query the database, print the response

This commit is contained in:
Laurie Voss
2023-10-29 18:43:09 -07:00
parent f9c2d18a86
commit 909f0f18f5
4 changed files with 93 additions and 2 deletions
Binary file not shown.
+27 -2
View File
@@ -1,9 +1,31 @@
from dotenv import load_dotenv
load_dotenv()
from flask import Flask, request, jsonify
from flask_cors import CORS, cross_origin
import os
from pymongo.mongo_client import MongoClient
from pymongo.server_api import ServerApi
from llama_index.vector_stores.mongodb import MongoDBAtlasVectorSearch
from llama_index.indices.vector_store.base import VectorStoreIndex
# Create a new client and connect to the server
client = MongoClient(os.getenv("MONGODB_URI"), server_api=ServerApi('1'))
# connect to Atlas as a vector store
store = MongoDBAtlasVectorSearch(
client,
db_name=os.getenv('MONGODB_DATABASE'), # this is the database where you stored your embeddings
collection_name=os.getenv('MONGODB_VECTORS'), # this is where your embeddings were stored in 2_load_and_index.py
index_name=os.getenv('MONGODB_VECTOR_INDEX') # this is the name of the index you created after loading your data
)
index = VectorStoreIndex.from_vector_store(store)
app = Flask(__name__)
cors = CORS(app)
app.config['CORS_HEADERS'] = 'Content-Type'
# This is just so you can easily tell the app is running
@app.route('/')
def hello_world():
return 'Hello, World!'
@@ -13,7 +35,10 @@ def hello_world():
def process_form():
query = request.form.get('query')
if query is not None:
# Do something with query if necessary
return jsonify({"response": "bar"})
# query your data!
# here we have customized the number of documents returned per query to 20, because tweets are really short
query_engine = index.as_query_engine(similarity_top_k=20)
response = query_engine.query(query)
return jsonify({"response": str(response)})
else:
return jsonify({"error": "query field is missing"}), 400
+6
View File
@@ -5,9 +5,11 @@ import React, { useState } from 'react';
export default function Home() {
const [query, setQuery] = useState('');
const [responseText, setResponseText] = useState('');
const handleSubmit = async (event) => {
event.preventDefault();
setResponseText('Thinking...')
const formData = new FormData();
formData.append('query', query);
@@ -20,6 +22,7 @@ export default function Home() {
if (response.ok) {
const responseData = await response.json();
console.log(responseData);
setResponseText(responseData.response)
} else {
console.error('Failed to submit:', response.statusText);
}
@@ -39,6 +42,9 @@ export default function Home() {
<button type="submit">Submit</button>
</form>
</div>
<div>
{responseText}
</div>
</main>
)
}
+60
View File
@@ -0,0 +1,60 @@
aiohttp==3.8.6
aiosignal==1.3.1
aiostream==0.5.2
annotated-types==0.6.0
anyio==3.7.1
async-timeout==4.0.3
attrs==23.1.0
certifi==2023.7.22
charset-normalizer==3.3.1
click==8.1.7
dataclasses-json==0.5.14
Deprecated==1.2.14
dnspython==2.4.2
Flask==2.2.5
Flask-Cors==4.0.0
frozenlist==1.4.0
fsspec==2023.10.0
greenlet==3.0.1
gunicorn==21.2.0
idna==3.4
itsdangerous==2.1.2
Jinja2==3.1.2
joblib==1.3.2
jsonpatch==1.33
jsonpointer==2.4
langchain==0.0.325
langsmith==0.0.53
llama-index==0.8.55
MarkupSafe==2.1.3
marshmallow==3.20.1
multidict==6.0.4
mypy-extensions==1.0.0
nest-asyncio==1.5.8
nltk==3.8.1
numpy==1.26.1
openai==0.28.1
packaging==23.2
pandas==2.1.2
pydantic==2.4.2
pydantic_core==2.10.1
pymongo==4.5.0
python-dateutil==2.8.2
python-dotenv==1.0.0
pytz==2023.3.post1
PyYAML==6.0.1
regex==2023.10.3
requests==2.31.0
six==1.16.0
sniffio==1.3.0
SQLAlchemy==2.0.22
tenacity==8.2.3
tiktoken==0.5.1
tqdm==4.66.1
typing-inspect==0.9.0
typing_extensions==4.8.0
tzdata==2023.3
urllib3==1.26.18
Werkzeug==2.2.3
wrapt==1.15.0
yarl==1.9.2