Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[refactor] support lasest openai-api #2

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.venv/
__pycache__/
.env.example
sample.py
2 changes: 0 additions & 2 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
FLASK_APP=app
FLASK_ENV=development
OPENAI_API_KEY=
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
venv/
.venv/

*.pyc
__pycache__/


sample.py

instance/

Expand Down
20 changes: 20 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#
FROM python:3.10

#
WORKDIR /code

#
COPY ./requirements.txt /code/requirements.txt

#
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt

#
COPY . /code

ENV OPENAI_API_KEY "ENTER YOUR API KEY"

#
# CMD ["fastapi", "dev", "app.py"]
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "8000"]
65 changes: 32 additions & 33 deletions app.py
Original file line number Diff line number Diff line change
@@ -1,57 +1,56 @@
import openai

import uvicorn
from openai import OpenAI
import os

from fastapi import FastAPI, Request, Form
from fastapi.responses import HTMLResponse, RedirectResponse
from fastapi.responses import HTMLResponse
from fastapi.templating import Jinja2Templates
from fastapi.staticfiles import StaticFiles

from pydantic import BaseSettings

from pydantic import BaseModel
from dotenv import load_dotenv

class Settings(BaseSettings):
OPENAI_API_KEY: str = 'OPENAI_API_KEY'
load_dotenv()
class Settings(BaseModel):
OPENAI_API_KEY: str = os.getenv("OPENAI_API_KEY")

class Config:
env_file = '.env'
class Config:
env_file = '.env'

settings = Settings()
openai.api_key = settings.OPENAI_API_KEY

client = OpenAI(
api_key = settings.OPENAI_API_KEY,
)

app = FastAPI()
templates = Jinja2Templates(directory="templates")
app.mount("/static", StaticFiles(directory="static"), name="static")


@app.get("/", response_class=HTMLResponse)
def index(request: Request):
return templates.TemplateResponse("index.html", {"request": request})

def return_index(request: Request):
return templates.TemplateResponse(request=request, name="index.html")

@app.post("/", response_class=HTMLResponse)
def index(request: Request, animal: str= Form(...)):
response = openai.Completion.create(
model="text-davinci-002",
prompt=generate_prompt(animal),
temperature=0.6,
response = client.chat.completions.create(
model = "gpt-3.5-turbo-0125",
messages = [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content" : generate_prompt(animal)}
],
temperature = 0.6,
)
result = response.choices[0].text
return templates.TemplateResponse("index.html", {"request": request, "result": result})
result = response.choices[0].message.content
return templates.TemplateResponse(request=request, name="index.html", context={"result": result})


def generate_prompt(animal):
return """Suggest three names for an animal that is a superhero.

Animal: Cat
Names: Captain Sharpclaw, Agent Fluffball, The Incredible Feline
Animal: Dog
Names: Ruff the Protector, Wonder Canine, Sir Barks-a-Lot
Animal: {}
Names:""".format(
animal.capitalize()
)
answer = f"""Suggest three names for an animal that is a superhero.
Animal: Cat
Names: Captain Sharpclaw, Agent Fluffball, The Incredible Feline
Animal: Dog
Names: Ruff the Protector, Wonder Canine, Sir Barks-a-Lot
Animal: {animal.capitalize()}
Names:"""
return answer

if __name__ == "__main__":
uvicorn.run('app:app', host="localhost", port=5001, reload=True)
41 changes: 3 additions & 38 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,38 +1,3 @@
anyio==3.6.1
asgiref==3.5.2
certifi==2022.5.18.1
charset-normalizer==2.0.12
click==8.1.3
colorama==0.4.4
distlib==0.3.4
et-xmlfile==1.1.0
fastapi==0.78.0
filelock==3.7.1
h11==0.13.0
idna==3.3
Jinja2==3.1.2
MarkupSafe==2.1.1
numpy==1.22.4
openai==0.19.0
openpyxl==3.0.10
pandas==1.4.2
pandas-stubs==1.2.0.61
pip==21.2.2
platformdirs==2.5.2
pydantic==1.9.1
python-dateutil==2.8.2
python-dotenv==0.20.0
python-multipart==0.0.5
pytz==2022.1
requests==2.28.0
setuptools==61.2.0
six==1.16.0
sniffio==1.2.0
starlette==0.19.1
tqdm==4.64.0
typing_extensions==4.2.0
urllib3==1.26.9
uvicorn==0.17.6
virtualenv==20.14.1
wheel==0.37.1
wincertstore==0.2
openai
fastapi
pydantic-settings