← 返回目录

Code prompting

第 19 / 34 卷日期:

代码提示

Gemini 主要专注于基于文本的提示,也包括为返回代码编写提示。让我们前往 Ve ex AI Studio 测试这些提示,查看一些编码示例。

Prompts for writing code

Gemini 还能充当开发者角色,协助您用任意编程语言编写代码。作为开发者,这能帮助您加速编码过程。

想象一下,你的机器上有一个包含数百个文件的文件夹,这些文件需要重命名。逐个重命名会花费大量时间。你懂一点 Bash,可以写个脚本来自动化这个任务,但这可能也需要一些时间。那么,我们来写个提示词吧。你可以在公开的消费者聊天机器人 Gemini 中编写提示词,或者如果你更关注保密性,可以在你的 Google Cloud 账户内编写这些提示词并打开 Ve ex AI Studio。Ve ex AI Studio 的优势在于你可以配置温度等参数。

Name1_bash 重命名文件夹中的文件

Goal编写一个提示,用于在Bash中重命名文件夹中的文件。
模型gemini-pro
温度n`x0np 令牌限制 1024

Top‑K 采样Top‑P 采样

N/A1 提示词Write a code snippet in Bash, which asks for a folder name.

Then it takes the contents of the folder and renames all the files inside by prepending the name draft to the file name.

输出```bash #!/bin/bash

# Ask for the folder name echo "Enter the folder name: "

read folder_name

# Check if the folder exists if [ ! -d "$folder_name" ]; then echo "Folder does not exist."

exit 1 fi

# Get the list of files in the folder files=( "$folder_name"/* )

# Rename each file by prepending "draft" to the file name for file in "${files[@]}"; do new_file_name="draft_$(basename "$file")"

mv "$file" "$new_file_name"

done

echo "Files renamed successfully."

```

Table 16. Using Gemini to program code

这段代码看起来不错 —— 甚至还有文档说明!但由于大语言模型不具备推理能力且会复述训练数据,务必亲自阅读并测试你的代码。

我们翘首以盼的时刻到了,它真的能运行吗?

全站分卷索引 · 34 卷
  1. 1Introduction
  2. 2Prompt engineering
  3. 3LLM output configuration
  4. 4Sampling controls
  5. 5Top-K and top-P
  6. 6Putting it all together
  7. 7Prompting techniques
  8. 8One-shot & few-shot
  9. 9System, contextual and role prompting
  10. 10System prompting
  11. 11Role prompting
  12. 12Contextual prompting
  13. 13Step-back prompting
  14. 14Chain of Thought (CoT)
  15. 15Self-consistency
  16. 16Tree of Thoughts (ToT)
  17. 17ReAct (reason & act)
  18. 18Automatic Prompt Engineering
  19. 19Code prompting
  20. 20Prompts for explaining code
  21. 21Prompts for translating code
  22. 22Prompts for debugging and reviewing code
  23. 23What about multimodal prompting?
  24. 24Design with simplicity
  25. 25Be specific about the output
  26. 26Control the max token length
  27. 27Experiment with input formats and writing styles
  28. 28Adapt to model updates
  29. 29JSON Repair
  30. 30Working with Schemas
  31. 31Experiment together with other prompt engineers
  32. 32CoT Best practices
  33. 33Summary
  34. 34Endnotes