mirror of
https://github.com/Kakune55/PyGetGPT.git
synced 2025-12-17 17:53:54 +08:00
新建存储库 PyGetGPT正式发行版
This commit is contained in:
27
src/Server/chatglmTurbo.py
Normal file
27
src/Server/chatglmTurbo.py
Normal file
@@ -0,0 +1,27 @@
|
||||
import zhipuai , config
|
||||
|
||||
zhipuai.api_key = config.readConf()["chatglmturbo"]["Authorization"]
|
||||
|
||||
def service(prompt,history = ""):
|
||||
if history == "":
|
||||
response = zhipuai.model_api.invoke(
|
||||
model="chatglm_turbo",
|
||||
prompt=[
|
||||
{"role": "user", "content": prompt},
|
||||
]
|
||||
)
|
||||
else:
|
||||
response = zhipuai.model_api.invoke(
|
||||
model="chatglm_turbo",
|
||||
prompt=[
|
||||
{"role": "user", "content": history[1]["user"]},
|
||||
{"role": "assistant", "content": history[1]["bot"]},
|
||||
{"role": "user", "content": history[0]["user"]},
|
||||
{"role": "assistant", "content": history[0]["bot"]},
|
||||
{"role": "user", "content": prompt},
|
||||
]
|
||||
)
|
||||
if response["code"] == 200:
|
||||
return 200, response["data"]["choices"][0]["content"], response["data"]["usage"]['total_tokens']
|
||||
else:
|
||||
return 50 , str(response["code"])+response["msg"], 0
|
||||
5
src/Server/config.py
Normal file
5
src/Server/config.py
Normal file
@@ -0,0 +1,5 @@
|
||||
import json
|
||||
|
||||
def readConf():
|
||||
with open('config.json') as f:
|
||||
return json.load(f)
|
||||
58
src/Server/db.py
Normal file
58
src/Server/db.py
Normal file
@@ -0,0 +1,58 @@
|
||||
import pymysql , config
|
||||
|
||||
|
||||
def userSurplus(userkey):
|
||||
#打开数据库连接
|
||||
db = pymysql.connect(host=config.readConf()["db"]["host"],
|
||||
port=config.readConf()["db"]["port"],
|
||||
user=config.readConf()["db"]["user"],
|
||||
password=config.readConf()["db"]["passwd"],
|
||||
database=config.readConf()["db"]["database"])
|
||||
# 使用 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=config.readConf()["db"]["host"],
|
||||
port=config.readConf()["db"]["port"],
|
||||
user=config.readConf()["db"]["user"],
|
||||
password=config.readConf()["db"]["passwd"],
|
||||
database=config.readConf()["db"]["database"])
|
||||
# 使用 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
|
||||
48
src/Server/main.py
Normal file
48
src/Server/main.py
Normal file
@@ -0,0 +1,48 @@
|
||||
import flask , requests , json
|
||||
from flask_cors import CORS
|
||||
import db , qwenTurbo ,chatglmTurbo
|
||||
|
||||
|
||||
|
||||
|
||||
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["model"] == "qwen-turbo": # 调用qwen-Turbo
|
||||
if userRequest["context"] == 1: # 是否使用上文关联
|
||||
code , output , tokenUsed = qwenTurbo.service(userRequest['prompt'],userRequest['history'])
|
||||
elif userRequest["context"] == 0:
|
||||
code , output , tokenUsed = qwenTurbo.service(userRequest['prompt'])
|
||||
|
||||
if userRequest["model"] == "chatglm-turbo": # 调用chatglm-turbo
|
||||
if userRequest["context"] == 1: # 是否使用上文关联
|
||||
code , output , tokenUsed = chatglmTurbo.service(userRequest['prompt'],userRequest['history'])
|
||||
elif userRequest["context"] == 0:
|
||||
code , output , tokenUsed = chatglmTurbo.service(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)
|
||||
31
src/Server/qwenTurbo.py
Normal file
31
src/Server/qwenTurbo.py
Normal file
@@ -0,0 +1,31 @@
|
||||
import requests , json , config
|
||||
|
||||
# 设置请求的目标URL
|
||||
url = config.readConf()["qwenturbo"]["url"] # 替换为你的API端点URL
|
||||
header = {
|
||||
"Content-Type":"application/json",
|
||||
"Authorization":config.readConf()["qwenturbo"]["Authorization"]
|
||||
}
|
||||
|
||||
def service(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"]
|
||||
Reference in New Issue
Block a user