Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions llama_cpp/server/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,8 @@ def create_app(
)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_origins=server_settings.cors_allow_origins,
allow_credentials="*" not in server_settings.cors_allow_origins,
allow_methods=["*"],
allow_headers=["*"],
)
Expand Down
7 changes: 4 additions & 3 deletions llama_cpp/server/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,9 +198,10 @@ def load_llama_from_model_settings(settings: ModelSettings) -> llama_cpp.Llama:
assert (
settings.hf_tokenizer_config_path is not None
), "hf_tokenizer_config_path must be set for hf-tokenizer-config"
chat_handler = llama_cpp.llama_chat_format.hf_tokenizer_config_to_chat_completion_handler(
json.load(open(settings.hf_tokenizer_config_path))
)
with open(settings.hf_tokenizer_config_path) as f:
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When opening JSON files, it's highly recommended to explicitly specify encoding="utf-8". Without it, open() uses the platform's default encoding (e.g., cp1252 on Windows), which will cause UnicodeDecodeError or corrupted data when reading tokenizer configurations that contain special or non-ASCII characters.

            with open(settings.hf_tokenizer_config_path, encoding="utf-8") as f:

Fix it with Roo Code or mention @roomote and request a fix.

chat_handler = llama_cpp.llama_chat_format.hf_tokenizer_config_to_chat_completion_handler(
json.load(f)
)

tokenizer: Optional[llama_cpp.BaseLlamaTokenizer] = None
if settings.hf_pretrained_model_name_or_path is not None:
Expand Down
4 changes: 4 additions & 0 deletions llama_cpp/server/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,10 @@ class ServerSettings(BaseSettings):
default=None,
description="API key for authentication. If set all requests need to be authenticated.",
)
cors_allow_origins: List[str] = Field(
default=["*"],
description="Allowed CORS origins. Use ['*'] to allow all origins (not recommended for production).",
)
interrupt_requests: bool = Field(
default=True,
description="Whether to interrupt requests when a new request is received.",
Expand Down
Loading