template_mode.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import argparse
  2. import asyncio
  3. import json
  4. import re
  5. from typing import List, Dict, Any
  6. from pydantic import ValidationError
  7. from config.settings import MONGO_URL, MONGO_DB_NAME
  8. from utils.logu import get_logger
  9. from src.manager.template_manager import TemplateManager
  10. logger = get_logger('cli')
  11. import asyncio
  12. import aiofiles
  13. import os
  14. import sys
  15. template_str = '''[
  16. {
  17. "$match": {"product_name": "{{ product_name }}"}
  18. },
  19. {
  20. '$project': {
  21. 'combined': {
  22. '$concatArrays': [
  23. {
  24. '$map': {
  25. 'input': '$competitor.results',
  26. 'as': 'result',
  27. 'in': {
  28. 'asin': '$$result.asin',
  29. 'main_key': '$$result.main_key',
  30. 'monthly_searches': '$$result.monthly_searches',
  31. 'type': 'result'
  32. }
  33. }
  34. }, {
  35. '$map': {
  36. 'input': '$competitor.tail_keys',
  37. 'as': 'tail',
  38. 'in': {
  39. 'tail_key': '$$tail.tail_key',
  40. 'monthly_searches': '$$tail.monthly_searches',
  41. 'type': 'tail_key'
  42. }
  43. }
  44. }
  45. ]
  46. }
  47. }
  48. }, {
  49. '$unwind': '$combined'
  50. }, {
  51. '$replaceRoot': {
  52. 'newRoot': '$combined'
  53. }
  54. }
  55. ]'''
  56. async def task():
  57. collections = ['Product', 'templates', ]
  58. m = TemplateManager()
  59. await m.initialize()
  60. name = 'agent.mainkeys_tailkeys'
  61. await m.create_or_update_template(
  62. name=name,
  63. collection_name='agent.product',
  64. template_str=template_str,
  65. description='获取Agent有关的关键词信息,避免 reason 干扰下一步分析', )
  66. res = await m.execute_template(name, context={'product_name': '电线保护套'})
  67. print(res)
  68. def main():
  69. asyncio.run(task())
  70. if __name__ == "__main__":
  71. main()