🔌 Lesson 9.2: Connecting Plugins and Tools

 

🔌 Lesson 9.2: Connecting Plugins and Tools

Module 9: Tools, Plugins, and Deployment
Course: Build Your Own AI Chat System (Like ChatGPT or DeepSeek) Using Free Open-Source Tools


🎯 Lesson Objective:

By the end of this lesson, learners will know how to connect external tools like Google Sheets, email, and third-party APIs to extend their chatbot’s functionality beyond just answering questions.


🤖 Why Add Plugins or Tool Integrations?

A basic chatbot can answer questions from documents.
But a powerful assistant can also:

  • Pull real-time data from a spreadsheet

  • Send or summarize emails

  • Use third-party APIs (e.g., weather, currency exchange)

  • Perform actions (e.g., task creation, reminders)


🛠 Plugin Types & What They Can Do

Plugin/Tool Purpose Example Use
Google Sheets API Read or write spreadsheet data Show live pricing, update orders
Email Tools (SMTP + IMAP) Read or send email Draft replies, summarize inbox
Third-party APIs (REST) Pull data from external sources Get weather, translate text, check currency rates
Zapier / Make.com (optional) Connect apps easily (no-code) Trigger actions in other tools
Function Calling Trigger custom Python functions Calculate prices, update databases

🔗 How It All Connects

User Query
   ↓
LLM (via Langchain or Flowise)
   ↓
If API needed → trigger plugin (Google Sheets / API / Email)
   ↓
Plugin returns result
   ↓
LLM formats final answer
   ↓
Displayed to user

📁 Example 1: Connect to Google Sheets (Pricing Bot)

Use Case:

User asks: “What’s the price for the gold logo package?”

Steps (Langchain):

  1. Create a sheet: pricing_data.csv

  2. Use Python to read and search rows:

import pandas as pd

def get_price(service_name):
    df = pd.read_csv("pricing_data.csv")
    row = df[df['Service'].str.lower() == service_name.lower()]
    return row['Price'].values[0] if not row.empty else "Service not found"
  1. Plug this into Langchain using a Tool:

from langchain.agents import Tool

price_tool = Tool(
    name="ServicePricing",
    func=lambda q: get_price(q),
    description="Use this to find the price of a service"
)
  1. Combine with agent:

from langchain.agents import initialize_agent

agent = initialize_agent(
    tools=[price_tool],
    llm=llm,
    agent="zero-shot-react-description"
)

📧 Example 2: Read & Summarize Emails (IMAP)

Use IMAP + Langchain Tool to summarize inbox:

import imaplib
import email

def get_latest_email_summary():
    mail = imaplib.IMAP4_SSL("imap.gmail.com")
    mail.login("[email protected]", "yourpassword")
    mail.select("inbox")
    _, data = mail.search(None, "ALL")
    latest_email_id = data[0].split()[-1]
    _, msg_data = mail.fetch(latest_email_id, "(RFC822)")
    msg = email.message_from_bytes(msg_data[0][1])
    return f"From: {msg['From']}, Subject: {msg['Subject']}"

Then wrap it as a tool and plug into the same LLM agent system.


🌐 Example 3: Call External APIs (e.g., Weather)

import requests

def get_weather(city):
    url = f"https://wttr.in/{city}?format=3"
    return requests.get(url).text

Use like before: wrap in a Tool → Add to Agent → Allow LLM to decide when to call


🧪 Try It Yourself Activity

Task:
Build a chatbot that:

  1. Checks service prices from a spreadsheet

  2. Can call an API to check today’s weather

  3. Has a function to send you a summary of your last email

Bonus:
Wrap all 3 as tools, and let the LLM choose which one to use based on the question.


💡 Tips & Best Practices

  • Secure all API keys and credentials!

  • Don’t overload your bot—use tools for essential tasks only

  • Always log tool outputs for debugging

  • Limit number of external calls (use caching)


🧠 Recap:

You’ve now learned to:

  • Extend your chatbot with real-time capabilities

  • Use tools to access Google Sheets, Emails, and APIs

  • Use function-calling patterns in Langchain/Flowise

  • Build a smart agent that can act, not just chat


 

67