Update app.py
Browse files
app.py
CHANGED
|
@@ -1,57 +1,86 @@
|
|
| 1 |
-
|
| 2 |
-
from
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
app = Flask(__name__)
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
def
|
| 18 |
-
|
| 19 |
-
return
|
| 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 |
if __name__ == '__main__':
|
| 57 |
-
app.run(host='0.0.0.0', port=7860
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from flask import Flask, request, abort
|
| 3 |
+
import hashlib
|
| 4 |
+
import xmltodict
|
| 5 |
+
import openai
|
| 6 |
+
from wechatpy import parse_message
|
| 7 |
+
from wechatpy.utils import check_signature
|
| 8 |
+
from wechatpy.exceptions import InvalidSignatureException
|
| 9 |
|
| 10 |
app = Flask(__name__)
|
| 11 |
+
|
| 12 |
+
# 配置
|
| 13 |
+
TOKEN = 'your_wechat_token'
|
| 14 |
+
APPID = 'your_wechat_appid'
|
| 15 |
+
APPSECRET = 'your_wechat_appsecret'
|
| 16 |
+
OPENAI_API_KEY = 'your_openai_api_key'
|
| 17 |
+
|
| 18 |
+
openai.api_key = OPENAI_API_KEY
|
| 19 |
+
|
| 20 |
+
# 存储用户当前使用的模型
|
| 21 |
+
user_models = {}
|
| 22 |
+
|
| 23 |
+
def split_message(message, max_length=500):
|
| 24 |
+
"""Split a message into chunks of max_length characters."""
|
| 25 |
+
return [message[i:i+max_length] for i in range(0, len(message), max_length)]
|
| 26 |
+
|
| 27 |
+
def get_gpt_response(message, model="gpt-3.5-turbo"):
|
| 28 |
+
"""Get response from GPT model."""
|
| 29 |
+
try:
|
| 30 |
+
response = openai.ChatCompletion.create(
|
| 31 |
+
model=model,
|
| 32 |
+
messages=[{"role": "user", "content": message}]
|
| 33 |
+
)
|
| 34 |
+
return response.choices[0].message.content
|
| 35 |
+
except Exception as e:
|
| 36 |
+
return f"Error: {str(e)}"
|
| 37 |
+
|
| 38 |
+
@app.route('/', methods=['GET', 'POST'])
|
| 39 |
+
def wechat():
|
| 40 |
+
if request.method == 'GET':
|
| 41 |
+
token = TOKEN
|
| 42 |
+
signature = request.args.get('signature', '')
|
| 43 |
+
timestamp = request.args.get('timestamp', '')
|
| 44 |
+
nonce = request.args.get('nonce', '')
|
| 45 |
+
echostr = request.args.get('echostr', '')
|
| 46 |
+
try:
|
| 47 |
+
check_signature(token, signature, timestamp, nonce)
|
| 48 |
+
except InvalidSignatureException:
|
| 49 |
+
abort(403)
|
| 50 |
+
return echostr
|
| 51 |
+
else:
|
| 52 |
+
xml_data = request.data
|
| 53 |
+
msg = parse_message(xml_data)
|
| 54 |
+
if msg.type == 'text':
|
| 55 |
+
user_id = msg.source
|
| 56 |
+
content = msg.content
|
| 57 |
+
|
| 58 |
+
if content.startswith('/model'):
|
| 59 |
+
# 切换模型
|
| 60 |
+
model = content.split(' ')[1]
|
| 61 |
+
user_models[user_id] = model
|
| 62 |
+
return f'Model switched to {model}'
|
| 63 |
+
|
| 64 |
+
model = user_models.get(user_id, "gpt-3.5-turbo")
|
| 65 |
+
response = get_gpt_response(content, model)
|
| 66 |
+
|
| 67 |
+
# 拆分长消息
|
| 68 |
+
response_parts = split_message(response)
|
| 69 |
+
|
| 70 |
+
# 构建回复消息
|
| 71 |
+
reply = []
|
| 72 |
+
for part in response_parts:
|
| 73 |
+
reply.append(f"""
|
| 74 |
+
<xml>
|
| 75 |
+
<ToUserName><![CDATA[{msg.source}]]></ToUserName>
|
| 76 |
+
<FromUserName><![CDATA[{msg.target}]]></FromUserName>
|
| 77 |
+
<CreateTime>{int(time.time())}</CreateTime>
|
| 78 |
+
<MsgType><![CDATA[text]]></MsgType>
|
| 79 |
+
<Content><![CDATA[{part}]]></Content>
|
| 80 |
+
</xml>
|
| 81 |
+
""")
|
| 82 |
+
|
| 83 |
+
return ''.join(reply)
|
| 84 |
|
| 85 |
if __name__ == '__main__':
|
| 86 |
+
app.run(host='0.0.0.0', port=7860)
|