Created
May 26, 2023 10:53
-
-
Save haseeb-heaven/71d25c448ad6292b333a6357a49df605 to your computer and use it in GitHub Desktop.
This is ChatGPT Plugin Template for FastAPI shows directory structure and necessary FastAPI request component initialized.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Description: This is ChatGPT Plugin for [YOUR_APP_NAME] | |
Server API : FastAPI. | |
Language: Python. | |
Date: 26/05/2023. | |
Author : HeavenHM | |
""" | |
# Importing the required libraries. | |
from fastapi import FastAPI, Request, Depends,Response | |
from fastapi.responses import FileResponse | |
from fastapi.middleware.cors import CORSMiddleware | |
from fastapi.staticfiles import StaticFiles | |
from pydantic import BaseModel | |
from starlette.requests import Request | |
from contextvars import ContextVar | |
from waitress import serve | |
from threading import Thread | |
import logging | |
import uvicorn | |
import json | |
#defining the origin for CORS | |
ORIGINS = [ | |
"localhost:8000", "https://chat.openai.com" | |
] | |
## Main application for FastAPI Web Server | |
app = FastAPI() | |
app.add_middleware( | |
CORSMiddleware, | |
allow_origins=ORIGINS, | |
allow_credentials=True, | |
allow_methods=["*"], | |
allow_headers=["*"], | |
) | |
# Mount the .well-known directory. | |
app.mount("/.well-known", StaticFiles(directory=".well-known"), name="static") | |
# Setup logging for the application. | |
global logger | |
# Context variable to store the request. | |
# Credit - https://sl.bing.net/ib0YUGReKZg | |
request_var: ContextVar[Request] = ContextVar("request") | |
#Method to configure logs. | |
def configure_logger(name: str, filename: str): | |
logger = logging.getLogger(name) | |
logger.setLevel(logging.INFO) | |
file_handler = logging.FileHandler(filename) | |
formatter = logging.Formatter( | |
'%(asctime)s - %(name)s - %(levelname)s - %(message)s') | |
file_handler.setFormatter(formatter) | |
logger.addHandler(file_handler) | |
return logger | |
# Helper method to get the request. | |
def get_request(): | |
return request_var.get() | |
def set_request(request: Request): | |
request_var.set(request) | |
@app.middleware("http") | |
async def set_request_middleware(request: Request, call_next): | |
set_request(request) | |
response = await call_next(request) | |
return response | |
# Plugin logo. | |
@app.get("/logo.png") | |
async def plugin_logo(): | |
filename = 'logo.png' | |
logging.info(f"logo filename is {filename}") | |
return FileResponse(filename) | |
# Plugin manifest. | |
@app.get("/.well-known/ai-plugin.json") | |
async def plugin_manifest(): | |
with open("./.well-known/ai-plugin.json") as f: | |
text = f.read() | |
return Response(text, media_type="text/json") | |
# Plugin OpenAPI spec. | |
@app.get("/openapi.yaml") | |
async def openapi_spec(): | |
with open("openapi.yaml") as f: | |
text = f.read() | |
return Response(text, media_type="text/yaml") | |
@app.get('/help') | |
@app.get('/') | |
async def help(): | |
logger.info("help: Displayed for Plugin Guide") | |
json_data = {} | |
return json_data | |
# Testing purpose. | |
# call this with uvicorn main:app --reload only. | |
#logger = configure_logger('YOUR_APP_NAME', 'YOUR_APP_NAME.log') | |
# Run the app. | |
# Will only work with python main.py | |
if __name__ == "__main__": | |
logger = configure_logger('YOUR_APP_NAME', 'YOUR_APP_NAME.log') | |
uvicorn.run(app, host='127.0.0.1', port=8000) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment