| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- #!/usr/bin/env python3
- """
- 更新 4 个评估函数,使用新的 system/user prompt 结构
- """
- import re
- def read_file(filepath):
- with open(filepath, 'r', encoding='utf-8') as f:
- return f.read()
- def write_file(filepath, content):
- with open(filepath, 'w', encoding='utf-8') as f:
- f.write(content)
- def update_evaluate_is_knowledge(content):
- """更新 evaluate_is_knowledge 函数"""
- # 查找并替换
- old_pattern = r'''try:
- prompt_text = PROMPT1_IS_KNOWLEDGE\.format\(
- title=post\.title,
- body_text=post\.body_text or "",
- num_images=len\(image_urls\)
- \)
- data = await _call_openrouter_api\(prompt_text, image_urls, semaphore\)'''
- new_code = '''try:
- user_prompt = PROMPT1_USER_TEMPLATE.format(
- title=post.title,
- body_text=post.body_text or "",
- num_images=len(image_urls)
- )
- data = await _call_openrouter_api(
- system_prompt=PROMPT1_SYSTEM,
- user_prompt=user_prompt,
- image_urls=image_urls,
- semaphore=semaphore
- )'''
- content = re.sub(old_pattern, new_code, content)
- print("✅ evaluate_is_knowledge 更新完成")
- return content
- def update_evaluate_is_content_knowledge(content):
- """更新 evaluate_is_content_knowledge 函数"""
- old_pattern = r'''try:
- prompt_text = PROMPT2_IS_CONTENT_KNOWLEDGE\.format\(
- title=post\.title,
- body_text=post\.body_text or "",
- num_images=len\(image_urls\)
- \)
- data = await _call_openrouter_api\(prompt_text, image_urls, semaphore\)'''
- new_code = '''try:
- user_prompt = PROMPT2_USER_TEMPLATE.format(
- title=post.title,
- body_text=post.body_text or "",
- num_images=len(image_urls)
- )
- data = await _call_openrouter_api(
- system_prompt=PROMPT2_SYSTEM,
- user_prompt=user_prompt,
- image_urls=image_urls,
- semaphore=semaphore
- )'''
- content = re.sub(old_pattern, new_code, content)
- print("✅ evaluate_is_content_knowledge 更新完成")
- return content
- def update_evaluate_purpose_match(content):
- """更新 evaluate_purpose_match 函数"""
- old_pattern = r'''try:
- prompt_text = PROMPT3_PURPOSE_MATCH\.format\(
- original_query=original_query,
- title=post\.title,
- body_text=post\.body_text or "",
- num_images=len\(image_urls\)
- \)
- data = await _call_openrouter_api\(prompt_text, image_urls, semaphore\)'''
- new_code = '''try:
- user_prompt = PROMPT3_USER_TEMPLATE.format(
- original_query=original_query,
- title=post.title,
- body_text=post.body_text or "",
- num_images=len(image_urls)
- )
- data = await _call_openrouter_api(
- system_prompt=PROMPT3_SYSTEM,
- user_prompt=user_prompt,
- image_urls=image_urls,
- semaphore=semaphore
- )'''
- content = re.sub(old_pattern, new_code, content)
- print("✅ evaluate_purpose_match 更新完成")
- return content
- def update_evaluate_category_match(content):
- """更新 evaluate_category_match 函数"""
- old_pattern = r'''try:
- prompt_text = PROMPT4_CATEGORY_MATCH\.format\(
- original_query=original_query,
- title=post\.title,
- body_text=post\.body_text or "",
- num_images=len\(image_urls\)
- \)
- data = await _call_openrouter_api\(prompt_text, image_urls, semaphore\)'''
- new_code = '''try:
- user_prompt = PROMPT4_USER_TEMPLATE.format(
- original_query=original_query,
- title=post.title,
- body_text=post.body_text or "",
- num_images=len(image_urls)
- )
- data = await _call_openrouter_api(
- system_prompt=PROMPT4_SYSTEM,
- user_prompt=user_prompt,
- image_urls=image_urls,
- semaphore=semaphore
- )'''
- content = re.sub(old_pattern, new_code, content)
- print("✅ evaluate_category_match 更新完成")
- return content
- def main():
- filepath = 'post_evaluator_v3.py'
- print("📖 读取文件...")
- content = read_file(filepath)
- print("\n🔧 更新评估函数...")
- content = update_evaluate_is_knowledge(content)
- content = update_evaluate_is_content_knowledge(content)
- content = update_evaluate_purpose_match(content)
- content = update_evaluate_category_match(content)
- print("\n💾 保存文件...")
- write_file(filepath, content)
- print("\n✅ 所有评估函数更新完成!")
- if __name__ == '__main__':
- main()
|