← 返回目录

Prompts for translating code

第 21 / 34 卷日期:

输出3. ** 文件列表 **:

  • 使用 ŠÀ 文件 ‚P(“$folder_”) 列出指定文件夹内的所有文件

名称 ”/* )ŠÀ ~ 命令。这会创建一个名为 ŠÀ文件ŠÀ ~ 的数组,包含路径到文件夹中所有文件的路径。

  1. ** 文件重命名 **:
  • 它使用 ŠÀ forŠÀ ~ 循环遍历 ŠÀ 文件ŠÀ ~ 数组中的每个文件。
  • 对于每个文件,它使用 ŠÀ new_file_

名称 ‚P”dra _$(basename “$ 文件 ”)” ŠÀ ~ 命令生成一个新的文件名。这个新文件名被创建

通过在原始文件名前添加字符串 “dra_” 来实现。

  • 然后使用 ŠÀ mv“$ 文件 ”“$ 新文件名 ”ŠÀ ~ 命令重命名文件,

该命令将文件移动到新的文件名下。

  1. ** 成功消息 **:
  • 在重命名所有文件后,它会显示一条成功消息( “ 文件重命名成功 ”),

以表明重命名操作已成功完成。

表 17. 代码解释提示

代码翻译提示

表 16 中的 bash 代码似乎能正常运行。但若该脚本能提示输入文件名,将更具复用性,理想情况下应作为带界面的独立应用运行。作为起点,Python 比 Bash 更适合开发(网页)应用。大语言模型可协助实现跨语言代码转换,示例见表 18:

Name1_python_ 重命名 _ 文件

Goal编写一个提示词,将Bash代码翻译为Python
模型gemini-pro
温度n`x0np 令牌限制 1024
Top-KN/A Top-P 1
提示TranslatethebelowBashcodetoaPythonsnippet.

```bash #!/bin/bash

echo "Enter the folder name: "

read folder_name

if [ ! -d "$folder_name" ]; then echo "Folder does not exist."

exit 1 fi

files=( "$folder_name"/* )

for file in "${files[@]}"; do new_file_name="draft_$(basename "$file")"

mv "$file" "$new_file_name"

done

echo "Files renamed successfully."

``` 输出```python import os import shutil

# Get the folder name from the user folder_name = input("Enter the folder name: ")

# Check if the folder exists if not os.path.isdir(folder_name):

print("Folder does not exist.") exit(1)

Continues next page...

全站分卷索引 · 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