计算化学公社

 找回密码 Forget password
 注册 Register
Views: 454|回复 Reply: 3
打印 Print 上一主题 Last thread 下一主题 Next thread

[程序/脚本开发] 用AI辅助写简单分析小程序应用笔记

[复制链接 Copy URL]

485

帖子

1

威望

1131

eV
积分
1636

Level 5 (御坂)

A Student

跳转到指定楼层 Go to specific reply
楼主
本帖最后由 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. 模板例子

  1. **Aim and general**
  2. 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.}

  3. {确保脚本与python environment的Library相容} Refer to the attached conda environment for library versions, ensure compatibility of the script with the environment.

  4. If there is anything uncertain, ask me first before you proceed.

  5. **What the script should do**
  6. 1. Estimate memory usage of the run using {用来测memory usage的library}.
  7. 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.
  8. 3. {如何读取输入文件,例如定义receptor ligand、xvg文件要用第几个column}
  9. 4. {缺少文件的处理方法,如预设值或warning;预处理如align trajectory on protein C-alpha}
  10. 5. {要做的分析,如calculate CV using the equation in the attached reference,输出哪些文件,如绘图跟data file}
  11. 6. {绘图用的参数,如字体大小、颜色、labels、error bars}
  12. 7. {Optional的分析和输出}

  13. Finally,
  14. 8. {输出Log文件内容} Log all steps to analysis.log, report the memory usage of the run after all the tasks have finished.
  15. 9. {报错写入Log方便排查} Catch all the errors and log in analysis.log.

复制代码


4. 结语
上文分享了个人用AI辅助写分析小程序的笔记,如果大家有好用的技巧也欢迎留言分享讨论,如有问题也请不吝指正。

评分 Rate

参与人数
Participants 2
eV +6 收起 理由
Reason
sobereva + 5
Rreido + 1 GJ!

查看全部评分 View all ratings

敬仰一针见血的指责,厌倦别有用心的赞美。

166

帖子

0

威望

3347

eV
积分
3513

Level 5 (御坂)

2#
发表于 Post on 2025-8-2 02:20:12 | 只看该作者 Only view this author
各位大佬最近有尝试让claude code/gemini cli构建md的结构吗,已经准备好了各种分子的pdb和itp文件,告诉cc用mdanalysis读取pdb文件的关键信息,packmol进行建模,结果一塌糊涂,是prompt不合适吗

485

帖子

1

威望

1131

eV
积分
1636

Level 5 (御坂)

A Student

3#
 楼主 Author| 发表于 Post on 2025-8-2 03:59:39 | 只看该作者 Only view this author
本帖最后由 student0618 于 2025-8-3 02:12 编辑

我自己用AI的前提是让他作一个任务是可以提高效率。一些较specific的tasks、或者是不用一分钟就写完的脚本还是自己弄更快,让AI帮忙还要整理好逻辑写prompt,反而更慢。

建模跑模拟目前还是手动更快更好,尤其是複杂的体系还是手动较安全。而且就算是普适有html manual的软件如gmx,AI也很常搞混指令跟版本等,还不如直接看手册ctrl-F搜关键词。很多时候这些tasks用 AI反而降低了工作效率。
论坛上其他相关评论可参考 http://bbs.keinsci.com/thread-54071-1-1.htmlhttp://bbs.keinsci.com/thread-52382-1-1.html

目前比较放心用AI而且感觉真显着提高工作效率的tasks有例如
  • 让他写python脚本用numpy scipy 等库分析、matplotlib seaborn作图等。这些都有海量公开资料作training data的。较专业的任务如写mdp还是不太行。
  • 给它一个分析MD软件A模拟数据的脚本,让他改成分析MD软件B模拟数据的脚本。附件要提供AB软件模拟数据格式或样本。
  • 给AI一个自己写的 Pseudocode 或者计划大纲、研究方向,让他提问还缺什么。很多时候能帮忙找到可改善之处,启发新思路。
  • 给它指定的目的,讨论代码框架、不同方向不同写法的利弊。
  • 聊聊研究思路、整合各种资讯、Literature review初筛。
  • 跟它吐槽诉苦,提供情绪价值,避免内耗。这要找对AI用对mode,不然有些AI比人类更虚伪、或者只是教科书式让你document everything/重复几句样版式鼓励句子。我自己有时想AI模仿一位如今很少联系的老师给点鼓励,写了至少一千字背景设定、语气、例句、不同情况应有反应等。


敬仰一针见血的指责,厌倦别有用心的赞美。

485

帖子

1

威望

1131

eV
积分
1636

Level 5 (御坂)

A Student

4#
 楼主 Author| 发表于 Post on 2025-8-2 14:11:56 | 只看该作者 Only view this author
本帖最后由 student0618 于 2025-8-2 14:13 编辑
neocc 发表于 2025-8-2 02:20
各位大佬最近有尝试让claude code/gemini cli构建md的结构吗,已经准备好了各种分子的pdb和itp文件,告诉cc ...

而且真要试的话,建模过程的步骤、不同if else设定等,很可能都要完整写出来才能用,要写的字可能更多。

还不如自己写,先用一个体系试错。能用后,要重复给相似体系建模的话写个模板input 每次修改、或者用bash脚本自动化(e.g. sed 改文件名、variable 定义放多少分子等)。
敬仰一针见血的指责,厌倦别有用心的赞美。

本版积分规则 Credits rule

手机版 Mobile version|北京科音自然科学研究中心 Beijing Kein Research Center for Natural Sciences|京公网安备 11010502035419号|计算化学公社 — 北京科音旗下高水平计算化学交流论坛 ( 京ICP备14038949号-1 )|网站地图

GMT+8, 2025-8-12 08:01 , Processed in 0.166887 second(s), 22 queries , Gzip On.

快速回复 返回顶部 返回列表 Return to list