Creating a Lightweight Multi-Tool AI Agent with Hugging Face Transformers
This tutorial shows how to create a compact AI agent that integrates multiple NLP tools using Hugging Face models, enabling tasks like chat, sentiment analysis, and calculations in one package.
Setting Up the Environment and Libraries
This tutorial guides you through building a compact yet powerful AI agent that integrates multiple natural language processing capabilities using Hugging Face transformers. We begin by installing essential Python libraries including transformers, torch, accelerate, datasets, requests, and beautifulsoup4 to handle model loading, inference, and web scraping efficiently in a Colab environment.
!pip install transformers torch accelerate datasets requests beautifulsoup4
import torch
import json
import requests
from datetime import datetime
from transformers import (
AutoTokenizer, AutoModelForCausalLM, AutoModelForSequenceClassification,
AutoModelForQuestionAnswering, pipeline
)
from bs4 import BeautifulSoup
import warnings
warnings.filterwarnings('ignore')The AdvancedAIAgent Class
We encapsulate all functionalities in an AdvancedAIAgent Python class that initializes on GPU if available and loads multiple lightweight models for dialogue generation, sentiment analysis, and question answering. The agent also supports helper tools such as web search stubs, a safe calculator, and mock weather lookups.
class AdvancedAIAgent:
def __init__(self):
"""Initialize the AI Agent with multiple models and capabilities"""
self.device = "cuda" if torch.cuda.is_available() else "cpu"
print(f" Initializing AI Agent on {self.device}")
self._load_models()
self.tools = {
"web_search": self.web_search,
"calculator": self.calculator,
"weather": self.get_weather,
"sentiment": self.analyze_sentiment
}
print(" AI Agent initialized successfully!")
def _load_models(self):
"""Load all required models"""
print(" Loading models...")
self.gen_tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-medium")
self.gen_model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-medium")
self.gen_tokenizer.pad_token = self.gen_tokenizer.eos_token
self.sentiment_pipeline = pipeline(
"sentiment-analysis",
model="cardiffnlp/twitter-roberta-base-sentiment-latest",
device=0 if self.device == "cuda" else -1
)
self.qa_pipeline = pipeline(
"question-answering",
model="distilbert-base-cased-distilled-squad",
device=0 if self.device == "cuda" else -1
)
print(" All models loaded!")Key Features and Capabilities
The agent supports several functions:
- Text generation: Generates conversational replies using DialoGPT.
- Sentiment analysis: Uses a RoBERTa-based model for sentiment classification.
- Question answering: Answers questions based on provided context using DistilBERT.
- Web search simulation: Returns stubbed search results.
- Safe calculator: Evaluates simple arithmetic expressions safely.
- Mock weather lookup: Provides example weather data.
Intent detection routes user inputs dynamically to appropriate tools or falls back to text generation.
Example Usage
The demo runs several test queries showcasing the agent’s multi-tool capabilities:
if __name__ == "__main__":
agent = AdvancedAIAgent()
print("\n" + "="*50)
print(" DEMO: Advanced AI Agent Capabilities")
print("="*50)
test_cases = [
"Calculate 25 * 4 + 10",
"What's the weather in Tokyo?",
"Search for latest AI developments",
"Analyze sentiment of: I love working with AI!",
"Hello, how are you today?"
]
for test in test_cases:
print(f"\n User: {test}")
result = agent.process_request(test)
print(f" Agent: {json.dumps(result, indent=2)}")This approach demonstrates how to combine multiple NLP models and tools into a cohesive, extensible AI agent that works efficiently within resource constraints like those of Colab notebooks. The modular design allows easy extension and integration with real APIs for weather or search if desired.
Сменить язык
Читать эту статью на русском