Skip to content

Commit 759405c

Browse files
committed
Fix issue with Literal and Optional cli arguments not working. Closes #702
1 parent 6cfc542 commit 759405c

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

llama_cpp/server/__main__.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,27 @@
2323
"""
2424
import os
2525
import argparse
26+
from typing import Literal, Union
2627

2728
import uvicorn
2829

2930
from llama_cpp.server.app import create_app, Settings
3031

32+
def get_non_none_base_types(annotation):
33+
if not hasattr(annotation, "__args__"):
34+
return annotation
35+
return [arg for arg in annotation.__args__ if arg is not type(None)][0]
36+
37+
def get_base_type(annotation):
38+
if getattr(annotation, '__origin__', None) is Literal:
39+
return type(annotation.__args__[0])
40+
elif getattr(annotation, '__origin__', None) is Union:
41+
non_optional_args = [arg for arg in annotation.__args__ if arg is not type(None)]
42+
if non_optional_args:
43+
return get_base_type(non_optional_args[0])
44+
else:
45+
return annotation
46+
3147
if __name__ == "__main__":
3248
parser = argparse.ArgumentParser()
3349
for name, field in Settings.model_fields.items():
@@ -37,7 +53,7 @@
3753
parser.add_argument(
3854
f"--{name}",
3955
dest=name,
40-
type=field.annotation if field.annotation is not None else str,
56+
type=get_base_type(field.annotation) if field.annotation is not None else str,
4157
help=description,
4258
)
4359

0 commit comments

Comments
 (0)