LiangYang666 / ChatGPT-Web

使用GPT-3.5 API创建的ChatGPT聊天页面,支持云部署,多用户使用,多对话管理,公式显示,流式动态显示,windows和linux均可极简部署,网页版 html python flask
MIT License
492 stars 308 forks source link

我这边想在代码基础上加入个逻辑 #31

Closed laovvvcom closed 1 year ago

laovvvcom commented 1 year ago

我这边想在代码基础上加入个逻辑,用户id得来自于数据库, 尝试加了个user_exist方法,并给new_user_dict添加了个逻辑,但达不到需求 `def user_exists(user_id):

连接数据库,查询用户id是否存在

conn = pymysql.connect(host=host, user=user, password=password, database=database)
sql_select_wechatnumber = f"SELECT *FROM table WHERE wechatnumber = '{user_id}'"
with conn.cursor() as cursor:
    cursor.execute(sql_select_wechatnumber)
    sql_select_number_result = cursor.fetchall()
return bool(sql_select_number_result)

def new_user_dict(user_id, send_time): if not user_exists(user_id): return "用户 ID 不存在于数据库,请重新输入或选择已有用户 ID"

chat_id = str(uuid.uuid1())
user_dict = {"chats": {chat_id: new_chat_dict(user_id, "默认对话", send_time)},
             "selected_chat_id": chat_id,
             "default_chat_id": chat_id}

user_dict['chats'][chat_id]['messages_history'].insert(1, {"role": "assistant",
                                                           "content": "- 创建新的用户id成功,请牢记该id  \n"
                                                           })
return user_dict`

能提供下修改思路吗,比如涉及哪些函数等

LiangYang666 commented 1 year ago

还需要在return_message函数中处理,判断没有时,直接返回url_redirect

laovvvcom commented 1 year ago

感谢,已经解决了