Skip to content

bhanuprakashtalluri/Agent

Repository files navigation

A production-ready AI agent built with LangChain, LangGraph, and Ollama that can search the web, query databases, manage emails, and automatically log all interactions.

โœจ Features

This AI agent includes:

  1. ๐Ÿ” Web Search & Scraping - DuckDuckGo search + web content extraction
  2. ๐Ÿ‘ฅ Customer Database - SQLite database with customer and order data
  3. ๐Ÿ“ง Gmail Integration - Search, read, and send emails
  4. ๐Ÿ“š RAG Document Search - Upload and search PDFs, Excel, CSV, Word docs
  5. ๐Ÿ“Š Excel Logging - Automatic logging of all interactions
  6. ๐Ÿ’ฌ Chat Interface - Clean Streamlit UI with conversation history
  7. ๐Ÿ”’ Privacy First - Runs locally with Ollama (no cloud API costs)

๐Ÿ“ฆ Installation

๐Ÿš€ Quick Start

1. Install Python Dependencies

1. Install Ollama (Local AI)

bashbash

macOSpip install -r requirements.txt

brew install ollama```

Or download from https://ollama.ai### 2. Set up Gmail API (Optional but recommended)


To use Gmail features, you need to set up OAuth2 credentials:

### 2. Start Ollama & Pull Model

```bash#### Step 1: Create Google Cloud Project

# Terminal 1 - Start service (keep running)1. Go to [Google Cloud Console](https://console.cloud.google.com/)

ollama serve2. Create a new project or select existing one

3. Enable the Gmail API:

# Terminal 2 - Pull model   - Navigate to "APIs & Services" > "Library"

ollama pull llama3.2:1b   - Search for "Gmail API"

```   - Click "Enable"


### 3. Install Dependencies#### Step 2: Create OAuth2 Credentials

```bash1. Go to "APIs & Services" > "Credentials"

pip install -r requirements.txt2. Click "Create Credentials" > "OAuth client ID"

```3. Configure consent screen if prompted:

   - User Type: External

### 4. Setup Database   - App name: "AI Agent"

```bash   - Add your email as test user

python setup_database.py4. Application type: "Desktop app"

```5. Download the credentials JSON file

6. Save it as `gmail_credentials.json` in the project root

### 5. Run the App

```bash#### Step 3: First-time Authentication

streamlit run app.pyWhen you first use Gmail features, the agent will:

```1. Open a browser window for authentication

2. Ask you to login with your Google account

Visit http://localhost:8501 in your browser.3. Save the token to `gmail_token.json` for future use



## ๐Ÿ“ Project Structure**Note:** If you skip Gmail setup, the agent will work fine without email features.



```## ๐Ÿš€ Usage

agent/

โ”œโ”€โ”€ app.py                    # Streamlit UI### Run the Streamlit App

โ”œโ”€โ”€ agent_complete.py         # Main agent with all tools

โ”œโ”€โ”€ setup_database.py         # Database initialization```bash

โ”œโ”€โ”€ requirements.txt          # Python dependenciesstreamlit run app.py

โ”‚```

โ”œโ”€โ”€ config/                   # Configuration files

โ”‚   โ”œโ”€โ”€ gmail_credentials.json   # Gmail OAuth (create this)### Choose Your Agent Type

โ”‚   โ””โ”€โ”€ gmail_token.json         # Auto-generated token

โ”‚The app offers three agent configurations:

โ”œโ”€โ”€ data/                     # Data storage

โ”‚   โ”œโ”€โ”€ customers.db             # SQLite database1. **Complete Agent (All Tools)** - Recommended

โ”‚   โ””โ”€โ”€ agent_logs.xlsx          # Interaction logs   - All features enabled

โ”‚   - Automatic Excel logging

โ”œโ”€โ”€ tools/                    # Tool modules   - Filesystem caching

โ”‚   โ”œโ”€โ”€ visit_web.py             # Web scraping   - Gmail integration

โ”‚   โ”œโ”€โ”€ query_database.py        # Database queries

โ”‚   โ”œโ”€โ”€ gmail_tools.py           # Email operations2. **Full Agent (Web + Database)**

โ”‚   โ””โ”€โ”€ excel_logger.py          # Logging utility   - Web search and scraping

โ”‚   - Customer database access

โ”œโ”€โ”€ docs/                     # Documentation   - No logging or Gmail

โ”‚   โ”œโ”€โ”€ PROJECT_EXPLANATION.md   # Detailed technical docs

โ”‚   โ””โ”€โ”€ OLLAMA_SETUP.md          # Ollama setup guide3. **Web Agent Only**

โ”‚   - Just web search and scraping

โ”œโ”€โ”€ versions/                 # Previous agent versions   - Minimal features

โ”‚   โ”œโ”€โ”€ agent.py                 # Basic version

โ”‚   โ”œโ”€โ”€ agent2.py                # Web-only agent## ๐Ÿ“‚ Project Structure

โ”‚   โ””โ”€โ”€ agent3.py                # Web + Database agent

โ”‚```

โ””โ”€โ”€ cache/                    # Cached responsesagent/

```โ”œโ”€โ”€ app.py                      # Streamlit UI

โ”œโ”€โ”€ agent2.py                   # Web-only agent

## ๐Ÿ› ๏ธ Tools Availableโ”œโ”€โ”€ agent3.py                   # Web + Database agent

โ”œโ”€โ”€ agent_complete.py           # Complete agent with all tools

### Web Toolsโ”œโ”€โ”€ requirements.txt            # Python dependencies

- **DuckDuckGo Search** - Find information on the webโ”œโ”€โ”€ customers.db                # SQLite database

- **Web Scraper** - Extract and read website contentโ”œโ”€โ”€ agent_logs.xlsx            # Excel log (auto-created)

โ”œโ”€โ”€ gmail_credentials.json      # Gmail OAuth (you create this)

### Database Toolsโ”œโ”€โ”€ gmail_token.json           # Gmail token (auto-created)

- **Query Customer Info** - Search customers by nameโ”œโ”€โ”€ cache/                     # Cached markdown files (auto-created)

- **Query Orders** - Get order history for customersโ”‚   โ””โ”€โ”€ *.md

- **List All Customers** - View all customers with statusโ””โ”€โ”€ tools/

    โ”œโ”€โ”€ visit_web.py           # Web scraping tool

### Gmail Tools (Optional)    โ”œโ”€โ”€ query_database.py      # Database query tools

- **Search Emails** - Use Gmail syntax to find emails    โ”œโ”€โ”€ filesystem_cache.py    # Cache management

- **Read Email** - Get full email content    โ”œโ”€โ”€ gmail_tools.py         # Gmail integration

- **Send Email** - Send emails to recipients    โ””โ”€โ”€ excel_logger.py        # Excel logging utility

๐Ÿ“ Example Queries

๐Ÿ’ก Example Queries

Customer Service


"Show me John Doe's information"- "List all customers"

"What orders does Jane Smith have?"- "Show me John Doe's information"

"List all active customers"- "What orders does Jane Smith have?"

"What's Alice Brown's account balance?"- "What's Alice Brown's account balance?"

Web Search

Web Research- "What's the latest news about Python?"


"Search for latest Python news"- "Find information about LangChain"

"Find tutorials on LangChain"

"What's new in AI this week?"### Gmail (if configured)

```- "Search for emails from john@example.com"

- "Find unread emails from today"

### Email Operations- "Search for emails with subject containing 'invoice'"

"Search for emails from john@example.com"### File Management

"Find unread emails from today"- "Save this response to cache"

"Read email with ID abc123"- "List all cached files"




### Document Search (RAG)

"What documents have been uploaded?" "Search my documents for Python tutorials" "Find sales data in my Excel files" "What does the contract say about payments?"


### Mixed Queries

"Look up customer John Doe and email me his order history" "Search for CRM best practices and show me our top customers" "Search my documents about AI and also search the web for latest news"


## ๐Ÿ“š RAG Document Search

**NEW: Upload and search your documents!**

Supported file types:
- PDF documents (.pdf)
- Excel spreadsheets (.xlsx, .xls)
- CSV files (.csv)
- Word documents (.docx)
- Text files (.txt, .md)
- JSON files (.json)

**Quick Start:**
```bash
# Upload a document
python upload_documents.py upload document.pdf

# Upload entire folder
python upload_documents.py upload-dir RAG/documents/

# List uploaded documents
python upload_documents.py list

Example Queries:

  • "What documents are in the system?"
  • "Search my documents for Python information"
  • "Find quarterly sales in my Excel files"

See full documentation: docs/RAG_GUIDE.md

๐Ÿ“Š Excel Logging

๐Ÿ” Gmail Setup (Optional)All interactions are automatically logged to agent_logs.xlsx with:

  • Timestamp

If you want email features:- User input

  • Tools used
  1. Create Google Cloud Project- Sources/URLs visited

View the log summary in the sidebar of the Streamlit app.

  1. Create OAuth Credentials

    • Navigate to APIs & Services โ†’ Credentials## ๐Ÿ—„๏ธ Database Schema

    • Create OAuth 2.0 Client ID (Desktop app)

    • Download credentials JSON### Customers Table

  • id (PRIMARY KEY)
  1. Save Credentials- name

    
    # Save downloaded file as:- phone
    
    config/gmail_credentials.json- status
    
    ```- account_balance
    
    
  • join_date
  1. First Run Authentication

    • Agent will open browser for authentication### Orders Table

    • Grant permissions- id (PRIMARY KEY)

    • Token saved automatically to config/gmail_token.json- customer_id (FOREIGN KEY)

  • order_number

Note: The agent works perfectly fine without Gmail setup. Email features will simply be unavailable.- product

  • amount

๐Ÿ’พ Database Schema- status

  • order_date

Customers Table

  • id - Primary key## ๐Ÿ”ง Troubleshooting

  • name - Customer name

  • email - Email address### Gmail Not Working

  • phone - Phone number- Make sure gmail_credentials.json exists

  • status - Active/Inactive- Check that Gmail API is enabled in Google Cloud Console

  • account_balance - Account balance- Delete gmail_token.json and re-authenticate if issues persist

  • join_date - Join date

Excel Logging Errors

Orders Table- Ensure openpyxl is installed: pip install openpyxl

  • id - Primary key- Check write permissions in the project directory

  • customer_id - Foreign key to customers

  • order_number - Order number### Cache Directory Issues

  • product - Product name- The cache/ directory is auto-created

  • amount - Order amount- Check write permissions

  • status - Delivered/Shipped/Processing

  • order_date - Order date## ๐ŸŽจ Customization

๐Ÿ“Š Excel Logging### Add More Customers

Edit setup_database.py and run:

All interactions are automatically logged to data/agent_logs.xlsx:```bash

python3 setup_database.py

Columns:```

  • Timestamp

  • User Input### Modify Agent Behavior

  • Tools UsedEdit the system message in agent_complete.py to change how the agent uses tools.

  • Sources/URLs

  • Output### Add New Tools

  • Status (Success/Error)1. Create a new tool in tools/ directory

  1. Import it in agent_complete.py

View summary in the sidebar or open the Excel file directly.3. Add to the tools list

๐ŸŽฏ Technology Stack## ๐Ÿ“ Notes

| Technology | Purpose |- The agent uses Groq's LLaMA model (configured in .env)

|------------|---------|- Memory is session-based using MemorySaver

| LangChain | LLM application framework |- Excel logs persist across sessions

| LangGraph | Agent orchestration & state management |- Cache files are stored indefinitely (manage manually if needed)

| Ollama | Local LLM (llama3.2:1b) | | Streamlit | Web UI framework | | SQLite | Embedded database | | Gmail API | Email integration | | OpenPyXL | Excel file operations | | DuckDuckGo | Web search | | Markdownify | HTML to Markdown conversion |

๐Ÿ”ง Configuration

Change AI Model

Edit agent_complete.py:

# Use different Ollama model
model = ChatOllama(model="llama3.1", temperature=0.6)

# Available models: llama3.2:1b, llama3.1, mistral, codellama

Adjust Agent Behavior

Modify the system message in agent_complete.py to change how the agent uses tools.

Add Custom Tools

  1. Create new tool in tools/ directory
  2. Import in agent_complete.py
  3. Add to tools list

๐Ÿ› Troubleshooting

Ollama Connection Error

# Make sure Ollama is running
ollama serve

# Check if running
ps aux | grep ollama

Model Not Found

# Pull the model
ollama pull llama3.2:1b

# List installed models
ollama list

Database Not Found

# Recreate database
python setup_database.py

Gmail Authentication Failed

# Delete token and re-authenticate
rm config/gmail_token.json

# Ensure credentials file exists
ls config/gmail_credentials.json

Slow Performance

# Use smaller/faster model
ollama pull llama3.2:1b  # Fastest

# Or larger/better model
ollama pull llama3.1     # More capable but slower

๐Ÿ“š Documentation

๐ŸŽ“ Key Concepts

Agent Architecture

The agent uses LangGraph to create a stateful AI that can:

  • Decide which tools to use based on user queries
  • Maintain conversation history
  • Chain multiple tool calls together
  • Synthesize results into coherent responses

Tool Pattern

Each capability is a decorated function:

@tool
def query_customer_info(name: str) -> str:
    """Search customer by name"""
    # Implementation
    return result

Memory & State

memory = MemorySaver()  # Persistent conversation memory
config = {"configurable": {"thread_id": "abc123"}}

๐ŸŒŸ Advantages

โœ… Cost Effective - No API costs, run locally
โœ… Privacy - Data stays on your machine
โœ… Unlimited Usage - No rate limits
โœ… Fast - No network latency for AI
โœ… Modular - Easy to add/remove tools
โœ… Observable - Complete audit trail in Excel
โœ… Production Ready - Error handling & logging

๐Ÿ”ฎ Future Enhancements

  • File upload and analysis
  • Export conversations to PDF
  • Multi-user support with authentication
  • API endpoints for programmatic access
  • Analytics dashboard
  • Custom report generation
  • Integration with more services (Slack, Discord, etc.)

Intent Detection & Tool Distinction

  • Integrate NLP libraries (spaCy, NLTK, transformers) for intent classification
  • Implement rule-based and context-aware parsing for tool selection
  • Add context tracking and conversation history for better intent resolution
  • Use confidence scoring and fallback to clarification when intent is ambiguous
  • Leverage LLMs for intent extraction and tool routing
  • Expand and refine training data for intent mapping

๐Ÿ“„ License

MIT License - Feel free to use and modify for your needs.

๐Ÿค Contributing

Contributions welcome! Feel free to:

  • Add new tools
  • Improve documentation
  • Report bugs
  • Suggest features

Need Help?

Built with โค๏ธ using LangChain, LangGraph, and Ollama

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages