对于自然语言处理模型,构建API接口可以有效扩展其应用场景的支持范围。我们借助FastAPI搭建私有云服务,就能够通过接口调用获取到ChatGLM3模型的功能——其它外部程序可以发送自然语言查询请求,并快速获取精准的回复结果,从而实现ChatGLM模型与各类应用的无缝集成。
FastAPI是一种python框架,可以用来构建快速、高效的web应用程序。
作为一个异步框架,它支持使用sayncio库来处理高并发生成的大量请求,并且支持依赖注入、中间件等特性。
基于FastAPI开启服务的示例代码如下:
import uvicorn
from fastapi import FastAPI,Body
from fastapi.responses import JSONResponse
from typing import Dict
app = FastAPI()
from modelscope import AutoTokenizer,AutoModel,snapshot_download
model_dir = snapshot_download("ZhipuAI/chatglm3-6b",revision = "v1.0.0")
tokenizer = AutoTokenizer.from_pretrained(model_dit,trust_remote_code=True)
model = AutoModel.from_pretrained(model_dir,trust_remote_code=True).quantize(4).cuda()
@app.post("/chat")
def f1(data:Dict):
query = data["query"]
history = data["history"]
if history == "":
history = []
response,history = model.chat(tokenizer,query,history=history,top_p=0.95,temperature=0.95)
response = {"response":response,"history":history}
return JSONResponse(content=response)
if __name__ == "__main__":
uvicorn.run(app,host='127.0.0.1',port=7866)
通过上述代码,可以基于本地127.0.0.1:7866启动服务程序,支持信息接收并返回处理结果。
发送和接收相应的query,可以基于以下示例代码:
import requests
import json
#from ChatGLM3.basic_demo.web_demo_streamlit import response
data = {"query":"早上好", "history":""}
encoded_data = json.dumps(data)
response = requests.post("http://127.0.0.1:7866/chat",data=encoded_data).json()
response_chat = response["response"];history = response["history"]
print(response_chat)
结果有一点莫名其妙,但是能成功: