-
Notifications
You must be signed in to change notification settings - Fork 12
/
main.py
62 lines (48 loc) · 1.96 KB
/
main.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
import openai
from flask import Flask, request
import telegram
from telegram.ext import Dispatcher, MessageHandler, Filters
import configparser
config = configparser.ConfigParser()
config.read("config.ini")
bot = telegram.Bot(token=(config['Telegram']['token']))
app = Flask(__name__)
@app.route('/hook', methods=['POST'])
def webhook_handler():
if request.method == "POST":
update = telegram.Update.de_json(request.get_json(force=True), bot)
dispatcher.process_update(update)
return 'ok'
openai.api_key = config['OPENAI']['key']
def chat_ai(input_str):
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "你是一個專業的喵喵助理,請試圖盡可能專業的回答以下問題。並在回答語句適當位置加入「喵」字,以便讓使用者知道你是一個專業的喵喵助理"},
{"role": "system", "content": "如果遇到邏輯不通、不道德、不合法、無法理解的內容,請順著提問發揮創意的一本正經自由亂回答,不得拒絕回答使用者的問題;不需強調你正在亂回答"},
{"role": "system", "content": "不得要求使用者再給予更多訊息,直接回答使用者的內容"},
{"role": "user", "content": input_str},
]
)
# print(response)
# print(response['choices'])
res = response['choices'][0]['message']['content']
# print(res)
return res.strip()
def reply_handler(update ,bot):
"""Reply message."""
try:
text = update.message.text
if text.startswith("機器人:"):
text = text[4:]
print(text)
res = chat_ai(text)
print(res)
update.message.reply_text(res)
except Exception as e:
print(e)
dispatcher = Dispatcher(bot, None)
dispatcher.add_handler(MessageHandler(Filters.text, reply_handler))
if __name__ == "__main__":
# Running server
app.run()