Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[译] [204] 应对常见的 Copilot 难题 #34

Open
cssmagic opened this issue Mar 1, 2024 · 0 comments
Open

[译] [204] 应对常见的 Copilot 难题 #34

cssmagic opened this issue Mar 1, 2024 · 0 comments

Comments

@cssmagic
Copy link
Owner

cssmagic commented Mar 1, 2024

2.4 Addressing common Copilot challenges

2.4 应对常见的 Copilot 难题

It may seem early in your experience with Copilot to start talking about common challenges with Copilot, but you may have already run into challenges when writing your first program. You’ll certainly encounter some of these working through our next example and in the next chapters, so we wanted to give these to you now.

你才刚开始接触 Copilot,现在就讨论 Copilot 的常见难题似乎为时过早。但你在编写第一个程序时可能已经碰到了这些问题,在接下来的示例和下一章中肯定还会遇到,因此我们想提前给你一些建议。

In our time working with Copilot, we’ve run into a few common challenges. These challenges will likely decrease with time as Copilot improves, but they were still problems at the time of this writing. Although the challenges in table 2.1 are not exhaustive of what you might encounter, we hope our tips on how to address these common challenges will help you get up and running quickly. We’ll keep a running list at our book’s website, so please feel free to reach out to us on the forums if you feel we’ve missed something!

在与 Copilot 的协作过程中,我们遇到了一些常见的挑战。虽然随着 Copilot 的持续改善,这些挑战可能会逐渐减少,但在本书撰写时,这些问题仍然存在。表 2.1 或许无法囊括你将来可能遇到的所有问题,但我们希望这些应对方法能帮助你迅速展开工作。我们会在本书英文版官网上维护一份不断更新的清单,所以如果你觉得我们遗漏了某些内容,请随时通过论坛联系我们!

Table 2.1 Common challenges working with Copilot
表 2.1 在使用 Copilot 过程中的常见难题

Challenge
难题

Comments only
仅得到注释

Description
描述

If you give Copilot a prompt using the comment symbol (#), when you start a newline, it wants to just give you more comments rather than code. For example:

如果你使用注释符号(#)给 Copilot 提供提示词,当你换行时,它可能会生成更多的注释而不是生成代码。例如:

# output "Hello Copilot" to the screen
👉# print "Hello world" to the screen👈

We’ve seen Copilot generate line after line of comments, sometimes repeating itself! When this happens, suggestion 3 (use docstrings) is sometimes the most effective.

我们见过 Copilot 生成一行又一行的注释,有时候还会重复!当发生这种情况时,第 3 条建议(使用文档字符串)往往是最有效的。

Remedies
应对方法
  1. Add a newline (Enter) between your comment and Copilot’s suggestion to help it switch from comments to code.

  2. 在你写的注释和 Copilot 的建议之间添加一个空行(插入一个回车),可以帮助它从注释切换到代码。

  3. If a newline doesn’t work, you can type a letter or two of code (no comment symbol). A couple letters from a keyword in your prompt usually works. For example:

  4. 如果新行不起作用,你可以输入一两个代码字母(不使用注释符号)。以代码关键字的前几个字母作为提示词通常可以奏效。例如:

# output "Hello Copilot" to the screen
👉pr👈

A couple letters from a keyword typically causes Copilot to give a code suggestion.

关键字的几个前字母通常会使 Copilot 给出代码建议。

  1. Switch from using # comments to docstring comments like this:
  2. # 注释换成文档字符串注释,像这样:
"""
output "Hello Copilot" to the screen
"""
  1. Use Ctrl–Enter to see if Copilot will give you suggestions that are code rather than comments

  2. 使用 Ctrl+回车来看看 Copilot 是否可以给出代码而非注释的建议。


Challenge
难题

Wrong code
错误的代码

Description
描述

Sometimes Copilot just gives you obviously wrong code from the start. (You’ll learn throughout this book how to identify incorrect code!)
有时 Copilot 一开始就给出了明显错误的代码。(在本书中,你将学习如何识别错误的代码!)

In addition, sometimes Copilot seems to get stuck down wrong paths. For example, it might seem to be trying to solve a different problem than what you’ve asked it to solve. (Suggestion 3, in particular, can help with getting Copilot to go down a new path.)

此外,有时 Copilot 似乎会陷入错误的路径。例如,它可能会试图解决一个与你要求解决的问题不同的问题。(特别是建议 3,可以帮助 Copilot 走上新的路径。)

Remedies
应对方法

Much of this book is about how to address this problem, but here are some quick tips to get Copilot to help:

本书的很多内容都是关于如何解决这个问题的,不过这里先给出一些能够让 Copilot 恢复正常的快捷技巧:

  1. Change your prompt to see if you can better describe what you need.

  2. 改变你的提示词,看看你是否能更好地描述你的需求。

  3. Try using Ctrl–Enter to find a suggestion from Copilot that is the correct code.

  4. 尝试使用 Ctrl+回车找到 Copilot 的正确代码建议。

  5. Close the VS Code program, wait a little bit, and restart it. This can help clear the Copilot cache to get new suggestions.

  6. 关闭 VS Code 程序,稍等一会儿,然后重启它。这可以帮助清除 Copilot 缓存,从而获取新的建议。

  7. Try breaking down the problem into smaller steps (see chapter 7 for more details).

  8. 尝试将问题分解成更小的步骤(更多细节参见第 7 章)。

  9. Debug the code (see chapter 8).

  10. 调试代码(参见第 8 章)。

  11. Try asking ChatGPT for the code and feed its suggestions into VS Code. A different large language model (LLM) can sometimes give suggestions that help the other LLM to get unstuck.

  12. 尝试向 ChatGPT 请求代码,并将其建议复制到 VS Code。当一个大型语言模型(LLM)陷入僵局时,换一个 LLM 往往可以帮助我们摆脱困境。


Challenge
难题

Copilot gives you
Copilot 给出:

# YOUR CODE HERE

Description
描述

We’ve had Copilot seem to tell us to write our own code by generating this (or similar text) after a prompt:

有时 Copilot 会在一段提示词后生成下面的注释(或类似的文本)来让我们自己写代码:

# YOUR CODE HERE

Remedies
应对方法

We believe this is happening when we ask Copilot to solve a problem that has been given by an instructor to students to solve in the past. Why? Well, when we write our assignments for our students, we (as instructors) often write some code and then tell our students to write the rest by writing

# YOUR CODE HERE

where we want students to write their code. Students tend to leave that comment in their solution code, which means Copilot was trained to think this comment is an important part of the solution (it’s not). Often, we’re able to solve this problem by finding reasonable solutions in the Copilot suggestions with Ctrl–Enter, but please see the solutions for Wrong Code if that doesn’t work.

我们认为,当我们让 Copilot 解决过去教师给学生布置的问题时,这种情况就会发生。为什么呢?因为当我们为学生布置作业时,我们(作为教师)经常会写出开头的代码,然后通过 # YOUR CODE HERE 来告诉学生该在哪里写出他们的代码。学生们往往会在他们的解决方案代码中保留这行注释,这意味着 Copilot 在训练时会认为这个注释是解决方案的重要部分(实际上不是)。通常,我们可以通过 Ctrl+回车来解决这个问题,因为在 Copilot 的多条建议中通常可以找到合理的解决方案。但如果这个方法不起作用,请参见上面“错误的代码”那一行的解决方案。


Challenge
难题

Missing modules
缺少模块

Description
描述

Copilot gives you code, but it won’t work because there are modules missing. (Modules are additional libraries that can be added to Python to give prebuilt functionality.)
Copilot 给出了代码,但由于缺少模块而无法运行。(模块是可以添加到 Python 中的额外代码包,可以提供预先构建好的特定功能。)

Remedies
应对方法

See section 2.5, under “Modules” for how to install new modules on your machine.

请参阅下一节中的“Python模块”段落,了解如何在你的机器上安装新模块。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant