Hansimov commited on
Commit
4107531
1 Parent(s): 3ae76f6

:gem: [Feature] ChatAPIApp: argparser for dev, and hide schema for some routes

Browse files
Files changed (1) hide show
  1. apis/chat_api.py +44 -1
apis/chat_api.py CHANGED
@@ -1,3 +1,5 @@
 
 
1
  import uvicorn
2
  from fastapi import FastAPI
3
  from pydantic import BaseModel, Field
@@ -124,23 +126,64 @@ class ChatAPIApp:
124
 
125
  def setup_routes(self):
126
  for prefix in ["", "/v1", "/api", "/api/v1"]:
 
127
  self.app.get(
128
  prefix + "/models",
129
  summary="Get available models",
 
130
  )(self.get_available_models)
131
 
132
  self.app.post(
133
  prefix + "/create",
134
  summary="Create a conversation session",
 
135
  )(self.create_conversation_session)
136
 
137
  self.app.post(
138
  prefix + "/chat/completions",
139
  summary="Chat completions in conversation session",
 
140
  )(self.chat_completions)
141
 
142
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
143
  app = ChatAPIApp().app
144
 
145
  if __name__ == "__main__":
146
- uvicorn.run("__main__:app", host="0.0.0.0", port=22222, reload=True)
 
 
 
 
 
 
 
 
1
+ import argparse
2
+ import sys
3
  import uvicorn
4
  from fastapi import FastAPI
5
  from pydantic import BaseModel, Field
 
126
 
127
  def setup_routes(self):
128
  for prefix in ["", "/v1", "/api", "/api/v1"]:
129
+ include_in_schema = True if prefix == "" else False
130
  self.app.get(
131
  prefix + "/models",
132
  summary="Get available models",
133
+ include_in_schema=include_in_schema,
134
  )(self.get_available_models)
135
 
136
  self.app.post(
137
  prefix + "/create",
138
  summary="Create a conversation session",
139
+ include_in_schema=include_in_schema,
140
  )(self.create_conversation_session)
141
 
142
  self.app.post(
143
  prefix + "/chat/completions",
144
  summary="Chat completions in conversation session",
145
+ include_in_schema=include_in_schema,
146
  )(self.chat_completions)
147
 
148
 
149
+ class ArgParser(argparse.ArgumentParser):
150
+ def __init__(self, *args, **kwargs):
151
+ super(ArgParser, self).__init__(*args, **kwargs)
152
+
153
+ self.add_argument(
154
+ "-s",
155
+ "--server",
156
+ type=str,
157
+ default="0.0.0.0",
158
+ help="Server IP for Bing Chat API",
159
+ )
160
+ self.add_argument(
161
+ "-p",
162
+ "--port",
163
+ type=int,
164
+ default=22222,
165
+ help="Server Port for Bing Chat API",
166
+ )
167
+
168
+ self.add_argument(
169
+ "-d",
170
+ "--dev",
171
+ default=False,
172
+ action="store_true",
173
+ help="Run in dev mode",
174
+ )
175
+
176
+ self.args = self.parse_args(sys.argv[1:])
177
+
178
+
179
  app = ChatAPIApp().app
180
 
181
  if __name__ == "__main__":
182
+ args = ArgParser().args
183
+ if args.dev:
184
+ uvicorn.run("__main__:app", host=args.server, port=args.port, reload=True)
185
+ else:
186
+ uvicorn.run("__main__:app", host=args.server, port=args.port, reload=False)
187
+
188
+ # python -m apis.chat_api # [Docker] on product mode
189
+ # python -m apis.chat_api -d # [Dev] on develop mode