-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
115 lines (89 loc) · 3.51 KB
/
main.py
File metadata and controls
115 lines (89 loc) · 3.51 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
from flask import Flask, send_from_directory
from flask import request, jsonify
from flask import render_template
from flask_cors import CORS
import os
import openai
import subprocess
import json
app = Flask(__name__)
#CORS(app)
API_KEY= os.getenv("FLASK_API_KEY")
msg_prompt = {
'Recommendation' : {
'system' : "You are a helpful assistant to recommend the products searched in the chatbot conversation system.",
'user' : "Write 1 sentence of a simple greeting that starts with '말씀하신 상품을 찾아드리겠습니다.'"
},
'Comparison' : {
'system' : "You are an assistant who can compare prices in the chatbot conversation system.",
'user' : "Write 1 sentence of a simple greeting that starts with '물론이죠! 말씀하신 상품의 가격을 비교해드릴게요.'"
},
'Summarization' : {
'system' : "You are a helpful assistant to summarize the review.",
'user' : "Write 1 sentence of a simple greeting that starts with '물론이죠! 이 상품의 리뷰를 요약해드리겠습니다.'"
},
'intent' : {
'system' : "You are a helpful assistant who understands the intent of the user's question.",
'user' : "Which category does the sentence below belong to: 'Search', Recommendation', 'Comparison', 'Summarization'? Show only categories."
}
}
price_messages = [
{"role" : "system", "content" : "You are a helpful assistant to print out only product_list."}
]
def set_prompt(intent, input, msg_prompt):
m = dict()
if('Recommendation' in intent) or ('Search' in intent):
msg = msg_prompt['Recommendation']
elif('Comparison' in intent):
msg = msg_prompt['Comparison']
elif('Summarization' in intent):
msg = msg_prompt['Summarization']
else:
msg = msg_prompt['intent']
msg['user'] += f' {input} \n A:'
for k, v in msg.items():
m['role'], m['content'] = k, v
return [m]
def chatGPT(messages):
# set api key
openai.api_key = API_KEY
completion = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=messages,
temperature=0.7,
max_tokens=2048
)
return completion['choices'][0]['message']['content']
@app.route("/")
def chat():
return send_from_directory('gpt_call', 'chat_copy.html')
@app.route('/process_output', methods=['POST'])
def process_output():
data = request.get_json()
output = data['output']
result = {'message' : 'output 작업이 완료되었습니다.'}
crawling(output)
product_list = []
with open('./webCrawling/output.json', 'r') as f:
product_list = json.load(f)
return jsonify({'product_list' : product_list})
@app.route('/process_input', methods=['POST'])
def process_input():
data = request.get_json()
input = data['input']
result = {'message' : 'input 작업이 완료되었습니다.'}
user_intent = set_prompt('intent', input, msg_prompt)
user_intent = chatGPT(user_intent)
intent_data = set_prompt(user_intent, input, msg_prompt)
intent_data_msg = chatGPT(intent_data).replace("\n", "").strip()
print(intent_data_msg)
return jsonify({'user_intent': user_intent, 'intent_data_msg' : intent_data_msg})
@app.route('/')
def crawling(output):
cmd = f'node ./webCrawling/crawling.js "{output}"'
subprocess.run(cmd, shell=True)
@app.route("/crawl")
def crawldata():
return send_from_directory('webCrawling', 'output.json')
if __name__ == '__main__':
app.run(host='127.0.0.1', debug=True, port=5000)