添加启动API的参数,支持https、wss方式API调用 (#728)
* update README.md * 添加启动API的参数,支持https、wss方式API调用。(CA证书自备) 添加启动API服务的markdown说明文档。 --------- Co-authored-by: imClumsyPanda <littlepanda0716@gmail.com> Co-authored-by: 一帆 <zhang.f@digitalcnzz.com>
This commit is contained in:
parent
967ac2ed2b
commit
10abb8d781
10
api.py
10
api.py
|
|
@ -445,7 +445,7 @@ async def document():
|
|||
return RedirectResponse(url="/docs")
|
||||
|
||||
|
||||
def api_start(host, port):
|
||||
def api_start(host, port, **kwargs):
|
||||
global app
|
||||
global local_doc_qa
|
||||
|
||||
|
|
@ -494,15 +494,21 @@ def api_start(host, port):
|
|||
embedding_device=EMBEDDING_DEVICE,
|
||||
top_k=VECTOR_SEARCH_TOP_K,
|
||||
)
|
||||
if kwargs.get("ssl_keyfile") and kwargs.get("ssl_certfile"):
|
||||
uvicorn.run(app, host=host, port=port, ssl_keyfile=kwargs.get("ssl_keyfile"),
|
||||
ssl_certfile=kwargs.get("ssl_certfile"))
|
||||
else:
|
||||
uvicorn.run(app, host=host, port=port)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
parser.add_argument("--host", type=str, default="0.0.0.0")
|
||||
parser.add_argument("--port", type=int, default=7861)
|
||||
parser.add_argument("--ssl_keyfile", type=str)
|
||||
parser.add_argument("--ssl_certfile", type=str)
|
||||
# 初始化消息
|
||||
args = None
|
||||
args = parser.parse_args()
|
||||
args_dict = vars(args)
|
||||
shared.loaderCheckPoint = LoaderCheckPoint(args_dict)
|
||||
api_start(args.host, args.port)
|
||||
api_start(args.host, args.port, ssl_keyfile=args.ssl_keyfile, ssl_certfile=args.ssl_certfile)
|
||||
|
|
|
|||
6
cli.py
6
cli.py
|
|
@ -42,7 +42,9 @@ def start():
|
|||
@start.command(name="api", context_settings=dict(help_option_names=['-h', '--help']))
|
||||
@click.option('-i', '--ip', default='0.0.0.0', show_default=True, type=str, help='api_server listen address.')
|
||||
@click.option('-p', '--port', default=7861, show_default=True, type=int, help='api_server listen port.')
|
||||
def start_api(ip, port):
|
||||
@click.option('-k', '--ssl_keyfile', type=int, help='enable api https/wss service, specify the ssl keyfile path.')
|
||||
@click.option('-c', '--ssl_certfile', type=int, help='enable api https/wss service, specify the ssl certificate file path.')
|
||||
def start_api(ip, port, **kwargs):
|
||||
# 调用api_start之前需要先loadCheckPoint,并传入加载检查点的参数,
|
||||
# 理论上可以用click包进行包装,但过于繁琐,改动较大,
|
||||
# 此处仍用parser包,并以models.loader.args.DEFAULT_ARGS的参数为默认参数
|
||||
|
|
@ -51,7 +53,7 @@ def start_api(ip, port):
|
|||
from models.loader import LoaderCheckPoint
|
||||
from models.loader.args import DEFAULT_ARGS
|
||||
shared.loaderCheckPoint = LoaderCheckPoint(DEFAULT_ARGS)
|
||||
api_start(host=ip, port=port)
|
||||
api_start(host=ip, port=port, **kwargs)
|
||||
|
||||
# # 通过cli.py调用cli_demo时需要在cli.py里初始化模型,否则会报错:
|
||||
# langchain-ChatGLM: error: unrecognized arguments: start cli
|
||||
|
|
|
|||
|
|
@ -0,0 +1,37 @@
|
|||
# 启动API服务
|
||||
|
||||
## 通过py文件启动
|
||||
可以通过直接执行`api.py`文件启动API服务,默认以ip:0.0.0.0和port:7861启动http和ws服务。
|
||||
```shell
|
||||
python api.py
|
||||
```
|
||||
同时,启动时支持StartOption所列的模型加载参数,同时还支持IP和端口设置。
|
||||
```shell
|
||||
python api.py --model-name chatglm-6b-int8 --port 7862
|
||||
```
|
||||
|
||||
## 通过cli.bat/cli.sh启动
|
||||
也可以通过命令行控制文件继续启动。
|
||||
```shell
|
||||
cli.sh api --help
|
||||
```
|
||||
其他可设置参数和上述py文件启动方式相同。
|
||||
|
||||
|
||||
# 以https、wss启动API服务
|
||||
## 本地创建ssl相关证书文件
|
||||
如果没有正式签发的CA证书,可以[安装mkcert](https://github.com/FiloSottile/mkcert#installation)工具, 然后用如下指令生成本地CA证书:
|
||||
```shell
|
||||
mkcert -install
|
||||
mkcert api.example.com 47.123.123.123 localhost 127.0.0.1 ::1
|
||||
```
|
||||
默认回车保存在当前目录下,会有以生成指令第一个域名命名为前缀命名的两个pem文件。
|
||||
|
||||
附带两个文件参数启动即可。
|
||||
````shell
|
||||
python api --port 7862 --ssl_keyfile api.example.com+4-key.pem --ssl_certfile api.example.com+4.pem
|
||||
|
||||
./cli.sh api --port 7862 --ssl_keyfile api.example.com+4-key.pem --ssl_certfile api.example.com+4.pem
|
||||
````
|
||||
|
||||
此外可以通过前置Nginx转发实现类似效果,可另行查阅相关资料。
|
||||
Loading…
Reference in New Issue