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