update_functions.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #!/usr/bin/env python3
  2. """
  3. 更新 4 个评估函数,使用新的 system/user prompt 结构
  4. """
  5. import re
  6. def read_file(filepath):
  7. with open(filepath, 'r', encoding='utf-8') as f:
  8. return f.read()
  9. def write_file(filepath, content):
  10. with open(filepath, 'w', encoding='utf-8') as f:
  11. f.write(content)
  12. def update_evaluate_is_knowledge(content):
  13. """更新 evaluate_is_knowledge 函数"""
  14. # 查找并替换
  15. old_pattern = r'''try:
  16. prompt_text = PROMPT1_IS_KNOWLEDGE\.format\(
  17. title=post\.title,
  18. body_text=post\.body_text or "",
  19. num_images=len\(image_urls\)
  20. \)
  21. data = await _call_openrouter_api\(prompt_text, image_urls, semaphore\)'''
  22. new_code = '''try:
  23. user_prompt = PROMPT1_USER_TEMPLATE.format(
  24. title=post.title,
  25. body_text=post.body_text or "",
  26. num_images=len(image_urls)
  27. )
  28. data = await _call_openrouter_api(
  29. system_prompt=PROMPT1_SYSTEM,
  30. user_prompt=user_prompt,
  31. image_urls=image_urls,
  32. semaphore=semaphore
  33. )'''
  34. content = re.sub(old_pattern, new_code, content)
  35. print("✅ evaluate_is_knowledge 更新完成")
  36. return content
  37. def update_evaluate_is_content_knowledge(content):
  38. """更新 evaluate_is_content_knowledge 函数"""
  39. old_pattern = r'''try:
  40. prompt_text = PROMPT2_IS_CONTENT_KNOWLEDGE\.format\(
  41. title=post\.title,
  42. body_text=post\.body_text or "",
  43. num_images=len\(image_urls\)
  44. \)
  45. data = await _call_openrouter_api\(prompt_text, image_urls, semaphore\)'''
  46. new_code = '''try:
  47. user_prompt = PROMPT2_USER_TEMPLATE.format(
  48. title=post.title,
  49. body_text=post.body_text or "",
  50. num_images=len(image_urls)
  51. )
  52. data = await _call_openrouter_api(
  53. system_prompt=PROMPT2_SYSTEM,
  54. user_prompt=user_prompt,
  55. image_urls=image_urls,
  56. semaphore=semaphore
  57. )'''
  58. content = re.sub(old_pattern, new_code, content)
  59. print("✅ evaluate_is_content_knowledge 更新完成")
  60. return content
  61. def update_evaluate_purpose_match(content):
  62. """更新 evaluate_purpose_match 函数"""
  63. old_pattern = r'''try:
  64. prompt_text = PROMPT3_PURPOSE_MATCH\.format\(
  65. original_query=original_query,
  66. title=post\.title,
  67. body_text=post\.body_text or "",
  68. num_images=len\(image_urls\)
  69. \)
  70. data = await _call_openrouter_api\(prompt_text, image_urls, semaphore\)'''
  71. new_code = '''try:
  72. user_prompt = PROMPT3_USER_TEMPLATE.format(
  73. original_query=original_query,
  74. title=post.title,
  75. body_text=post.body_text or "",
  76. num_images=len(image_urls)
  77. )
  78. data = await _call_openrouter_api(
  79. system_prompt=PROMPT3_SYSTEM,
  80. user_prompt=user_prompt,
  81. image_urls=image_urls,
  82. semaphore=semaphore
  83. )'''
  84. content = re.sub(old_pattern, new_code, content)
  85. print("✅ evaluate_purpose_match 更新完成")
  86. return content
  87. def update_evaluate_category_match(content):
  88. """更新 evaluate_category_match 函数"""
  89. old_pattern = r'''try:
  90. prompt_text = PROMPT4_CATEGORY_MATCH\.format\(
  91. original_query=original_query,
  92. title=post\.title,
  93. body_text=post\.body_text or "",
  94. num_images=len\(image_urls\)
  95. \)
  96. data = await _call_openrouter_api\(prompt_text, image_urls, semaphore\)'''
  97. new_code = '''try:
  98. user_prompt = PROMPT4_USER_TEMPLATE.format(
  99. original_query=original_query,
  100. title=post.title,
  101. body_text=post.body_text or "",
  102. num_images=len(image_urls)
  103. )
  104. data = await _call_openrouter_api(
  105. system_prompt=PROMPT4_SYSTEM,
  106. user_prompt=user_prompt,
  107. image_urls=image_urls,
  108. semaphore=semaphore
  109. )'''
  110. content = re.sub(old_pattern, new_code, content)
  111. print("✅ evaluate_category_match 更新完成")
  112. return content
  113. def main():
  114. filepath = 'post_evaluator_v3.py'
  115. print("📖 读取文件...")
  116. content = read_file(filepath)
  117. print("\n🔧 更新评估函数...")
  118. content = update_evaluate_is_knowledge(content)
  119. content = update_evaluate_is_content_knowledge(content)
  120. content = update_evaluate_purpose_match(content)
  121. content = update_evaluate_category_match(content)
  122. print("\n💾 保存文件...")
  123. write_file(filepath, content)
  124. print("\n✅ 所有评估函数更新完成!")
  125. if __name__ == '__main__':
  126. main()