← 返回目录

Control the max token length

第 26 / 34 卷日期:

控制最大令牌长度

要控制生成式大语言模型( LLM)响应的长度,您可以在配置中设置最大令牌限制,或在提示中明确指定所需长度。例如:

"Explain quantum physics in a tweet length message."

在提示中使用变量

为了实现提示的重用并使其更具动态性,可在提示中使用变量,这些变量可根据不同输入进行替换。如表 20 所示,一个提供城市相关信息的提示案例。无需在提示中硬编码城市名称,改用变量即可。变量能节省时间和精力,避免重复劳动。若需在多条提示中使用相同信息,可将其存储于变量中,并在各提示中引用该变量。这在将提示集成到自有应用程序时尤为实用。

提示VARIABLES {city} = "Amsterdam"

PROMPT You are a travel guide. Tell me a fact about the city: {city}

输出阿姆斯特丹是一座美丽的城市,遍布运河、桥梁和狭窄的街道。这是一个因其丰富的历史、文化和夜生活而值得一游的好地方。

表 20. 在提示中使用变量

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