Skip to content

Commit 060bfa6

Browse files
committed
feat: Add support for yaml based configs
1 parent 1347e1d commit 060bfa6

File tree

4 files changed

+50
-2
lines changed

4 files changed

+50
-2
lines changed

examples/batch-processing/server.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
"""llama-cpp-python server from scratch in a single file.
2+
"""
3+
4+
# import llama_cpp
5+
6+
# path = b"../../models/Qwen1.5-0.5B-Chat-GGUF/qwen1_5-0_5b-chat-q8_0.gguf"
7+
8+
# model_params = llama_cpp.llama_model_default_params()
9+
# model = llama_cpp.llama_load_model_from_file(path, model_params)
10+
11+
# if model is None:
12+
# raise RuntimeError(f"Failed to load model from file: {path}")
13+
14+
15+
# ctx_params = llama_cpp.llama_context_default_params()
16+
# ctx = llama_cpp.llama_new_context_with_model(model, ctx_params)
17+
18+
# if ctx is None:
19+
# raise RuntimeError("Failed to create context")
20+
21+
22+
from fastapi import FastAPI
23+
24+
app = FastAPI()
25+
26+
import openai.types.chat as types
27+
28+
@app.post("/v1/chat/completions")
29+
def create_chat_completions():
30+
return {"message": "Hello World"}

llama_cpp/server/__main__.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,16 @@ def main():
5959
if not os.path.exists(config_file):
6060
raise ValueError(f"Config file {config_file} not found!")
6161
with open(config_file, "rb") as f:
62-
config_file_settings = ConfigFileSettings.model_validate_json(f.read())
62+
# Check if yaml file
63+
if config_file.endswith(".yaml") or config_file.endswith(".yml"):
64+
import yaml
65+
import json
66+
67+
config_file_settings = ConfigFileSettings.model_validate_json(
68+
json.dumps(yaml.safe_load(f))
69+
)
70+
else:
71+
config_file_settings = ConfigFileSettings.model_validate_json(f.read())
6372
server_settings = ServerSettings.model_validate(config_file_settings)
6473
model_settings = config_file_settings.models
6574
else:

llama_cpp/server/app.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,15 @@ def create_app(
9797
if not os.path.exists(config_file):
9898
raise ValueError(f"Config file {config_file} not found!")
9999
with open(config_file, "rb") as f:
100-
config_file_settings = ConfigFileSettings.model_validate_json(f.read())
100+
# Check if yaml file
101+
if config_file.endswith(".yaml") or config_file.endswith(".yml"):
102+
import yaml
103+
104+
config_file_settings = ConfigFileSettings.model_validate_json(
105+
json.dumps(yaml.safe_load(f))
106+
)
107+
else:
108+
config_file_settings = ConfigFileSettings.model_validate_json(f.read())
101109
server_settings = ServerSettings.model_validate(config_file_settings)
102110
model_settings = config_file_settings.models
103111

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ server = [
3535
"pydantic-settings>=2.0.1",
3636
"sse-starlette>=1.6.1",
3737
"starlette-context>=0.3.6,<0.4",
38+
"PyYAML>=5.1",
3839
]
3940
test = [
4041
"pytest>=7.4.0",

0 commit comments

Comments
 (0)