mirror of
https://github.com/Kakune55/PyGetGPT.git
synced 2025-06-28 15:58:04 +08:00
模块化重构
This commit is contained in:
parent
694130d5e8
commit
ba021500b6
58
src/Server/db.py
Normal file
58
src/Server/db.py
Normal file
@ -0,0 +1,58 @@
|
||||
import pymysql
|
||||
|
||||
|
||||
def userSurplus(userkey):
|
||||
#打开数据库连接
|
||||
db = pymysql.connect(host='www.kakuweb.top',
|
||||
port=33306,
|
||||
user='root',
|
||||
password='KAku3.14159..',
|
||||
database='AIapi')
|
||||
# 使用 cursor() 方法创建一个游标对象 cursor
|
||||
cursor = db.cursor()
|
||||
|
||||
# 使用 execute() 方法执行 SQL 查询
|
||||
cursor.execute(f"SELECT surplus FROM usersurplus WHERE userkey = '{userkey}';")
|
||||
# 使用 fetchone() 方法获取单条数据.
|
||||
data = cursor.fetchone()
|
||||
|
||||
# 关闭连接
|
||||
db.close()
|
||||
|
||||
if data != None:
|
||||
return data[0]
|
||||
return -99999
|
||||
|
||||
def reduce_value(userkey, value): # 减去对应的值
|
||||
#打开数据库连接
|
||||
db = pymysql.connect(host='www.kakuweb.top',
|
||||
port=33306,
|
||||
user='root',
|
||||
password='KAku3.14159..',
|
||||
database='AIapi')
|
||||
# 使用 cursor() 方法创建一个游标对象 cursor
|
||||
cursor = db.cursor()
|
||||
|
||||
# 执行 SQL 查询以获取当前值
|
||||
cursor.execute(f"SELECT surplus FROM usersurplus WHERE userkey = '{userkey}';")
|
||||
current_value = cursor.fetchone()[0]
|
||||
|
||||
# 如果没有找到用户,则返回错误信息
|
||||
if current_value is None:
|
||||
db.close()
|
||||
return -1
|
||||
|
||||
# 计算新的值
|
||||
new_value = current_value - value
|
||||
|
||||
# 更新数据库中的值
|
||||
cursor.execute(f"UPDATE usersurplus SET surplus={new_value} WHERE userkey='{userkey}'")
|
||||
|
||||
# 提交事务
|
||||
db.commit()
|
||||
|
||||
# 关闭连接
|
||||
db.close()
|
||||
|
||||
# 返回新值
|
||||
return 0
|
37
src/Server/main.py
Normal file
37
src/Server/main.py
Normal file
@ -0,0 +1,37 @@
|
||||
import flask , requests , json
|
||||
from flask_cors import CORS
|
||||
import db , qwenTurbo
|
||||
|
||||
|
||||
|
||||
|
||||
app = flask.Flask(__name__)
|
||||
CORS(app,origins="*")
|
||||
|
||||
@app.route('/api/user', methods=['POST'])
|
||||
def post_data():
|
||||
userRequest = flask.request.json
|
||||
surplusToken = db.userSurplus(userRequest['userkey'])
|
||||
|
||||
if userRequest["prompt"] == "":
|
||||
return {"code":42,"output":"Input is empty"}
|
||||
|
||||
if userRequest["prompt"] == "":
|
||||
return {"code":42,"output":"UserKey is empty"}
|
||||
|
||||
if surplusToken == -99999: # 判断用户是否存在和余额
|
||||
return {"code":41,"output":"UserKey not found"}
|
||||
elif surplusToken <= 0:
|
||||
return {"code":40,"output":"Token has been use up"}
|
||||
|
||||
if userRequest["context"] == 1: # 是否使用上文关联
|
||||
code , output , tokenUsed = qwenTurbo.service(userRequest['userkey'],userRequest['prompt'],userRequest['history'])
|
||||
elif userRequest["context"] == 0:
|
||||
code , output , tokenUsed = qwenTurbo.service(userRequest['userkey'],userRequest['prompt'])
|
||||
|
||||
db.reduce_value(userRequest['userkey'], tokenUsed)
|
||||
return {"code":code,"output":output,"surplus":surplusToken}
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run(debug=True,host='0.0.0.0',port=5000)
|
32
src/Server/qwenTurbo.py
Normal file
32
src/Server/qwenTurbo.py
Normal file
@ -0,0 +1,32 @@
|
||||
import requests , json
|
||||
|
||||
# 设置请求的目标URL
|
||||
url = "https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation" # 替换为你的API端点URL
|
||||
|
||||
header = {
|
||||
"Content-Type":"application/json",
|
||||
"Authorization":"Bearer sk-69129a5d7fc6468a9f6f30d6935254c6"
|
||||
}
|
||||
|
||||
def service(userkey,prompt,history = ""):
|
||||
# 设置请求数据
|
||||
if history == "":
|
||||
data = {
|
||||
"model": "qwen-turbo",
|
||||
"input":{
|
||||
"prompt":f"{prompt}"
|
||||
}
|
||||
}
|
||||
else:
|
||||
data = {
|
||||
"model": "qwen-turbo",
|
||||
"input":{
|
||||
"prompt":f"{prompt}",
|
||||
"history":history
|
||||
}
|
||||
}
|
||||
# 发送POST请求
|
||||
response = json.loads(requests.post(url, json=data ,headers=header).text)
|
||||
if 'code' in response:
|
||||
return 50,response['code']+response['message'],0
|
||||
return 200,response['output']['text'],response["usage"]["total_tokens"]
|
@ -1,123 +0,0 @@
|
||||
import flask , requests , json , pymysql
|
||||
from flask_cors import CORS
|
||||
|
||||
# 设置请求的目标URL
|
||||
url = "https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation" # 替换为你的API端点URL
|
||||
|
||||
header = {
|
||||
"Content-Type":"application/json",
|
||||
"Authorization":"Bearer sk-69129a5d7fc6468a9f6f30d6935254c6"
|
||||
|
||||
}
|
||||
|
||||
def request_TY(userkey,prompt,history = ""):
|
||||
# 设置请求数据
|
||||
if history == "":
|
||||
data = {
|
||||
"model": "qwen-turbo",
|
||||
"input":{
|
||||
"prompt":f"{prompt}"
|
||||
}
|
||||
}
|
||||
else:
|
||||
data = {
|
||||
"model": "qwen-turbo",
|
||||
"input":{
|
||||
"prompt":f"{prompt}",
|
||||
"history":history
|
||||
}
|
||||
}
|
||||
# 发送POST请求
|
||||
response = json.loads(requests.post(url, json=data ,headers=header).text)
|
||||
if 'code' in response:
|
||||
if response['code'] == 'DataInspectionFailed':
|
||||
return response['message']
|
||||
reduce_value(userkey, response["usage"]["total_tokens"])
|
||||
return response['output']['text']
|
||||
|
||||
def userSurplus(userkey):
|
||||
#打开数据库连接
|
||||
db = pymysql.connect(host='www.kakuweb.top',
|
||||
port=33306,
|
||||
user='root',
|
||||
password='KAku3.14159..',
|
||||
database='AIapi')
|
||||
# 使用 cursor() 方法创建一个游标对象 cursor
|
||||
cursor = db.cursor()
|
||||
|
||||
# 使用 execute() 方法执行 SQL 查询
|
||||
cursor.execute(f"SELECT surplus FROM usersurplus WHERE userkey = '{userkey}';")
|
||||
# 使用 fetchone() 方法获取单条数据.
|
||||
data = cursor.fetchone()
|
||||
|
||||
# 关闭连接
|
||||
db.close()
|
||||
|
||||
if data != None:
|
||||
return data[0]
|
||||
return -99999
|
||||
|
||||
def reduce_value(userkey, value):
|
||||
#打开数据库连接
|
||||
db = pymysql.connect(host='www.kakuweb.top',
|
||||
port=33306,
|
||||
user='root',
|
||||
password='KAku3.14159..',
|
||||
database='AIapi')
|
||||
# 使用 cursor() 方法创建一个游标对象 cursor
|
||||
cursor = db.cursor()
|
||||
|
||||
# 执行 SQL 查询以获取当前值
|
||||
cursor.execute(f"SELECT surplus FROM usersurplus WHERE userkey = '{userkey}';")
|
||||
current_value = cursor.fetchone()[0]
|
||||
|
||||
# 如果没有找到用户,则返回错误信息
|
||||
if current_value is None:
|
||||
db.close()
|
||||
return -1
|
||||
|
||||
# 计算新的值
|
||||
new_value = current_value - value
|
||||
|
||||
# 更新数据库中的值
|
||||
cursor.execute(f"UPDATE usersurplus SET surplus={new_value} WHERE userkey='{userkey}'")
|
||||
|
||||
# 提交事务
|
||||
db.commit()
|
||||
|
||||
# 关闭连接
|
||||
db.close()
|
||||
|
||||
# 返回新值
|
||||
return 0
|
||||
|
||||
|
||||
|
||||
|
||||
app = flask.Flask(__name__)
|
||||
CORS(app,origins="*")
|
||||
|
||||
@app.route('/api/user', methods=['POST'])
|
||||
def post_data():
|
||||
userrequest = flask.request.json
|
||||
surplus = userSurplus(userrequest['userkey'])
|
||||
|
||||
if userrequest["prompt"] == "":
|
||||
return {"code":400,"output":"Input is empty"}
|
||||
|
||||
if userrequest["prompt"] == "":
|
||||
return {"code":400,"output":"UserKey is empty"}
|
||||
|
||||
if surplus > 0:
|
||||
if userrequest["context"] == 1:
|
||||
return {"code":200,"output":f"{request_TY(userrequest['userkey'],userrequest['prompt'],userrequest['history'])}","surplus":userSurplus(userrequest['userkey'])}
|
||||
else:
|
||||
return {"code":200,"output":f"{request_TY(userrequest['userkey'],userrequest['prompt'])}","surplus":userSurplus(userrequest['userkey'])}
|
||||
elif surplus == -99999:
|
||||
return {"code":400,"output":"No User"}
|
||||
elif surplus <= 0:
|
||||
return {"code":403,"output":"No prompt Tokens","surplus":surplus}
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run(debug=True,host='0.0.0.0',port=5000)
|
Loading…
x
Reference in New Issue
Block a user