t_simple_mongo.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. # pip install llama-index-readers-mongodb
  2. from llama_index.readers.mongodb import SimpleMongoReader
  3. from config.settings import MONGO_URL, MONGO_DB_NAME
  4. from src.manager.template_manager import TemplateManager, TemplateService
  5. # Initialize SimpleMongoReader
  6. reader = SimpleMongoReader(
  7. uri=MONGO_URL, # Provide the URI if not using host and port
  8. )
  9. def simple_load_mongodata():
  10. # Lazy load data from MongoDB
  11. documents = reader.load_data(
  12. db_name="test", # Name of the database
  13. collection_name="Product", # Name of the collection
  14. field_names=[
  15. "competitor_analyze"
  16. ], # Names of the fields to concatenate (default: ["text"])
  17. separator="", # Separator between fields (default: "")
  18. query_dict=None, # Query to filter documents (default: None)
  19. max_docs=0, # Maximum number of documents to load (default: 0)
  20. metadata_names=None, # Names of the fields to add to metadata attribute (default: None)
  21. )
  22. for doc in documents:
  23. print(doc.get_content())
  24. def get_jinja2_env():
  25. from jinja2 import Environment, meta
  26. env = Environment()
  27. template_source = '[{"$match": {"basic_info.name": "{{product_name}}"}}, {"$project": {"basic_info": 1, "_id": {{show_id}}}}]'
  28. parsed_content = env.parse(template_source)
  29. variables = meta.find_undeclared_variables(parsed_content)
  30. print(variables) # 输出: {'product_name'}
  31. async def query_dict_load_mongodata():
  32. manager = TemplateManager()
  33. await manager.initialize()
  34. tempalte_mmodel = await manager.get_template("product_info")
  35. manager.render_template(tempalte_mmodel, {"product_name": "测试"})
  36. await reader.aload_data(
  37. db_name="test", # Name of the database
  38. collection_name="Product", # Name of the collection
  39. )
  40. import asyncio
  41. import aiofiles
  42. import os
  43. import sys
  44. async def task():
  45. get_jinja2_env()
  46. def main():
  47. asyncio.run(task())
  48. if __name__ == "__main__":
  49. main()