|
本帖最后由 student0618 于 2025-7-18 17:18 编辑
0. 前言
近日需要写点python code作些比较冷门的MD分析,但个人只懂bash,python是能看不能写的程度。以前要写python 脚本我是写好pseudo code后逐行代码Google的,如今有AI无论是debug 还是文档也可以省不少时间。当然,以后有时间的话还是打算多学些基础的,就是太忙要先把工作做好。
跟同学闲聊时见都不少人也是想找AI帮忙写code,但怎样也用不了。在此分享一下试错试出来比较有用的prompt结构,希望可以帮助大家更有效地利用工具。生成脚本后也要调整/debug的,但一开始的prompt有完整逻辑可更有效率,有问题更容易排查解决。
1. 软件
结构相近的Prompt在copilot、chatgpt、gemini等试用过,目前较常用社长推荐的grok3 (免费版think mode),感觉他比较完整的抓到我所有的需求,尤其是当要做的分析比较複杂时。没有用专门写code的AI、没付费试grok4。
2. Prompt 结构
段落1:清楚、详细地描述所有的需求及目的
段落2:code架构
附件:含Library版本的conda environment yml、要implement公式的参考文献(如有,可以的话把公式的latex/mathml code和protocol部份先抓出来更好)
3. 模板例子
- **Aim and general**
- Write a python script to perform trajectory analysis using {选用的python library,如 mdanalysis to load the trajectory, numpy to do numerical calculation, scipy for clustering}. {根据经验要给的指示,如 Only load the trajectory once. Improve the code efficiency by minimizing the use of for-loops. Clean up any unnecessary repeating actions. Optimize the analysis on a large dataset.} {个人偏好的输出文件格式,如 For the data files to save, use spaces instead of comma or tab as separater.} {计算用的单位, 如Use kJ/mol as the free energy unit, nanometer as distance unit, degree as angle unit.}
- {确保脚本与python environment的Library相容} Refer to the attached conda environment for library versions, ensure compatibility of the script with the environment.
- If there is anything uncertain, ask me first before you proceed.
- **What the script should do**
- 1. Estimate memory usage of the run using {用来测memory usage的library}.
- 2. get the filename of {必要的输入文件,例如 topology (e.g. topology.pdb)), trajectory (e.g. trajectory.xtc), and weight (e.g. weights.xvg)}; also {必要的参数如residue ID to be analyzed, temperature in K}, and {optional的输入文件和参数} from command line option. {个人喜好所有预设参数放一起置顶方便修改}All the default variables should be defined at the beginning of the script.
- 3. {如何读取输入文件,例如定义receptor ligand、xvg文件要用第几个column}
- 4. {缺少文件的处理方法,如预设值或warning;预处理如align trajectory on protein C-alpha}
- 5. {要做的分析,如calculate CV using the equation in the attached reference,输出哪些文件,如绘图跟data file}
- 6. {绘图用的参数,如字体大小、颜色、labels、error bars}
- 7. {Optional的分析和输出}
- Finally,
- 8. {输出Log文件内容} Log all steps to analysis.log, report the memory usage of the run after all the tasks have finished.
- 9. {报错写入Log方便排查} Catch all the errors and log in analysis.log.
复制代码
4. 结语
上文分享了个人用AI辅助写分析小程序的笔记,如果大家有好用的技巧也欢迎留言分享讨论,如有问题也请不吝指正。
|
评分 Rate
-
查看全部评分 View all ratings
|