A comprehensive travel recommendation system built with IBM Watson AI that provides real-time streaming responses for travel planning queries.
This project demonstrates how to integrate with IBM Watson AI to create an intelligent travel assistant that can help with:
- Destination recommendations
- Itinerary planning
- Travel tips and advice
- Local recommendations
- Budget-friendly suggestions
- Authentication Layer: IBM Cloud IAM OAuth 2.0 integration
- Streaming API Handler: Real-time response processing using Server-Sent Events (SSE)
- Error Management: Comprehensive error handling for production use
- Modular Design: Reusable functions for easy integration
- Deployment: Uses IBM Watson AI streaming endpoint
- Region: US-South (us-south.ml.cloud.ibm.com)
- Protocol: HTTPS with Bearer token authentication
- Response Format: Server-Sent Events (SSE) with JSON chunks
Travel_Agent_Ai/
βββ travel_ai_notebook.ipynb # Complete Jupyter notebook implementation
βββ arsh.env # Your API key file (create this)
βββ README.md # This file
File Descriptions:
- travel_ai_notebook.ipynb: Interactive notebook with step-by-step implementation and detailed explanations
- arsh.env: Your API key configuration file (you need to create this)
- Python 3.7+
- IBM Cloud account with Watson AI service
- Required Python packages:
requests,re,json
-
Clone or download the project files
git clone https://github.com/arshdeepyadavofficial/Travel_Agent_Ai cd Travel_Agent_Ai -
Install dependencies
pip install requests python-dotenv
-
Set up your IBM Cloud API key
Create a
.envfile in the project root directory and add your IBM Cloud API key:API_KEY=your_actual_ibm_cloud_api_key_here
How to get your IBM Cloud API key:
- Go to IBM Cloud Console
- Navigate to Manage β Access (IAM) β API Keys
- Click Create to generate a new API key
- Copy the key and paste it in your
.envfile
Important Notes:
- Never share your API key publicly
- Add
.envto your.gitignorefile - The key should be without quotes or extra spaces
- Set up your API key (see Configuration section above)
- Start Jupyter Notebook
jupyter notebook travel_ai_notebook.ipynb
- Run the cells in order - the notebook includes detailed explanations for each step
# Make sure your .env file is set up first
python travel_ai_notebook.py # If you convert the notebook to .py-
Sign up/Login to IBM Cloud
- Visit IBM Cloud
- Create an account or login
-
Create Watson AI Service
- Go to Catalog β AI/Machine Learning β Watson Studio
- Create a service instance
-
Generate API Key
- Navigate to Manage β Access (IAM) β API Keys
- Click Create button
- Give it a name (e.g., "Travel AI Assistant")
- Copy the generated API key
-
Create Environment File
- Create a
.envfile in your project directory - Add your API key:
API_KEY=your_actual_ibm_cloud_api_key_here
- Create a
Method 1: Custom Environment File (arsh.env)
API_KEY=your_ibm_cloud_api_key_hereMethod 2: System Environment Variables
export API_KEY="your_ibm_cloud_api_key_here"# The code automatically loads from .env or arsh.env files
# Your .env file should contain:
API_KEY=abcd1234-efgh-5678-ijkl-9012mnopqrst
# The deployment URL is already configured in the code- Real-time Streaming: See AI responses as they're generated
- Authentication: Secure IBM Cloud IAM integration
- Error Handling: Comprehensive error management
- Modular Design: Easy to integrate and extend
- Destination planning and recommendations
- Itinerary creation (multi-day trips)
- Travel tips and packing advice
- Local recommendations (restaurants, attractions)
- Budget-friendly travel options
- Server-Sent Events (SSE) parsing
- JSON response chunk assembly
- Token-based authentication
- Production-ready error handling
Once your API key is configured, you can use the travel assistant for various queries:
# Ask about destinations
travel_ai_assistant("I'm planning a trip to Paris. Can you suggest popular tourist attractions?")# Multi-day trip planning
travel_ai_assistant("Create a 3-day itinerary for Rome including must-see attractions")# Packing and preparation advice
travel_ai_assistant("What should I pack for a winter trip to Iceland?")# Local experiences and food
travel_ai_assistant("Best authentic street food in Bangkok?")- API Key: Your IBM Cloud API key
- Token Request: POST to IBM IAM endpoint
- Access Token: Temporary token (expires in 3600 seconds)
- Bearer Auth: Token used in Watson AI requests
# Authentication process
token_response = requests.post(IAM_URL, data={
"apikey": API_KEY,
"grant_type": 'urn:ibm:params:oauth:grant-type:apikey'
})
access_token = token_response.json()["access_token"]Watson AI returns streaming responses in SSE format:
id: 1
event: message
data: {"choices": [{"index": 0, "delta": {"role": "assistant", "content": "Hello"}}]}
id: 2
event: message
data: {"choices": [{"index": 0, "delta": {"role": "assistant", "content": " there"}}]}
The code parses these chunks and assembles the complete response.
# Extract data lines from SSE format
data_lines = re.findall(r'data: (.+)', response_text)
# Parse each JSON chunk
for line in data_lines:
data = json.loads(line)
content = data['choices'][0]['delta'].get('content', '')
full_response += contenterror_messages = {
400: "Bad Request - Check your message format",
401: "Unauthorized - Verify your API key",
403: "Forbidden - Check your permissions",
404: "Not Found - Verify deployment URL",
429: "Rate Limited - Wait before making more requests",
500: "Internal Server Error - Try again later",
503: "Service Unavailable - Watson AI may be down"
}-
Destination Planning
- Family vacation recommendations
- Beach and food destinations
- Seasonal travel timing
-
Itinerary Creation
- Multi-day trip planning
- Budget-friendly options
- City-specific itineraries
-
Travel Tips
- Packing recommendations
- Family travel advice
- Solo travel safety
-
Local Recommendations
- Authentic dining experiences
- Hidden gems and local culture
- Cultural experiences
Execute the notebook cells in order, or run:
# Test basic functionality
response = travel_ai_assistant("Plan a trip to Tokyo")
# Test multiple query types
test_multiple_queries()| Code | Issue | Solution |
|---|---|---|
| 401 | Authentication failed | Check API key validity |
| 404 | Deployment not found | Verify deployment URL |
| 429 | Rate limited | Wait and retry |
| 500 | Server error | Retry after delay |
-
"β API key is missing" Error
- Ensure your
.envorarsh.envfile exists in the project directory - Check that the file contains:
API_KEY=your_actual_key_here - Make sure there are no extra spaces around the equals sign
- Verify the API key is valid and not expired
- Ensure your
-
Authentication Failures
- Double-check your API key from IBM Cloud console
- Ensure the API key has proper permissions for Watson AI
- Try regenerating the API key if it's old
-
File Not Found Issues
- Make sure you're running the notebook from the correct directory
- Check that
.envorarsh.envfile is in the same folder as the notebook - Use absolute paths if needed
-
Network Issues
- Check internet connectivity
- Verify firewall settings allow HTTPS requests
- Try different network if corporate firewall blocks requests
-
Response Parsing Issues
- Check if your Watson deployment supports streaming
- Verify API version compatibility
- Ensure deployment URL is correct
- Store API keys in environment variables
- Use secrets management systems
- Implement request rate limiting
- Add input validation and sanitization
- Implement response caching
- Add connection pooling
- Use async requests for multiple queries
- Monitor token expiration
- Log API usage and errors
- Track response times
- Monitor token refresh cycles
- Set up alerting for failures
- Add conversation history support
- Implement token refresh mechanism
- Add response caching
- Create web interface (Flask/FastAPI)
- Multi-language support
- Image integration for destinations
- Travel booking integration
- Real-time pricing data
- Weather integration
- Map visualization
- Slack/Discord bot
- WhatsApp integration
- Voice assistant compatibility
- Mobile app backend
- Chrome extension
This project is open source. Feel free to use, modify, and distribute.
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
For issues and questions:
- Check the troubleshooting section
- Review IBM Watson AI documentation
- Create an issue in the repository
Arshdeep Yadav
BTech Computer Science Engineering
- IBM Watson AI team for the excellent API
- IBM Cloud documentation and examples
- Open source community for inspiration
Made with β€οΈ for travelers worldwide π