A scalable FastAPI application that serves as a chatbot to process user inquiries about the weather. It leverages Langchain SDK for natural language processing and interacts with the OpenWeather API for fetching up-to-date weather data.
- Chat Endpoint: Allows users to interact via
/api/chat
, delivering weather information based on user queries. - Weather-related Queries: Supports current weather, forecast, and historical data inquiries.
- Integration with External APIs: Utilizes OpenWeather API for accurate weather information.
- Python 3.8+
- Poetry for dependency management (optional)
- Docker (for containerization, optional)
-
Clone the repository
git clone https://github.com/yourusername/weather-chatbot-api.git cd weather-chatbot-api
-
Create a virtual environment
python -m venv venv source venv/bin/activate # On Windows use `venv\Scripts\activate`
-
Install dependencies
pip install -r requirements.txt
-
Set environment variables
Create a
.env
file with necessary configurations:OPENWEATHER_API_KEY=your_openweather_api_key OPENCAGE_API_KEY=your_opencage_api_key LANGSMITH_TRACING=true LANGSMITH_ENDPOINT=https://api.smith.langchain.com LANGSMITH_API_KEY=your_langsmith_api_key LANGSMITH_PROJECT=your_langsmith_project_name OPENAI_API_KEY=your_openai_api_key CURRENT_WEATHER_URL=current_weather_url FORECAST_WEATHER_URL=forecast_weather_url HISTORY_WEATHER_URL=history_weather_url
-
Run the application
uvicorn main:app --reload
The API will be accessible at
http://127.0.0.1:8000
.
-
POST
/api/chat
Takes user input and returns the processed query result related to weather information.
Request Example:
{ "user_query": "What is the weather like in New York today?" }
Response Example:
{ "What is the weather like in New York today?": "The current weather in New York is clear sky with a temperature of 18°C." }
- Ensure Docker is installed and running on your machine.
-
Build Docker Image
Before building the image, ensure your
Dockerfile
is configured as described in the previous section.docker build -t weather-chatbot-api .
This command builds and tags your Docker image as
weather-chatbot-api
. -
Run the Docker Container
You need to pass the environment variables defined in your
.env
file to the container. For that, use the--env-file
option in thedocker run
command. Make sure your.env
file is located in the same directory where you're running the command or provide the specific path to it.docker run -d --name weather-chatbot-api-container \ --env-file .env \ -p 8000:8000 \ weather-chatbot-api
This command runs the Docker container in detached mode (
-d
), sets the container name toweather-chatbot-api-container
, exposes port 8000, and passes the environment variables. -
Access the API
Once the container is running, your FastAPI application will be accessible at
http://localhost:8000
. -
Stopping and Removing Containers
To stop the running container, use:
docker stop weather-chatbot-api-container
To remove the stopped container, use:
docker rm weather-chatbot-api-container
-
Debugging: If you encounter issues, check the logs of the container for debugging using:
docker logs weather-chatbot-api-container
-
Environment Variables: It's crucial to manage your sensitive environment variables (like API keys) securely. Consider using Docker Secrets or similar tools for production environments.
By following these steps, you can easily build and deploy this Weather Chatbot API within a Docker container, which simplifies deployment and scaling in different environments.
Contributions are welcome! Please fork the repository and submit a pull request for review.
This project is licensed under the MIT License. See the LICENSE file for more information.
- Replace placeholder values such as
yourusername
oryour_openweather_api_key
with actual values where applicable. - Ensure that any additional setup steps specific to your development environment (like database initialization) are documented accordingly.