import pymongo
import openai
import os
from llama_index import VectorStoreIndex, StorageContext, SimpleDirectoryReader
from llama_index.vector_stores.mongodb import MongoDBAtlasVectorSearch
from llama_index.storage.storage_context import StorageContext
openai.api_key = os.getenv("OPENAI_API_KEY")
# setup mongo connection
mongo_uri = os.environ(mongo_uri)
# setup client
mongodb_client = pymongo.MongoClient(mongo_uri)
if mongodb_client :
print("Connection is successful!")
else:
print("Connection is not successful!")
# setup store
store = MongoDBAtlasVectorSearch(mongodb_client)
# print(store)
# setup storage context
storage_context = StorageContext.from_defaults(
vector_store= store
)
documents = SimpleDirectoryReader("input/text").load_data()
index = VectorStoreIndex.from_documents(documents, storage_context= storage_context)
print(index)
# ask query
query_engine = index.as_query_engine()
response = query_engine.query("What did author work on after college?")
print(response)
documents
, Check the length of it by doing print(len(documents))
for doc in documents: print(doc.text)
print(len(documents) --> 3
I can also see the output of all files when printing using print(doc.txt)
from llama_index import VectorStoreIndex, SimpleDirectoryReader documents = SimpleDirectoryReader('input/text').load_data() index = VectorStoreIndex.from_documents(documents) query_engine = index.as_query_engine() response = query_engine.query("What did author work on after college?") print(response)
# setup store
# store = MongoDBAtlasVectorSearch(mongodb_client)
# print(store)
# setup index
storage_context = StorageContext.from_defaults( docstore=MongoDocumentStore.from_uri(uri=mongo_uri),
index_store=MongoIndexStore.from_uri(uri=mongo_uri),
)
store = MongoDBAtlasVectorSearch(mongodb_client)
part and when index is created do print print(index.docstore.docs)