Make API Key OpenAI-compatible (#917)

This commit is contained in:
Ying Sheng
2024-08-04 13:35:44 -07:00
committed by GitHub
parent afd411d09f
commit 0d4f3a9fcd
7 changed files with 115 additions and 125 deletions

View File

@@ -391,7 +391,11 @@ def get_call_select(args: argparse.Namespace):
def popen_launch_server(
model: str, base_url: str, timeout: float, other_args: tuple = ()
model: str,
base_url: str,
timeout: float,
api_key: Optional[str] = None,
other_args: tuple = (),
):
_, host, port = base_url.split(":")
host = host[2:]
@@ -408,12 +412,19 @@ def popen_launch_server(
port,
*other_args,
]
if api_key:
command += ["--api-key", api_key]
process = subprocess.Popen(command, stdout=None, stderr=None)
start_time = time.time()
while time.time() - start_time < timeout:
try:
response = requests.get(f"{base_url}/v1/models")
headers = {
"Content-Type": "application/json; charset=utf-8",
"Authorization": f"Bearer {api_key}",
}
response = requests.get(f"{base_url}/v1/models", headers=headers)
if response.status_code == 200:
return process
except requests.RequestException: