task.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import re
  2. import math
  3. from datetime import datetime
  4. from typing import Optional
  5. def prompt_template(
  6. template: str, user_name: str = None, user_location: str = None
  7. ) -> str:
  8. # Get the current date
  9. current_date = datetime.now()
  10. # Format the date to YYYY-MM-DD
  11. formatted_date = current_date.strftime("%Y-%m-%d")
  12. formatted_time = current_date.strftime("%I:%M:%S %p")
  13. template = template.replace("{{CURRENT_DATE}}", formatted_date)
  14. template = template.replace("{{CURRENT_TIME}}", formatted_time)
  15. template = template.replace(
  16. "{{CURRENT_DATETIME}}", f"{formatted_date} {formatted_time}"
  17. )
  18. if user_name:
  19. # Replace {{USER_NAME}} in the template with the user's name
  20. template = template.replace("{{USER_NAME}}", user_name)
  21. if user_location:
  22. # Replace {{USER_LOCATION}} in the template with the current location
  23. template = template.replace("{{USER_LOCATION}}", user_location)
  24. return template
  25. def title_generation_template(
  26. template: str, prompt: str, user: Optional[dict] = None
  27. ) -> str:
  28. def replacement_function(match):
  29. full_match = match.group(0)
  30. start_length = match.group(1)
  31. end_length = match.group(2)
  32. middle_length = match.group(3)
  33. if full_match == "{{prompt}}":
  34. return prompt
  35. elif start_length is not None:
  36. return prompt[: int(start_length)]
  37. elif end_length is not None:
  38. return prompt[-int(end_length) :]
  39. elif middle_length is not None:
  40. middle_length = int(middle_length)
  41. if len(prompt) <= middle_length:
  42. return prompt
  43. start = prompt[: math.ceil(middle_length / 2)]
  44. end = prompt[-math.floor(middle_length / 2) :]
  45. return f"{start}...{end}"
  46. return ""
  47. template = re.sub(
  48. r"{{prompt}}|{{prompt:start:(\d+)}}|{{prompt:end:(\d+)}}|{{prompt:middletruncate:(\d+)}}",
  49. replacement_function,
  50. template,
  51. )
  52. template = prompt_template(
  53. template,
  54. **(
  55. {"user_name": user.get("name"), "user_location": user.get("location")}
  56. if user
  57. else {}
  58. ),
  59. )
  60. return template
  61. def search_query_generation_template(
  62. template: str, prompt: str, user: Optional[dict] = None
  63. ) -> str:
  64. def replacement_function(match):
  65. full_match = match.group(0)
  66. start_length = match.group(1)
  67. end_length = match.group(2)
  68. middle_length = match.group(3)
  69. if full_match == "{{prompt}}":
  70. return prompt
  71. elif start_length is not None:
  72. return prompt[: int(start_length)]
  73. elif end_length is not None:
  74. return prompt[-int(end_length) :]
  75. elif middle_length is not None:
  76. middle_length = int(middle_length)
  77. if len(prompt) <= middle_length:
  78. return prompt
  79. start = prompt[: math.ceil(middle_length / 2)]
  80. end = prompt[-math.floor(middle_length / 2) :]
  81. return f"{start}...{end}"
  82. return ""
  83. template = re.sub(
  84. r"{{prompt}}|{{prompt:start:(\d+)}}|{{prompt:end:(\d+)}}|{{prompt:middletruncate:(\d+)}}",
  85. replacement_function,
  86. template,
  87. )
  88. template = prompt_template(
  89. template,
  90. **(
  91. {"user_name": user.get("name"), "user_location": user.get("location")}
  92. if user
  93. else {}
  94. ),
  95. )
  96. return template
  97. def tools_function_calling_generation_template(template: str, tools_specs: str) -> str:
  98. template = template.replace("{{TOOLS}}", tools_specs)
  99. return template