TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Ask HN: What ChatGPT prompt do you use to avoid code-writing hallucinations?

5 点作者 strimp09912 个月前
I use ChatGPT to write tutorials for a Python.<p>I often find myself debugging great-looking code that makes up methods or functions.<p>This especially true when instructed to use somewhat niche libraries.<p>Is there a specific prompt technique you use to avoid hallucinations when writing code?

6 条评论

thomascountz12 个月前
Here is Jeremy Howard&#x27;s system prompt [0]. I often have success using a modified version of this.<p>&gt; You are an autoregressive language model that has been fine-tuned with instruction-tuning and RLHF. You carefully provide accurate, factual, thoughtful, nuanced answers, and are brilliant at reasoning. If you think there might not be a correct answer, you say so.<p>&gt;<p>&gt; Since you are autoregressive, each token you produce is another opportunity to use computation, therefore you always spend a few sentences explaining background context, assumptions, and step-by-step thinking BEFORE you try to answer a question.<p>&gt;<p>&gt; Your users are experts in AI and ethics, so they already know you&#x27;re a language model and your capabilities and limitations, so don&#x27;t remind them of that. They&#x27;re familiar with ethical issues in general so you don&#x27;t need to remind them about those either.<p>&gt;<p>&gt; Don&#x27;t be verbose in your answers, but do provide details and examples where it might help the explanation. When showing Python code, minimise vertical space, and do not include comments or docstrings; you do not need to follow PEP8, since your users&#x27; organizations do not do so.<p>[0] - <a href="https:&#x2F;&#x2F;m.youtube.com&#x2F;watch?v=jkrNMKz9pWU" rel="nofollow">https:&#x2F;&#x2F;m.youtube.com&#x2F;watch?v=jkrNMKz9pWU</a>
f0e4c2f712 个月前
Architect the program yourself in your head. Then use gpt to write one function at a time. For obscure libraries, paste in documents directly to the context window and give links to example GitHub repos. Paste errors in to troubleshoot as needed. Architect for many small libraries in their own files so it&#x27;s easier to compartmentalize and let gpt work with smaller blocks of code.
MattGaiser12 个月前
&gt; I often find myself debugging great-looking code that makes up methods or functions.<p>Are you using an IDE? I do some training of models for a few companies and yes, the models all do this at lot, but it is pretty obvious within 10 seconds of pasting the code into PyCharm.
评论 #40484696 未加载
keploy12 个月前
I asked this to ChatGPT only :D, it says prompt should have something like -<p>&quot;Please provide a Python solution using only established libraries documented on the official Python Package Index (PyPI). Avoid suggesting custom or non-standard libraries&quot;
moomoo1112 个月前
I just use my whiteboard honestly.<p>You mentioned you write tutorials so its probably better to just do it yourself.<p>If I misunderstood please excuse me. I also find it impossible to get right results with gpt. Only for really simple stuff, mostly manual, it works.
catlover7612 个月前
Are you using 3.5 or 4? I use 4 for code-related stuff all the time, and it almost never does this.
评论 #40482160 未加载