Update app.py
Browse files
app.py
CHANGED
|
@@ -15,35 +15,72 @@ app = Flask(__name__)
|
|
| 15 |
|
| 16 |
# 配置
|
| 17 |
TOKEN = os.getenv('TOKEN')
|
| 18 |
-
API_KEY = os.getenv("
|
| 19 |
BASE_URL = os.getenv("OPENAI_BASE_URL")
|
| 20 |
client = OpenAI(api_key=API_KEY, base_url=BASE_URL)
|
| 21 |
|
| 22 |
# 定义可用的模型列表
|
| 23 |
AVAILABLE_MODELS = {
|
| 24 |
'gpt-3.5-turbo': 'GPT-3.5 Turbo',
|
| 25 |
-
'gpt-
|
| 26 |
-
'gpt-
|
| 27 |
}
|
| 28 |
|
| 29 |
# 存储用户会话信息
|
| 30 |
user_sessions = {}
|
| 31 |
|
| 32 |
def verify_wechat(request):
|
| 33 |
-
#
|
| 34 |
-
.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
def getUserMessageContentFromXML(xml_content):
|
| 37 |
-
#
|
| 38 |
-
.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
def generate_response_xml(from_user_name, to_user_name, output_content):
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
def split_message(message, max_length=500):
|
| 49 |
return [message[i:i+max_length] for i in range(0, len(message), max_length)]
|
|
@@ -81,7 +118,7 @@ def wechatai():
|
|
| 81 |
response_content = "没有待发送的消息。"
|
| 82 |
else:
|
| 83 |
if from_user_name not in user_sessions:
|
| 84 |
-
user_sessions[from_user_name] = {'model': 'gpt-
|
| 85 |
|
| 86 |
session = user_sessions[from_user_name]
|
| 87 |
session['messages'].append({"role": "user", "content": user_message_content})
|
|
@@ -98,6 +135,6 @@ def wechatai():
|
|
| 98 |
response_content = response_parts[0]
|
| 99 |
|
| 100 |
return generate_response_xml(from_user_name, to_user_name, response_content)
|
| 101 |
-
|
| 102 |
if __name__ == '__main__':
|
| 103 |
app.run(host='0.0.0.0', port=7860, debug=True)
|
|
|
|
| 15 |
|
| 16 |
# 配置
|
| 17 |
TOKEN = os.getenv('TOKEN')
|
| 18 |
+
API_KEY = os.getenv("API_KEY")
|
| 19 |
BASE_URL = os.getenv("OPENAI_BASE_URL")
|
| 20 |
client = OpenAI(api_key=API_KEY, base_url=BASE_URL)
|
| 21 |
|
| 22 |
# 定义可用的模型列表
|
| 23 |
AVAILABLE_MODELS = {
|
| 24 |
'gpt-3.5-turbo': 'GPT-3.5 Turbo',
|
| 25 |
+
'gpt-4o': 'GPT-4o',
|
| 26 |
+
'gpt-4o-mini': 'GPT-4o-mini',
|
| 27 |
}
|
| 28 |
|
| 29 |
# 存储用户会话信息
|
| 30 |
user_sessions = {}
|
| 31 |
|
| 32 |
def verify_wechat(request):
|
| 33 |
+
# 获取微信服务器发送过来的参数
|
| 34 |
+
data = request.args
|
| 35 |
+
signature = data.get('signature')
|
| 36 |
+
timestamp = data.get('timestamp')
|
| 37 |
+
nonce = data.get('nonce')
|
| 38 |
+
echostr = data.get('echostr')
|
| 39 |
+
|
| 40 |
+
# 对参数进行字典排序,拼接字符串
|
| 41 |
+
temp = [timestamp, nonce, TOKEN]
|
| 42 |
+
temp.sort()
|
| 43 |
+
temp = ''.join(temp)
|
| 44 |
+
|
| 45 |
+
# 加密
|
| 46 |
+
if (hashlib.sha1(temp.encode('utf8')).hexdigest() == signature):
|
| 47 |
+
return echostr
|
| 48 |
+
else:
|
| 49 |
+
return 'error', 403
|
| 50 |
|
| 51 |
def getUserMessageContentFromXML(xml_content):
|
| 52 |
+
# 解析XML字符串
|
| 53 |
+
root = ET.fromstring(xml_content)
|
| 54 |
+
# 提取数据
|
| 55 |
+
content = root.find('Content').text
|
| 56 |
+
from_user_name = root.find('FromUserName').text
|
| 57 |
+
to_user_name = root.find('ToUserName').text
|
| 58 |
+
return content, from_user_name, to_user_name
|
| 59 |
|
| 60 |
def generate_response_xml(from_user_name, to_user_name, output_content):
|
| 61 |
+
output_xml = '''
|
| 62 |
+
<xml>
|
| 63 |
+
<ToUserName><![CDATA[%s]]></ToUserName>
|
| 64 |
+
<FromUserName><![CDATA[%s]]></FromUserName>
|
| 65 |
+
<CreateTime>%s</CreateTime>
|
| 66 |
+
<MsgType><![CDATA[text]]></MsgType>
|
| 67 |
+
<Content><![CDATA[%s]]></Content>
|
| 68 |
+
</xml>'''
|
| 69 |
+
|
| 70 |
+
response = make_response(output_xml % (from_user_name, to_user_name, str(int(time.time())), output_content))
|
| 71 |
+
response.content_type = 'application/xml'
|
| 72 |
+
return response
|
| 73 |
+
|
| 74 |
+
def get_openai_response(messages, model="gpt-4o-mini"):
|
| 75 |
+
try:
|
| 76 |
+
response = client.chat.completions.create(
|
| 77 |
+
model=model,
|
| 78 |
+
messages=messages
|
| 79 |
+
)
|
| 80 |
+
return response.choices[0].message.content
|
| 81 |
+
except Exception as e:
|
| 82 |
+
print(f"调用OpenAI API时出错: {str(e)}")
|
| 83 |
+
return "抱歉,我遇到了一些问题,无法回答您的问题。"
|
| 84 |
|
| 85 |
def split_message(message, max_length=500):
|
| 86 |
return [message[i:i+max_length] for i in range(0, len(message), max_length)]
|
|
|
|
| 118 |
response_content = "没有待发送的消息。"
|
| 119 |
else:
|
| 120 |
if from_user_name not in user_sessions:
|
| 121 |
+
user_sessions[from_user_name] = {'model': 'gpt-4o-mini', 'messages': [], 'pending_response': []}
|
| 122 |
|
| 123 |
session = user_sessions[from_user_name]
|
| 124 |
session['messages'].append({"role": "user", "content": user_message_content})
|
|
|
|
| 135 |
response_content = response_parts[0]
|
| 136 |
|
| 137 |
return generate_response_xml(from_user_name, to_user_name, response_content)
|
| 138 |
+
|
| 139 |
if __name__ == '__main__':
|
| 140 |
app.run(host='0.0.0.0', port=7860, debug=True)
|