代码拉取完成,页面将自动刷新
from fastapi import FastAPI, Request
import uvicorn, json, datetime
from modelscope import AutoTokenizer, AutoModel, snapshot_download
model_dir = snapshot_download("ZhipuAI/chatglm3-6b-32k", revision = "v1.0.0")
tokenizer = AutoTokenizer.from_pretrained(model_dir, trust_remote_code=True)
model = AutoModel.from_pretrained(model_dir, trust_remote_code=True).half().cuda()
model = model.eval()
def getBizPrompt(fn):
with open(fn, 'r',encoding="utf-8") as file:
content = file.read()
return content
history=[]
app = FastAPI()
@app.post("/gpu/his/clear")
async def gpuHisClear(request: Request):
global model, tokenizer,history
history=[]
return history
@app.post("/gpu/chat")
async def gpuChat(request: Request):
global model, tokenizer,history
json_post_raw = await request.json()
json_post = json.dumps(json_post_raw)
json_post_list = json.loads(json_post)
content = json_post_list.get('content')
withHistory = json_post_list.get('withHistory')
responseType = json_post_list.get('responseType')
if withHistory and len(history) == 0:
u1={"role": "user","content":"下面是你和我对话的背景信息:\n\n\n\n{}".format(getBizPrompt(json_post_list.get('BizPrompt')))}
x1={"role": "assistant","metadata": "","content":"好的,请问您有什么问题吗?"}
history.append(u1)
history.append(x1)
response, history = model.chat(tokenizer, content, history=history if withHistory else [] )
if "history" == responseType :
return history
else:
return response
if __name__ == '__main__':
uvicorn.run(app, host='0.0.0.0', port=9000, workers=1)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。