Preview
Open Original
π€ LangChain AI Agent
Intelligent Conversational AI with Multi-Tool Integration
π Tech Stack & Documentation
π Features
- Multi-tool Integration: Wikipedia search, web search, file saving, and time utilities
- Conversation Memory: Maintains chat history for contextual responses
- Extensible Architecture: Easy to add new tools and customize behavior
- Error Handling: Robust error handling and graceful degradation
- Configuration Management: Environment-based configuration
- Clean CLI Interface: User-friendly command-line interface
π Prerequisites
- Python 3.8+
- Google API Key (for Gemini models) or OpenAI API Key
π οΈ Installation
Clone the repository
git clone https://github.com/itanishqshelar/langchain-ai-agent...
π€ LangChain AI Agent
Intelligent Conversational AI with Multi-Tool Integration
π Tech Stack & Documentation
π Features
- Multi-tool Integration: Wikipedia search, web search, file saving, and time utilities
- Conversation Memory: Maintains chat history for contextual responses
- Extensible Architecture: Easy to add new tools and customize behavior
- Error Handling: Robust error handling and graceful degradation
- Configuration Management: Environment-based configuration
- Clean CLI Interface: User-friendly command-line interface
π Prerequisites
- Python 3.8+
- Google API Key (for Gemini models) or OpenAI API Key
π οΈ Installation
Clone the repository
git clone https://github.com/itanishqshelar/langchain-ai-agent.git
cd langchain-ai-agent
Create a virtual environment
python -m venv venv
# Windows
.\venv\Scripts\activate
# macOS/Linux
source venv/bin/activate
Install dependencies
pip install -r requirements.txt
Set up environment variables
Create a .env file in the project root:
# Required: At least one API key
GOOGLE_API_KEY=your_google_api_key_here
# OPENAI_API_KEY=your_openai_api_key_here
# Optional: Model Configuration
DEFAULT_MODEL=gemini-2.0-flash-exp
TEMPERATURE=0.7
MAX_TOKENS=2048
# Optional: Agent Settings
MAX_ITERATIONS=5
VERBOSE=True
MAX_HISTORY_LENGTH=10
# Optional: Storage
OUTPUT_DIR=outputs
π― Usage
Basic Usage
Run the chatbot:
python main.py
Available Commands
While chatting:
- Ask questions: Just type your question naturally
clear: Clear chat historyhistory: View conversation historyquitorexit: Exit the chatbot
Example Interactions
You: What is LangChain?
AI: [Searches Wikipedia and provides detailed explanation]
You: Search for the latest AI news
AI: [Uses web search to find current information]
You: Save that information to a file
AI: [Saves the previous response to a timestamped file]
You: What time is it?
AI: [Returns current date and time]
ποΈ Project Structure
langchain-ai-agent/
βββ main.py # Main chatbot application
βββ tools.py # Tool definitions and implementations
βββ config.py # Configuration settings
βββ requirements.txt # Python dependencies
βββ .env # Environment variables (create this)
βββ .gitignore # Git ignore rules
βββ README.md # This file
βββ outputs/ # Generated output files (auto-created)
π§ Customization
Adding New Tools
Define your tool function in tools.py:
def my_custom_tool(input: str) -> str:
"""Your tool implementation"""
return result
Create a Tool instance:
custom_tool = Tool(
name="MyTool",
func=my_custom_tool,
description="Description of what your tool does"
)
Add to the tools list:
all_tools = [wiki_tool, search_tool, save_tool, time_tool, custom_tool]
Changing the LLM Model
Edit .env:
# For Google Gemini
DEFAULT_MODEL=gemini-2.0-flash-exp
# For OpenAI (requires OPENAI_API_KEY)
# DEFAULT_MODEL=gpt-4-turbo-preview
Update main.py to use OpenAI:
from langchain_openai import ChatOpenAI
# In ChatBot.__init__:
self.llm = ChatOpenAI(
model=model_name,
temperature=temperature
)
Adjusting Response Style
Modify the system prompt in main.py:
prompt = ChatPromptTemplate.from_messages([
(
"system",
"""Your custom system prompt here..."""
),
# ... rest of the template
])
π Available Tools
| Tool | Description | Input Example |
|---|---|---|
| Wikipedia | Search Wikipedia for detailed information | βPython programming languageβ |
| WebSearch | Search the web for current information | βlatest AI developments 2024β |
| SaveToFile | Save content to a text file | βcontent to saveβ |
| CurrentTime | Get current date and time | (no input needed) |
π§ͺ Testing
Run a quick test:
python -c "from main import ChatBot; bot = ChatBot(); print(bot.chat('Hello!'))"
π Security Best Practices
- Never commit
.envfiles to version control - Rotate API keys regularly
- Use environment variables for all sensitive data
- Limit tool permissions based on your use case
- Monitor API usage to avoid unexpected costs
π Configuration Options
Edit .env to customize:
| Variable | Default | Description |
|---|---|---|
GOOGLE_API_KEY | - | Google Gemini API key (required) |
OPENAI_API_KEY | - | OpenAI API key (optional) |
DEFAULT_MODEL | gemini-2.0-flash-exp | LLM model to use |
TEMPERATURE | 0.7 | Response creativity (0.0-1.0) |
MAX_TOKENS | 2048 | Maximum response length |
MAX_ITERATIONS | 5 | Max tool calling iterations |
VERBOSE | True | Show detailed agent logs |
MAX_HISTORY_LENGTH | 10 | Chat history size |
OUTPUT_DIR | outputs | Directory for saved files |
π€ Contributing
Contributions are welcome! Please:
- Fork the repository
- Create a feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
π License
This project is licensed under the MIT License - see the LICENSE file for details.
π Troubleshooting
βNo API key foundβ
- Ensure
.envfile exists and contains valid API keys - Check that
python-dotenvis installed
βModule not foundβ errors
- Run
pip install -r requirements.txt - Ensure virtual environment is activated
Tool execution errors
- Check internet connection (required for Wikipedia and web search)
- Verify API rate limits havenβt been exceeded
Import errors
- Ensure all dependencies are up to date:
pip install --upgrade -r requirements.txt
π Resources
π§ Support
For issues and questions:
- Open an issue on GitHub
- Check existing issues for solutions
Made with β€οΈ using LangChain