few-shot-examples.mdx 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. ---
  2. title: "Few-Shot Examples"
  3. description: "Guide agent responses using few-shot prompting."
  4. icon: "clone"
  5. ---
  6. **Few-shot prompting** is a powerful technique where you provide a small number of sample interactions (typically 2 to 5) to guide your agent's behavior. This method helps the agent understand the desired output format and task requirements by learning from the given examples, thereby improving performance without writing extensive instructions.
  7. ## Crafting Effective Examples
  8. - **Provide Task Demonstrations**: Use examples that clearly illustrate the tasks that your agents will perform.
  9. - **Use Realistic Scenarios**: Include interactions that mirror actual conversations that your agent will handle.
  10. - **Use Preferred Tone and Style**: Ensure the agent's replies in your examples match your desired brand voice.
  11. ## Defining Few-Shot Examples
  12. In **Agency Swarm**, few-shot examples are earlier messages that show the style you want. Put them in `instructions` as plain text or pass them as message history to `get_response` / `get_response_stream`.
  13. Optional fields like `attachments` and `metadata` can be included in message history but are not required for basic examples.
  14. ## Using Few-Shot Examples
  15. Pick one of these ways:
  16. <Tabs>
  17. <Tab title="Message History (Scalable)">
  18. ```python
  19. from agency_swarm import Agency, Agent
  20. support_agent = Agent(
  21. name="CustomerSupportAgent",
  22. instructions="You assist customers with troubleshooting.",
  23. )
  24. agency = Agency(support_agent)
  25. examples = [
  26. {"role": "user", "content": "My device won't turn on."},
  27. {"role": "assistant", "content": "Please hold the power button for 10 seconds to restart the device."},
  28. ]
  29. new_message = {"role": "user", "content": "It still won't start."}
  30. response = await agency.get_response(examples + [new_message])
  31. ```
  32. </Tab>
  33. <Tab title="Instructions (Simple)">
  34. ```python
  35. from agency_swarm import Agent
  36. instructions = """
  37. You assist customers with troubleshooting.
  38. Example:
  39. User: My device won't turn on.
  40. Assistant: Please hold the power button for 10 seconds to restart the device.
  41. """
  42. agent = Agent(
  43. name="CustomerSupportAgent",
  44. instructions=instructions,
  45. )
  46. ```
  47. </Tab>
  48. </Tabs>
  49. Message history is more scalable. Instructions are best for short examples.
  50. See more advanced features in [Agent Class](/core-framework/agents/advanced-configuration)