ChatGPT plugins enhance the chatbot experience by providing the underlying language model with recent, personal, or specific data that was not included in the model’s training data. The connection of the LLM to real-time data sources, like databases or web browsing, is aimed at helping ChatGPT produce more accurate and timely results. It also allows the chatbot to perform more complex tasks, like responding to an email, directly within the ChatGPT interface. Let’s break down all this added functionality into different categories.
Step-by-Step Guide to Creating the Plugin with Flask
Step 1: Environment Setup
(I) Install Flask and Requests:
pip install Flask requests
(II) Create Project Directory:
mkdir weather-plugin
cd weather-plugin
Step 2: Plugin Structure
- Create a Flask Application:
- Create a file named
app.py
.
- Create a file named
- Define the Plugin Structure:
- In
app.py
, set up the Flask application and create routes.py to handle weather requests.
- In
- Project structure:
your_project_directory/
│
├── app.py
├── routes.py
├── ai-plugin.json
├── openapi.yaml
Step 3: Plugin Implementation
- Implement Weather API Integration:
- Use a weather API service like OpenWeatherMap. Obtain an API key for accessing weather data.
app.py
from flask import Flask
app = Flask(__name__)
# Import the routes from routes.py
from routes import *
if __name__ == '__main__':
app.run(debug=True)
routes.py
from flask import request, jsonify, send_from_directory
import requests
from app import app # Import the app instance from app.py
WEATHER_API_KEY = 'YOUR_WEATHER_API_KEY'
WEATHER_API_URL = 'http://api.weatherapi.com/v1'
def get_current_weather(city):
url = f"{WEATHER_API_URL}/current.json?key={WEATHER_API_KEY}&q={city}"
response = requests.get(url)
data = response.json()
return data
def get_weather_forecast(city):
url = f"{WEATHER_API_URL}/forecast.json?key={WEATHER_API_KEY}&q={city}&days=3"
response = requests.get(url)
data = response.json()
return data
@app.route('/openapi.yaml')
def openapi_spec():
return send_from_directory('.', 'openapi.yaml')
@app.route('/ai-plugin.json')
def plugin_manifest():
return send_from_directory('.', 'ai-plugin.json')
@app.route('/weather', methods=['POST'])
def weather_handler():
data = request.json
query = data.get('input')
if 'current weather' in query.lower():
city = extract_city_name(query)
weather_data = get_current_weather(city)
response = f"Current weather in {city}: {weather_data['current']['temp_c']}°C, {weather_data['current']['condition']['text']}"
elif 'forecast' in query.lower():
city = extract_city_name(query)
forecast_data = get_weather_forecast(city)
forecast_text = "\n".join(
[f"{day['date']}: {day['day']['condition']['text']}, High: {day['day']['maxtemp_c']}°C, Low: {day['day']['mintemp_c']}°C"
for day in forecast_data['forecast']['forecastday']]
)
response = f"Weather forecast for {city}:\n{forecast_text}"
else:
response = "I'm sorry, I couldn't understand your weather request."
return jsonify({"output": response})
def extract_city_name(query):
words = query.split()
city = words[-1] # This is a simplistic approach, adjust as needed
return city
Run the Application
- Navigate to your project directory: Open Command Prompt or Terminal and navigate to your project directory:
cd path/to/your_project
2. Run the Flask application:
python app.py
3. Access the application:
Open your web browser and go to http://127.0.0.1:5000/
. This will open the Flask application running locally.
Using Postman
- Open Postman.
- Set the request type to POST.
- Enter the URL:
http://127.0.0.1:5000/weather
. - Go to the Body tab, select raw, and choose JSON from the dropdown.
- Enter the JSON data in the request body:json
{
"input": "current weather in Mumbai"
}
6. Click Send.
Step 4: Integration with ChatGPT
(I) Create a Plugin Manifest: Define your plugin settings and capabilities in a manifest file in your flask project directory. Create a ai-plugin.json
file that describes your plugin:
{
"schema_version": "v1",
"name_for_human": "Weather Plugin",
"name_for_model": "weather_plugin",
"description_for_human": "Get current weather and forecasts.",
"description_for_model": "Provides current weather and forecast information.",
"auth": {
"type": "none"
},
"api": {
"type": "openapi",
"url": "https://yourdomain.com/openapi.yaml",
"is_user_authenticated": false
},
"logo_url": "https://yourdomain.com/logo.png",
"contact_email": "support@yourdomain.com",
"legal_info_url": "https://yourdomain.com/legal"
}
(II) Create an OpenAPI Specification: Describe your API endpoints.
Create an openapi.yaml
file that describes your API endpoints:
openapi: 3.0.0
info:
title: Weather Plugin API
description: API to get current weather and forecasts.
version: 1.0.0
servers:
- url: https://yourdomain.com/api
paths:
/weather:
post:
summary: Get weather information
requestBody:
content:
application/json:
schema:
type: object
properties:
input:
type: string
example: "current weather in Mumbai"
responses:
'200':
description: A successful response
content:
application/json:
schema:
type: object
properties:
output:
type: string
example: "Current weather in Gurgaon: 36.7°C, Sunny"
(III) Host Your Plugin: Deploy your plugin to a server.
(III) Register and Test Your Plugin: Register your plugin with ChatGPT and test it.
Register the endpoint URL (e.g., https://apiurl/weather
) in the ChatGPT plugin settings.