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 this when writing your first program and you’ll certainly encounter some of these working through our next example and in the next chapter, 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 issues at the time of 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

Description

Remedies

Comments only

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:

# 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.

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

2. 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:

# output "Hello Copilot" to the screen

pr

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

3. Switch from using # comments to docstring comments like this:

"""

output "Hello Copilot" to the screen

"""

4. Use Ctrl–Enter to see if Copilot will give you suggestions that are code rather than comments

Wrong code

Sometimes Copilot just gives you obviously wrong code from the start. (You’ll learn throughout this book how to identify incorrect code!)

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.)

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

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

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

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

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

5. Debug the code (see chapter 8).

6. 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.

Copilot gives you

# YOUR CODE HERE

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

# YOUR CODE HERE

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.

Missing modules

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.)

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

难题

描述

解决方法

仅限注释

如果你使用注释符号(#)给 Copilot 提供提示词,当你开始新的一行时,它只会给你更多的注释而不是代码。例如:

# 在屏幕上输出 “Hello Copilot”

# 在屏幕上打印 “Hello world”

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

1. 在你的注释和 Copilot 的建议之间添加一个新行(Enter),以帮助它从注释切换到代码。

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

# 在屏幕上输出 “Hello Copilot”

pr

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

3. 从使用 # 注释切换到使用文档字符串注释,像这样:

"""

在屏幕上输出 “Hello Copilot”

"""

4. 使用 Ctrl–Enter 看看 Copilot 是否会给出代码而非注释的建议

错误的代码

有时 Copilot 一开始就给出了明显错误的代码。(在本书中,你将学习如何识别错误的代码!)

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

本书的很多内容都是关于如何解决这个问题的,但这里有一些快速提示可以让 Copilot 帮到你:

1. 改变你的提示词,看看你是否能更好地描述你需要的内容。

2. 尝试使用 Ctrl–Enter 找到 Copilot 的正确代码建议。

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

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

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

6. 尝试向 ChatGPT 请求代码,并将其建议输入 VS Code。不同的大型语言模型(LLM)有时可以提供帮助,让另一个 LLM 从困境中解脱出来。

Copilot 给出

# YOUR CODE HERE

我们有时会让 Copilot 在提示词后生成这样(或类似的文本)来告诉我们写自己的代码:

# YOUR CODE HERE

我们认为,当我们让 Copilot 解决过去教师给学生出的问题时,这种情况就会发生。为什么呢?因为当我们为我们的学生编写作业时,我们(作为教师)经常会写一些代码,然后通过写

# YOUR CODE HERE

来告诉我们的学生在哪里编写他们的代码。学生们往往会在他们的解决方案代码中保留那个评论,这意味着 Copilot 被训练认为这个评论是解决方案的重要部分(实际上不是)。通常,我们能够通过在 Copilot 建议中使用 Ctrl–Enter 找到合理的解决方案,但如果那不起作用,请参见“错误的代码”的解决方案。

缺少模块

Copilot 给出了代码,但由于缺少模块而无法工作。(模块是可以添加到 Python 中的额外库,用以提供预构建的功能。)

请参阅第 2.5 节的“模块”,了解如何在你的机器上安装新模块。

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

No branches or pull requests

1 participant