生成函数注释.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. from toolbox import update_ui
  2. from toolbox import CatchException, report_exception
  3. from toolbox import write_history_to_file, promote_file_to_downloadzone
  4. from .crazy_utils import request_gpt_model_in_new_thread_with_ui_alive
  5. fast_debug = False
  6. def 生成函数注释(file_manifest, project_folder, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt):
  7. import time, os
  8. print('begin analysis on:', file_manifest)
  9. for index, fp in enumerate(file_manifest):
  10. with open(fp, 'r', encoding='utf-8', errors='replace') as f:
  11. file_content = f.read()
  12. i_say = f'请对下面的程序文件做一个概述,并对文件中的所有函数生成注释,使用markdown表格输出结果,文件名是{os.path.relpath(fp, project_folder)},文件内容是 ```{file_content}```'
  13. i_say_show_user = f'[{index}/{len(file_manifest)}] 请对下面的程序文件做一个概述,并对文件中的所有函数生成注释: {os.path.abspath(fp)}'
  14. chatbot.append((i_say_show_user, "[Local Message] waiting gpt response."))
  15. yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
  16. if not fast_debug:
  17. msg = '正常'
  18. # ** gpt request **
  19. gpt_say = yield from request_gpt_model_in_new_thread_with_ui_alive(
  20. i_say, i_say_show_user, llm_kwargs, chatbot, history=[], sys_prompt=system_prompt) # 带超时倒计时
  21. chatbot[-1] = (i_say_show_user, gpt_say)
  22. history.append(i_say_show_user); history.append(gpt_say)
  23. yield from update_ui(chatbot=chatbot, history=history, msg=msg) # 刷新界面
  24. if not fast_debug: time.sleep(2)
  25. if not fast_debug:
  26. res = write_history_to_file(history)
  27. promote_file_to_downloadzone(res, chatbot=chatbot)
  28. chatbot.append(("完成了吗?", res))
  29. yield from update_ui(chatbot=chatbot, history=history, msg=msg) # 刷新界面
  30. @CatchException
  31. def 批量生成函数注释(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, web_port):
  32. history = [] # 清空历史,以免输入溢出
  33. import glob, os
  34. if os.path.exists(txt):
  35. project_folder = txt
  36. else:
  37. if txt == "": txt = '空空如也的输入栏'
  38. report_exception(chatbot, history, a = f"解析项目: {txt}", b = f"找不到本地项目或无权访问: {txt}")
  39. yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
  40. return
  41. file_manifest = [f for f in glob.glob(f'{project_folder}/**/*.py', recursive=True)] + \
  42. [f for f in glob.glob(f'{project_folder}/**/*.cpp', recursive=True)]
  43. if len(file_manifest) == 0:
  44. report_exception(chatbot, history, a = f"解析项目: {txt}", b = f"找不到任何.tex文件: {txt}")
  45. yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
  46. return
  47. yield from 生成函数注释(file_manifest, project_folder, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt)