Upload 9 files
Browse files- .env +2 -0
- Dockerfile.txt +10 -0
- README2.md +14 -0
- app.py +15 -0
- docker-compose.yml +17 -0
- models/vector_model.py +5 -0
- requirements.txt +4 -0
- routes/vector_routes.py +20 -0
- utils/database.py +23 -0
.env
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
MONGO_URI=mongodb+srv://jaothan26:LP8YhV1EocgW97vu@cluster0.ibvao.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0
|
| 2 |
+
MONGO_DB_NAME=vector_db
|
Dockerfile.txt
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.9-slim
|
| 2 |
+
|
| 3 |
+
WORKDIR /app
|
| 4 |
+
|
| 5 |
+
COPY requirements.txt .
|
| 6 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 7 |
+
|
| 8 |
+
COPY . .
|
| 9 |
+
|
| 10 |
+
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "3000"]
|
README2.md
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# MongoDB Vector Search Project
|
| 2 |
+
|
| 3 |
+
This project demonstrates how to use MongoDB with vector search capabilities in a Dockerized environment. It includes a FastAPI application that interacts with MongoDB to store and retrieve vectors.
|
| 4 |
+
|
| 5 |
+
## Setup
|
| 6 |
+
|
| 7 |
+
1. Clone the repository.
|
| 8 |
+
2. Create a `.env` file with the necessary environment variables.
|
| 9 |
+
3. Run `docker-compose up` to start the services.
|
| 10 |
+
|
| 11 |
+
## API Endpoints
|
| 12 |
+
|
| 13 |
+
- `POST /vectors/`: Create a new vector.
|
| 14 |
+
- `GET /vectors/{vector_id}`: Retrieve a vector by its ID.
|
app.py
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
+
from app.routes.vector_routes import router as vector_router
|
| 3 |
+
from app.utils.database import connect_to_mongo, close_mongo_connection
|
| 4 |
+
|
| 5 |
+
app = FastAPI()
|
| 6 |
+
|
| 7 |
+
app.include_router(vector_router)
|
| 8 |
+
|
| 9 |
+
@app.on_event("startup")
|
| 10 |
+
async def startup_db_client():
|
| 11 |
+
await connect_to_mongo()
|
| 12 |
+
|
| 13 |
+
@app.on_event("shutdown")
|
| 14 |
+
async def shutdown_db_client():
|
| 15 |
+
await close_mongo_connection()
|
docker-compose.yml
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version: '3.8'
|
| 2 |
+
|
| 3 |
+
services:
|
| 4 |
+
app:
|
| 5 |
+
build: .
|
| 6 |
+
env_file:
|
| 7 |
+
- .env
|
| 8 |
+
depends_on:
|
| 9 |
+
mongodb:
|
| 10 |
+
condition: service_healthy
|
| 11 |
+
ports:
|
| 12 |
+
- "3000:3000"
|
| 13 |
+
environment:
|
| 14 |
+
- MONGO_URI=mongodb://${MONGO_USERNAME}:${MONGO_PASSWORD}@mongodb:27017/
|
| 15 |
+
- MONGO_DB_NAME=${MONGO_DB_NAME}
|
| 16 |
+
|
| 17 |
+
|
models/vector_model.py
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from pydantic import BaseModel
|
| 2 |
+
|
| 3 |
+
class Vector(BaseModel):
|
| 4 |
+
vector: list[float]
|
| 5 |
+
metadata: dict
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi==0.95.2
|
| 2 |
+
uvicorn==0.22.0
|
| 3 |
+
pymongo==4.3.3
|
| 4 |
+
pydantic==1.10.7
|
routes/vector_routes.py
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import APIRouter, HTTPException
|
| 2 |
+
from app.models.vector_model import Vector
|
| 3 |
+
from app.utils.database import db
|
| 4 |
+
|
| 5 |
+
router = APIRouter()
|
| 6 |
+
|
| 7 |
+
@router.post("/vectors/")
|
| 8 |
+
async def create_vector(vector: Vector):
|
| 9 |
+
vector_dict = vector.dict()
|
| 10 |
+
result = db.vectors.insert_one(vector_dict)
|
| 11 |
+
if result.inserted_id:
|
| 12 |
+
return {"id": str(result.inserted_id)}
|
| 13 |
+
raise HTTPException(status_code=500, detail="Failed to insert vector")
|
| 14 |
+
|
| 15 |
+
@router.get("/vectors/{vector_id}")
|
| 16 |
+
async def get_vector(vector_id: str):
|
| 17 |
+
vector = db.vectors.find_one({"_id": vector_id})
|
| 18 |
+
if vector:
|
| 19 |
+
return vector
|
| 20 |
+
raise HTTPException(status_code=404, detail="Vector not found")
|
utils/database.py
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from pymongo import MongoClient
|
| 2 |
+
from pymongo.errors import ConnectionFailure
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
client = None
|
| 6 |
+
db = None
|
| 7 |
+
|
| 8 |
+
async def connect_to_mongo():
|
| 9 |
+
global client, db
|
| 10 |
+
mongo_uri = os.getenv("MONGO_URI")
|
| 11 |
+
db_name = os.getenv("MONGO_DB_NAME")
|
| 12 |
+
client = MongoClient(mongo_uri)
|
| 13 |
+
db = client[db_name]
|
| 14 |
+
try:
|
| 15 |
+
client.admin.command('ping')
|
| 16 |
+
print("Pinged your deployment. You successfully connected to MongoDB!")
|
| 17 |
+
except ConnectionFailure as e:
|
| 18 |
+
print("Could not connect to MongoDB:", e)
|
| 19 |
+
|
| 20 |
+
async def close_mongo_connection():
|
| 21 |
+
global client
|
| 22 |
+
if client:
|
| 23 |
+
client.close()
|