|
| 1 | +# Multi-Turn vs Single-Turn Agent Testing |
| 2 | + |
| 3 | +This example demonstrates the differences between multi-turn and single-turn agents in Contextual AI through comprehensive testing scenarios. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +The notebook creates two agents with identical configurations except for their multi-turn settings: |
| 8 | +- **MultiTurnAgent**: Maintains conversation context across multiple messages |
| 9 | +- **SingleTurnAgent**: Treats each message independently without memory |
| 10 | + |
| 11 | +## What You'll Learn |
| 12 | + |
| 13 | +- How to enable/disable multi-turn functionality in Contextual AI agents |
| 14 | +- The difference in behavior between multi-turn and single-turn agents |
| 15 | +- How to use `conversation_id` to continue previous conversations |
| 16 | +- Best practices for testing agent memory capabilities |
| 17 | + |
| 18 | +## Key Concepts |
| 19 | + |
| 20 | +### Multi-Turn Agents |
| 21 | +- Remember conversation history using `conversation_id` |
| 22 | +- Can reference previous messages and maintain context |
| 23 | +- Ideal for conversational interfaces and complex interactions |
| 24 | + |
| 25 | +### Single-Turn Agents |
| 26 | +- Process each message independently |
| 27 | +- No memory of previous interactions |
| 28 | +- Suitable for stateless operations and simple Q&A |
| 29 | + |
| 30 | +### Conversation Management |
| 31 | +- The first message in a conversation generates a `conversation_id` |
| 32 | +- Subsequent messages use this ID to maintain context |
| 33 | +- Conversations can be resumed later using the same ID |
| 34 | + |
| 35 | +## Test Methodology |
| 36 | + |
| 37 | +The notebook follows a systematic approach: |
| 38 | + |
| 39 | +1. **Setup**: Creates two identical agents with different multi-turn settings |
| 40 | +2. **Initial Facts**: Shares the same information with both agents |
| 41 | +3. **Memory Testing**: Asks follow-up questions to test retention |
| 42 | +4. **Comparison**: Validates expected vs actual behavior |
| 43 | +5. **Recurring Conversations**: Tests conversation continuity |
| 44 | + |
| 45 | +### Test Scenarios |
| 46 | + |
| 47 | +| Scenario | MultiTurnAgent | SingleTurnAgent | |
| 48 | +|----------|----------------|-----------------| |
| 49 | +| Initial message with facts | ✓ Acknowledges | ✓ Acknowledges | |
| 50 | +| Follow-up question about facts | ✓ Remembers | ✗ Forgets | |
| 51 | +| Complex recall request | ✓ Full context | ✗ No context | |
| 52 | +| Conversation resumption | ✓ Continues | N/A | |
| 53 | + |
| 54 | +## Running the Notebook |
| 55 | + |
| 56 | +### Prerequisites |
| 57 | +- Contextual AI API key |
| 58 | +- Python environment with required packages |
| 59 | + |
| 60 | +### Setup Steps |
| 61 | +1. Update the `API_KEY` variable with your Contextual AI API key |
| 62 | +2. Run all cells sequentially |
| 63 | +3. Review the test results summary at the end |
| 64 | + |
| 65 | +### Expected Outputs |
| 66 | +- Multi-turn agent should pass memory tests (remember color and cats) |
| 67 | +- Single-turn agent should fail memory tests (forget previous information) |
| 68 | +- All 5 tests should pass if multi-turn functionality works correctly |
| 69 | + |
| 70 | +## Configuration Details |
| 71 | + |
| 72 | +### Agent Configuration |
| 73 | +```python |
| 74 | +agent_configs = { |
| 75 | + "global_config": { |
| 76 | + "enable_multi_turn": True, # Key difference between agents |
| 77 | + "enable_filter": False, |
| 78 | + "enable_rerank": False, |
| 79 | + "should_check_retrieval_need": True, |
| 80 | + }, |
| 81 | + "reformulation_config": { |
| 82 | + "enable_query_expansion": False, |
| 83 | + "enable_query_decomposition": False, |
| 84 | + }, |
| 85 | + "generate_response_config": { |
| 86 | + "model": 'vertex_ai/gemini-2.0-flash-lite' |
| 87 | + } |
| 88 | +} |
| 89 | +``` |
| 90 | + |
| 91 | +### Query with Conversation ID |
| 92 | +```python |
| 93 | +response = client.agents.query.create( |
| 94 | + agent_id=agent.id, |
| 95 | + messages=[{"content": "Your message", "role": "user"}], |
| 96 | + conversation_id=conversation_id # Enables context retention |
| 97 | +) |
| 98 | +``` |
| 99 | + |
| 100 | +## Test Data |
| 101 | + |
| 102 | +The example uses simple, verifiable facts: |
| 103 | +- **Favorite color**: Blue |
| 104 | +- **Pet count**: 3 cats |
| 105 | +- **Pet names**: Whiskers, Mittens, Shadow |
| 106 | + |
| 107 | +These concrete values make it easy to verify whether agents remember information correctly. |
| 108 | + |
| 109 | +## Cleanup |
| 110 | + |
| 111 | +The notebook includes an optional cleanup section to delete the test agents and datastore after experimentation. Uncomment the cleanup code if you want to remove the created resources. |
| 112 | + |
| 113 | +## Use Cases |
| 114 | + |
| 115 | +### Multi-Turn Agents Are Ideal For: |
| 116 | +- Customer service chatbots |
| 117 | +- Interactive tutorials |
| 118 | +- Complex problem-solving sessions |
| 119 | +- Personalized conversations |
| 120 | + |
| 121 | +### Single-Turn Agents Are Ideal For: |
| 122 | +- FAQ systems |
| 123 | +- Simple classification tasks |
| 124 | +- Stateless API endpoints |
| 125 | +- One-off queries |
| 126 | + |
| 127 | +## Troubleshooting |
| 128 | + |
| 129 | +### Common Issues |
| 130 | +- **Agent doesn't remember**: Ensure you're using the correct `conversation_id` |
| 131 | +- **Tests fail**: Check API key validity and agent configuration |
| 132 | +- **Unexpected behavior**: Verify the `enable_multi_turn` setting |
| 133 | + |
| 134 | +### Debugging Tips |
| 135 | +- Monitor conversation IDs in the output |
| 136 | +- Check agent responses for context clues |
| 137 | +- Validate test data matches expected values |
| 138 | + |
| 139 | +## Next Steps |
| 140 | + |
| 141 | +After understanding multi-turn behavior, consider: |
| 142 | +- Implementing conversation persistence |
| 143 | +- Adding conversation branching |
| 144 | +- Building stateful applications |
| 145 | +- Exploring advanced context management |
| 146 | + |
| 147 | +--- |
| 148 | + |
| 149 | +**Note**: Remember to keep your API key secure and never commit it to version control. |
0 commit comments