forked from NVIDIA/workbench-example-hybrid-rag
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__main__.py
132 lines (114 loc) · 3.81 KB
/
__main__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# SPDX-FileCopyrightText: Copyright (c) 2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Entrypoint for the Conversation GUI.
The functions in this module are responsible for bootstrapping then executing the Conversation GUI server.
"""
import argparse
import os
import sys
import uvicorn
def parse_args() -> argparse.Namespace:
"""Parse command-line arguments for the program.
:returns: A namespace containing the parsed arguments.
:rtype: argparse.Namespace
"""
parser = argparse.ArgumentParser(description="Document Retrieval Service")
parser.add_argument(
"--help-config",
action="store_true",
default=False,
help="show the configuration help text",
)
parser.add_argument(
"-c",
"--config",
metavar="CONFIGURATION_FILE",
default="/dev/null",
help="path to the configuration file (json or yaml)",
)
parser.add_argument(
"-v",
"--verbose",
action="count",
default=1,
help="increase output verbosity",
)
parser.add_argument(
"-q",
"--quiet",
action="count",
default=0,
help="decrease output verbosity",
)
parser.add_argument(
"--host",
metavar="HOSTNAME",
type=str,
default="0.0.0.0", # nosec # this is intentional
help="Bind socket to this host.",
)
parser.add_argument(
"--port",
metavar="PORT_NUM",
type=int,
default=8080,
help="Bind socket to this port.",
)
parser.add_argument(
"--workers",
metavar="NUM_WORKERS",
type=int,
default=1,
help="Number of worker processes.",
)
parser.add_argument(
"--ssl-keyfile", metavar="SSL_KEY", type=str, default=None, help="SSL key file"
)
parser.add_argument(
"--ssl-certfile",
metavar="SSL_CERT",
type=str,
default=None,
help="SSL certificate file",
)
cliargs = parser.parse_args()
if cliargs.help_config:
# pylint: disable=import-outside-toplevel; this is intentional to allow for the environment to be configured
# before any of the application libraries are loaded.
from chatui.configuration import AppConfig
sys.stdout.write("\nconfiguration file format:\n")
AppConfig.print_help(sys.stdout.write)
sys.exit(0)
return cliargs
if __name__ == "__main__":
args = parse_args()
os.environ["APP_VERBOSITY"] = f"{args.verbose - args.quiet}"
os.environ["APP_CONFIG_FILE"] = args.config
from chatui import api, chat_client, configuration, pages
# load config
config_file = os.environ.get("APP_CONFIG_FILE", "/dev/null")
config = configuration.AppConfig.from_file(config_file)
if not config:
sys.exit(1)
# connect to other services
api_url = f"{config.server_url}:{config.server_port}"
print(api_url)
client = chat_client.ChatClient(
api_url, config.model_name
)
proxy_prefix = os.environ.get("PROXY_PREFIX")
blocks = pages.converse.build_page(client)
blocks.queue(max_size=10)
blocks.launch(server_name="0.0.0.0", server_port=8080, root_path=proxy_prefix)