task.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. else:
  22. # Replace {{USER_NAME}} in the template with "Unknown"
  23. template = template.replace("{{USER_NAME}}", "Unknown")
  24. if user_location:
  25. # Replace {{USER_LOCATION}} in the template with the current location
  26. template = template.replace("{{USER_LOCATION}}", user_location)
  27. else:
  28. # Replace {{USER_LOCATION}} in the template with "Unknown"
  29. template = template.replace("{{USER_LOCATION}}", "Unknown")
  30. return template
  31. def title_generation_template(
  32. template: str, prompt: str, user: Optional[dict] = None
  33. ) -> str:
  34. def replacement_function(match):
  35. full_match = match.group(0)
  36. start_length = match.group(1)
  37. end_length = match.group(2)
  38. middle_length = match.group(3)
  39. if full_match == "{{prompt}}":
  40. return prompt
  41. elif start_length is not None:
  42. return prompt[: int(start_length)]
  43. elif end_length is not None:
  44. return prompt[-int(end_length) :]
  45. elif middle_length is not None:
  46. middle_length = int(middle_length)
  47. if len(prompt) <= middle_length:
  48. return prompt
  49. start = prompt[: math.ceil(middle_length / 2)]
  50. end = prompt[-math.floor(middle_length / 2) :]
  51. return f"{start}...{end}"
  52. return ""
  53. template = re.sub(
  54. r"{{prompt}}|{{prompt:start:(\d+)}}|{{prompt:end:(\d+)}}|{{prompt:middletruncate:(\d+)}}",
  55. replacement_function,
  56. template,
  57. )
  58. template = prompt_template(
  59. template,
  60. **(
  61. {"user_name": user.get("name"), "user_location": user.get("location")}
  62. if user
  63. else {}
  64. ),
  65. )
  66. return template
  67. def search_query_generation_template(
  68. template: str, prompt: str, user: Optional[dict] = None
  69. ) -> str:
  70. def replacement_function(match):
  71. full_match = match.group(0)
  72. start_length = match.group(1)
  73. end_length = match.group(2)
  74. middle_length = match.group(3)
  75. if full_match == "{{prompt}}":
  76. return prompt
  77. elif start_length is not None:
  78. return prompt[: int(start_length)]
  79. elif end_length is not None:
  80. return prompt[-int(end_length) :]
  81. elif middle_length is not None:
  82. middle_length = int(middle_length)
  83. if len(prompt) <= middle_length:
  84. return prompt
  85. start = prompt[: math.ceil(middle_length / 2)]
  86. end = prompt[-math.floor(middle_length / 2) :]
  87. return f"{start}...{end}"
  88. return ""
  89. template = re.sub(
  90. r"{{prompt}}|{{prompt:start:(\d+)}}|{{prompt:end:(\d+)}}|{{prompt:middletruncate:(\d+)}}",
  91. replacement_function,
  92. template,
  93. )
  94. template = prompt_template(
  95. template,
  96. **(
  97. {"user_name": user.get("name"), "user_location": user.get("location")}
  98. if user
  99. else {}
  100. ),
  101. )
  102. return template
  103. def tools_function_calling_generation_template(template: str, tools_specs: str) -> str:
  104. template = template.replace("{{TOOLS}}", tools_specs)
  105. return template