Skip to content

Commit

Permalink
wip fastapi
Browse files Browse the repository at this point in the history
fastapi on localhost
  • Loading branch information
0dm committed May 7, 2023
1 parent 29419e1 commit cdaee2a
Show file tree
Hide file tree
Showing 7 changed files with 204 additions and 0 deletions.
Empty file.
40 changes: 40 additions & 0 deletions puterbot/plugin/data/demo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from pynput.keyboard import Key, Controller
from dotenv import load_dotenv
import os
import openai

load_dotenv()

openai.api_key = os.getenv("API_KEY")

params = {
'engine': 'text-davinci-003',
'prompt': '',
'max_tokens': 496,
'temperature': 0.7,
}

endpoint = "localhost:3333"

keyboard = Controller()

def sendkeys(args):
# open sublime in new terminal window
os.system("open /Applications/Sublime\ Text.app")

params.update({"prompt": args})
response = openai.Completion.create(**params)

print("Sending keys: ", response["choices"][0]["text"])
# sleep for 3 seconds
# __import__("time").sleep(3)
keyboard.type(response["choices"][0]["text"])
return 200

def test():
a = sendkeys("Hello, world!")
if a == 200:
print("Success!")

if __name__ == "__main__":
print(os.getenv("API_KEY"))
18 changes: 18 additions & 0 deletions puterbot/plugin/server/ai-plugin.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"schema_version": "v1",
"name_for_human": "puterbot plugin",
"name_for_model": "openadapt-puterbot",
"description_for_human": "Connect to ChatGPT",
"description_for_model": "Utilize puterbot for process automation, be precise.",

This comment has been minimized.

Copy link
@abrichr

abrichr May 10, 2023

Member

These descriptions need to be more descriptive. This is all that ChatGPT has to go on to understand what OpenAdapt is all about. Can you please flesh this out?

"auth": {
"type": "none"
},
"api": {
"type": "openapi",
"url": "http://localhost:3333/.well-known/openapi.yaml",
"is_user_authenticated": false
},
"logo_url": "https://avatars.githubusercontent.com/u/67930343?s=200&v=4",
"contact_email": "[email protected]",
"legal_info_url": "todo.todo"
}
Binary file added puterbot/plugin/server/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
67 changes: 67 additions & 0 deletions puterbot/plugin/server/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import uvicorn
from typing import List, Dict
from fastapi import FastAPI, File, Form, HTTPException, Body
from starlette.responses import FileResponse
from fastapi.middleware.cors import CORSMiddleware
# import datetime, maybe for timestamps?
from data.demo import (
sendkeys
)


# USEFUL : https://github.com/NextWordDev/chatgpt-plugins/tree/main

app = FastAPI()

PORT = 3333

origins = [
f"http://localhost:{PORT}",
"https://chat.openai.com",
]

app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)


def _automate_helper(data):
out = sendkeys(data)
return out


#@app.post("/automate", response_model=Dict)
@app.post("/")
async def automate(data: str):
return _automate_helper(data)


@app.route("/.well-known/ai-plugin.json")
async def get_manifest(request):
file_path = "./server/ai-plugin.json"
return FileResponse(file_path, media_type="text/json", headers={"Cache-Control": "no-store"})


@app.route("/.well-known/logo.png")
async def get_logo(request):
file_path = "./server/logo.png"
return FileResponse(file_path, media_type="text/json", headers={"Cache-Control": "no-store"})


@app.route("/.well-known/openapi.yaml")
async def get_openapi(request):
file_path = "./server/openapi.yaml"
return FileResponse(file_path, media_type="text/json", headers={"Cache-Control": "no-store"})


@app.on_event("startup")
async def startup():
print("Starting up...")


def start():
uvicorn.run("server.main:app", host="localhost", port=PORT, reload=True)
74 changes: 74 additions & 0 deletions puterbot/plugin/server/openapi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
---
openapi: 3.0.2
info:
title: FastAPI
version: 0.1.0
paths:
"/automate":
post:
summary: Integrate with puterbot.
operationId: automate_automate_post
description: Integrate with puterbot.
parameters:
- required: true
schema:
title: Data
type: string
name: data
in: query
responses:
'200':
description: OK
content:
application/json:
$ref: '#/components/schemas/getOutputResponse'
'422':
description: Validation Error
content:
application/json:
schema:
"$ref": "#/components/schemas/HTTPValidationError"
components:
schemas:
HTTPValidationError:
title: HTTPValidationError
type: object
properties:
detail:
title: Detail
type: array
items:
"$ref": "#/components/schemas/ValidationError"
ValidationError:
title: ValidationError
required:
- loc
- msg
- type
type: object
properties:
loc:
title: Location
type: array
items:
anyOf:
- type: string
- type: integer
msg:
title: Message
type: string
type:
title: Error Type
type: string
getOutputResponse:
type: object
properties:
data:
type: string
message:
type: string
status:
type: string
timestamp:
type: string
description: response model
5 changes: 5 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,8 @@ sqlalchemy==1.4.43
torch==2.0.0
tqdm==4.64.0
transformers==4.28.1
requests==2.30.0
fastapi==0.95.1
uvicorn==0.22.0
openapi==1.1.0
python-dotenv==1.0.0

0 comments on commit cdaee2a

Please sign in to comment.