-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
74 lines (61 loc) · 1.67 KB
/
app.py
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import os
import json
import openai
from flask import Flask, render_template, request, send_from_directory
from serpapi import GoogleSearch
from dotenv import load_dotenv
app = Flask(__name__)
load_dotenv()
openai.api_key = os.getenv("OPENAI_API_KEY")
serp_api_key = os.getenv("SERP_API_KEY")
headers = {
'User-agent':
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36 Edg/110.0.1587.63"
}
@app.route("/chat", methods=["POST"])
def chat():
question = request.form.get("q")
response = openai.Completion.create(
model="text-davinci-003",
prompt=generate_prompt(question),
temperature=0.2,
max_tokens=150,
n=1,
top_p=1,
frequency_penalty=0,
presence_penalty=0.6,
stop=["User:", "Sally:"]
)
print(response)
return {
'question': question,
'answer': response
}
@app.route("/googlesearch", methods=["POST"])
def search():
question = request.form.get("q")
return {
'question': question,
'answer': get_knowledge_graph(question)
}
@app.route("/<path:file_path>")
def files(file_path):
return send_from_directory('static', file_path)
@app.route("/", methods=["GET"])
def index():
return render_template("index.html")
def get_knowledge_graph(text):
params = {
"api_key": serp_api_key,
"engine": "google",
"q": text,
"hl": "en",
}
search = GoogleSearch(params)
results = search.get_dict()
return json.dumps(results.get('answer_box', ""))
def generate_prompt(phrase):
return """{}
Sally:""".format(
phrase.capitalize()
)