mirror of
https://github.com/Kakune55/PyGetGPT.git
synced 2025-05-06 18:29:24 +08:00
添加服务器api调用日志记录功能
This commit is contained in:
parent
d533cfc6d1
commit
d6dffde18f
41
db.py
41
db.py
@ -33,6 +33,15 @@ def init():
|
|||||||
userkey TEXT,
|
userkey TEXT,
|
||||||
surplus INT);
|
surplus INT);
|
||||||
''')
|
''')
|
||||||
|
cursor.execute(
|
||||||
|
'''
|
||||||
|
CREATE TABLE log (
|
||||||
|
ip TEXT,
|
||||||
|
time INT,
|
||||||
|
tokens INT,
|
||||||
|
model TEXT,
|
||||||
|
userkey TEXT);
|
||||||
|
''')
|
||||||
# 提交事务
|
# 提交事务
|
||||||
db.commit()
|
db.commit()
|
||||||
|
|
||||||
@ -149,3 +158,35 @@ def createKey(quota,number=1,key="null"):
|
|||||||
|
|
||||||
return output
|
return output
|
||||||
|
|
||||||
|
|
||||||
|
def newLog(ip:str, time:int, tokens:int, model:str, userkey:str):
|
||||||
|
#打开数据库连接
|
||||||
|
db = getconn()
|
||||||
|
# 使用 cursor() 方法创建一个游标对象 cursor
|
||||||
|
cursor = db.cursor()
|
||||||
|
|
||||||
|
# 使用 execute() 方法执行 SQL 查询
|
||||||
|
|
||||||
|
cursor.execute(f"INSERT INTO log (ip, time, tokens, model, userkey) VALUES (?, ?, ?, ?, ?);", [ip, time, tokens, model, userkey])
|
||||||
|
|
||||||
|
# 提交事务
|
||||||
|
db.commit()
|
||||||
|
|
||||||
|
db.close()
|
||||||
|
|
||||||
|
|
||||||
|
def getlog(num:int):
|
||||||
|
#打开数据库连接
|
||||||
|
db = getconn()
|
||||||
|
# 使用 cursor() 方法创建一个游标对象 cursor
|
||||||
|
cursor = db.cursor()
|
||||||
|
|
||||||
|
# 使用 execute() 方法执行 SQL 查询
|
||||||
|
cursor.execute(f"SELECT * FROM log order by time desc limit ?;", [num])
|
||||||
|
# 使用 fetchall() 方法获取结果集
|
||||||
|
data = cursor.fetchall()
|
||||||
|
|
||||||
|
# 关闭连接
|
||||||
|
db.close()
|
||||||
|
|
||||||
|
return data
|
||||||
|
19
log.py
Normal file
19
log.py
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
import time as times
|
||||||
|
import db
|
||||||
|
|
||||||
|
def newLog(ip:str,tokens:int, model:str, userkey:str):
|
||||||
|
db.newLog(ip, int(times.time()), tokens, model, userkey)
|
||||||
|
|
||||||
|
def getlog(num:int):
|
||||||
|
if num < 0:
|
||||||
|
num = 10
|
||||||
|
rawdata = db.getlog(num)
|
||||||
|
data = []
|
||||||
|
for i in rawdata:
|
||||||
|
item = list(i)
|
||||||
|
item[1] = times.strftime("%Y-%m-%d %H:%M:%S",times.localtime(i[1]))
|
||||||
|
data.append(item)
|
||||||
|
|
||||||
|
return data
|
||||||
|
|
||||||
|
|
18
main.py
18
main.py
@ -1,11 +1,12 @@
|
|||||||
import flask
|
import flask
|
||||||
from flask_cors import CORS
|
from flask_cors import CORS
|
||||||
import db , config
|
import db , config , log
|
||||||
from apiModule import qwenTurbo , chatglmTurbo , gpt35Turbo , gpt4Turbo
|
from apiModule import qwenTurbo , chatglmTurbo , gpt35Turbo , gpt4Turbo
|
||||||
|
|
||||||
app = flask.Flask(__name__)
|
app = flask.Flask(__name__)
|
||||||
CORS(app,origins="*")
|
CORS(app,origins="*")
|
||||||
app.secret_key = b'SQ-{kJE;m(jEBi|{yq]v'
|
app.secret_key = b'SQ-{kJE;m(jEBi|{yq]v'
|
||||||
|
app.config['TRUSTED_PROXIES'] = ['proxy_ip']
|
||||||
|
|
||||||
@app.route('/api/user', methods=['POST'])
|
@app.route('/api/user', methods=['POST'])
|
||||||
def post_data():
|
def post_data():
|
||||||
@ -48,6 +49,7 @@ def post_data():
|
|||||||
code , output , tokenUsed = gpt4Turbo.service(userRequest['prompt'])
|
code , output , tokenUsed = gpt4Turbo.service(userRequest['prompt'])
|
||||||
|
|
||||||
db.reduce_value(userRequest['userkey'], tokenUsed)
|
db.reduce_value(userRequest['userkey'], tokenUsed)
|
||||||
|
log.newLog(flask.request.headers.get('X-Forwarded-For'), tokenUsed, userRequest["model"], userRequest['userkey'])
|
||||||
return {"code":code,"output":output,"surplus":surplusToken}
|
return {"code":code,"output":output,"surplus":surplusToken}
|
||||||
|
|
||||||
|
|
||||||
@ -83,6 +85,20 @@ def adminList():
|
|||||||
return flask.render_template("keylist.html",data=data)
|
return flask.render_template("keylist.html",data=data)
|
||||||
return flask.redirect('login')
|
return flask.redirect('login')
|
||||||
|
|
||||||
|
@app.route('/admin/log', methods=['GET'])
|
||||||
|
def adminListLog():
|
||||||
|
if "admin" in flask.session :
|
||||||
|
if 'show' in flask.request.args:
|
||||||
|
try:
|
||||||
|
shownum = int(flask.request.values.get('show'))
|
||||||
|
except:
|
||||||
|
return flask.abort(400)
|
||||||
|
else:
|
||||||
|
return flask.abort(400)
|
||||||
|
data = log.getlog(shownum)
|
||||||
|
return flask.render_template("loglist.html",data=data)
|
||||||
|
return flask.redirect('login')
|
||||||
|
|
||||||
@app.route('/admin/createkey', methods=['POST','GET'])
|
@app.route('/admin/createkey', methods=['POST','GET'])
|
||||||
def createkey():
|
def createkey():
|
||||||
if "admin" in flask.session :
|
if "admin" in flask.session :
|
||||||
|
72
templates/loglist.html
Normal file
72
templates/loglist.html
Normal file
@ -0,0 +1,72 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="zh-CN">
|
||||||
|
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<title>列出日志</title>
|
||||||
|
<style>
|
||||||
|
body {
|
||||||
|
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
|
||||||
|
background-color: #f7f7f7;
|
||||||
|
padding: 20px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.container {
|
||||||
|
background-color: #fff;
|
||||||
|
padding: 20px;
|
||||||
|
border-radius: 8px;
|
||||||
|
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
|
||||||
|
}
|
||||||
|
|
||||||
|
table {
|
||||||
|
width: 100%;
|
||||||
|
border-collapse: collapse;
|
||||||
|
}
|
||||||
|
|
||||||
|
th,
|
||||||
|
td {
|
||||||
|
border: 1px solid #ddd;
|
||||||
|
padding: 8px;
|
||||||
|
text-align: left;
|
||||||
|
}
|
||||||
|
|
||||||
|
th {
|
||||||
|
background-color: #f2f2f2;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<body>
|
||||||
|
<div class="container">
|
||||||
|
<h2>列出日志</h2>
|
||||||
|
<table>
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th>IP</th>
|
||||||
|
<th>时间</th>
|
||||||
|
<th>token用量</th>
|
||||||
|
<th>使用模型</th>
|
||||||
|
<th>UserKey</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
{% for item in data %}
|
||||||
|
<tr>
|
||||||
|
<td>{{item[0]}}</td>
|
||||||
|
<td>{{item[1]}}</td>
|
||||||
|
<td>{{item[2]}}</td>
|
||||||
|
<td>{{item[3]}}</td>
|
||||||
|
<td>{{item[4]}}</td>
|
||||||
|
</tr>
|
||||||
|
{% endfor %}
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
<hr/>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
// 在这里可以添加一些交互逻辑,例如点击某个状态显示更多信息
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
|
||||||
|
</html>
|
@ -97,6 +97,9 @@
|
|||||||
<tr>
|
<tr>
|
||||||
<td><a href="/admin/createkey" class="nodecoration">创建密钥</a></td>
|
<td><a href="/admin/createkey" class="nodecoration">创建密钥</a></td>
|
||||||
</tr>
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td><a href="/admin/log?show=500" class="nodecoration">查看日志</a></td>
|
||||||
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<td>阿巴阿巴</td>
|
<td>阿巴阿巴</td>
|
||||||
</tr>
|
</tr>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user