forked from openai/openai-quickstart-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
57 lines (50 loc) · 1.66 KB
/
app.py
File metadata and controls
57 lines (50 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import os
import json
import openai
from flask import Flask, redirect, render_template, request, url_for
from dotenv import load_dotenv # Add
load_dotenv()
app = Flask(__name__)
openai.api_key = os.getenv("OPENAI_API_KEY")
conversation = [
{"role": "system", "content": "You are a customer serivce."}
]
@app.route("/", methods=("GET", "POST"))
def index():
if request.method == "POST":
conversation.append(
{
'role': 'user',
'content': request.form["prompt"]
}
)
response = openai.ChatCompletion.create(
# model="text-davinci-003",
model="gpt-3.5-turbo",
# prompt=generate_prompt(animal),
messages=conversation
# prompt=request.form["prompt"],
# temperature=0.6,
# temperature=0,
)
conversation.append(
{
'role': response.choices[0].message.role,
'content': response.choices[0].message.content
}
)
# return redirect(url_for("index", result=response.choices[0].text))
# return redirect(url_for("index", result=json.dumps(conversation)))
# result = request.args.get("result")
# return render_template("index.html", result=result)
return render_template("index.html", result=json.dumps(conversation))
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()
)