-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
100 lines (85 loc) · 3.58 KB
/
app.py
File metadata and controls
100 lines (85 loc) · 3.58 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
from flask import Flask, render_template, request, jsonify, abort
import os, openai
from dotenv import load_dotenv
from linebot.exceptions import InvalidSignatureError
from linebot import LineBotApi, WebhookHandler
from linebot.models import TextSendMessage, MessageEvent, TextMessage
load_dotenv()
openai.api_key = os.getenv("OPEN_AI_API_KEY")
line_bot_api = LineBotApi(os.getenv("LINE_CHANNEL_ACCESS_TOKEN"))
handler = WebhookHandler(os.getenv("LINE_CHANNEL_SECRET"))
app = Flask(__name__, static_folder='static', template_folder='templates', static_url_path='/static')
user_list_in_memory = []
conversation_history = {}
@app.route("/callback", methods=['POST'])
def callback():
signature = request.headers['X-Line-Signature']
body = request.get_data(as_text=True)
# get user id
user_id = request.json['events'][0]['source']['userId']
if user_id not in user_list_in_memory:
user_list_in_memory.append(user_id)
try:
handler.handle(body, signature)
except InvalidSignatureError:
print("Invalid signature. Please check your channel access token/channel secret.")
abort(400)
return 'OK'
# Line chatGPT
@handler.add(MessageEvent, message=TextMessage)
def handle_message(event):
message_log = None
message = event.message.text
user_id = event.source.user_id
if user_id not in conversation_history:
conversation_history[user_id] = []
try:
if message == "clear" or message == "清除" or message == "清空" or message == "清除歷史" or message == "清空歷史":
conversation_history[user_id] = []
line_bot_api.reply_message(
event.reply_token,
TextSendMessage(text="已清除歷史訊息")
)
elif message == "get_list":
line_bot_api.reply_message(
event.reply_token,
TextSendMessage(text=str(user_list_in_memory))
)
elif message == "get_history":
line_bot_api.reply_message(
event.reply_token,
TextSendMessage(text=str(conversation_history))
)
else:
conversation_history[user_id].append({"role": "user", "content": message})
message_log = conversation_history[user_id]
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo", # 使用的模型
messages=message_log, # 對話紀錄
# max_tokens=4097, # token上限
stop="exit", # 結束對話的字串
temperature=0.7,
)
conversation_history[user_id].append({"role": "assistant", "content": response.choices[0].message.content})
line_bot_api.reply_message(
event.reply_token,
TextSendMessage(text=response.choices[0].message.content)
)
except Exception as e:
print(e)
print(message_log)
# clear conversation history
conversation_history[user_id] = []
line_bot_api.reply_message(
event.reply_token,
TextSendMessage(text="Oops! 發生了一點問題,已為您清除歷史訊息,請重新嘗試")
)
if __name__ == "__main__":
app.run(host="0.0.0.0", port=5000, debug=True)
# HOW TO RUN WITH DOCKER
# 1. build image: docker build -t chatgpt .
# 2. run container: docker run -d -p 5000:5000 chatgpt
# 3. check container: docker ps
# 4. Open browser and go to http://localhost:5000/
# 5. stop container: docker stop <container id>
# 6. remove container: docker rm <container id>