FastAPI file upload and storing in vector index #12604
Replies: 1 comment
-
Hey there, @PradiptaAtByteIQMind-Graph2023! Good to see you diving into another interesting challenge. To store files received through a FastAPI REST endpoint into an Astra vector index using the provided
Here's a simplified example to illustrate these steps: from fastapi import FastAPI, File, UploadFile
from your_module import AstraDBVectorStore # Adjust the import path as necessary
from typing import List
app = FastAPI()
# Initialize your AstraDBVectorStore (adjust parameters as necessary)
astra_store = AstraDBVectorStore(
collection_name="your_collection_name",
token="your_astra_db_token",
api_endpoint="your_astra_db_api_endpoint",
embedding_dimension=128, # Example dimension
)
@app.post("/upload-files/")
async def create_upload_files(files: List[UploadFile] = File(...)):
for file in files:
contents = await file.read()
# Process the file to extract embeddings and metadata
embeddings, metadata = process_file(contents) # You need to implement this
# Create a node object for each file (assuming you have a way to generate node IDs)
nodes = [BaseNode(node_id=generate_node_id(), embedding=embeddings, metadata=metadata)]
# Store the embeddings and metadata in Astra vector index
astra_store.add(nodes)
return {"message": "Files processed successfully"}
def process_file(file_contents):
# Implement your file processing and embedding extraction logic here
# This is a placeholder function
embeddings = [0.0] * 128 # Example embeddings
metadata = {"example_key": "example_value"}
return embeddings, metadata
def generate_node_id():
# Implement your logic to generate a unique node ID
return "unique_node_id" This example assumes you have a function You can find more details about the Please note that there are specific requirements and constraints for the embeddings or metadata stored using the I hope this helps! If you have any further questions, feel free to ask. Sources
|
Beta Was this translation helpful? Give feedback.
-
Hi
I am recieving the file by uploading from a rest endpoint and want to store it into astra vector index. How can i do it?
Thanks and Regards
Beta Was this translation helpful? Give feedback.
All reactions