-
Notifications
You must be signed in to change notification settings - Fork 558
chore(logging): Log model name and base URL before invoking LLMs #1465
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
JashG
wants to merge
3
commits into
NVIDIA-NeMo:develop
Choose a base branch
from
JashG:jgulabrai/add-llm-invocation-logging
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,79 @@ | ||||||
| # SPDX-FileCopyrightText: Copyright (c) 2023-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||||||
| # SPDX-License-Identifier: Apache-2.0 | ||||||
| # | ||||||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||||||
| # you may not use this file except in compliance with the License. | ||||||
| # You may obtain a copy of the License at | ||||||
| # | ||||||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||||||
| # | ||||||
| # Unless required by applicable law or agreed to in writing, software | ||||||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||||||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||||
| # See the License for the specific language governing permissions and | ||||||
| # limitations under the License. | ||||||
|
|
||||||
| import logging | ||||||
| import re | ||||||
| from typing import Any, Dict, Optional | ||||||
|
|
||||||
| log = logging.getLogger(__name__) | ||||||
|
|
||||||
|
|
||||||
| def extract_model_name_and_base_url( | ||||||
| serialized: Dict[str, Any] | ||||||
| ) -> tuple[Optional[str], Optional[str]]: | ||||||
| """Extract model name and base URL from serialized LLM parameters. | ||||||
| Args: | ||||||
| serialized: The serialized LLM configuration | ||||||
| Returns: | ||||||
| A tuple of (model_name, base_url). Either value can be None if not found | ||||||
| """ | ||||||
| model_name = None | ||||||
| base_url = None | ||||||
|
|
||||||
| # Case 1: Try to extract from kwargs (we expect kwargs to be populated for the `ChatOpenAI` class). | ||||||
| if "kwargs" in serialized: | ||||||
| kwargs = serialized["kwargs"] | ||||||
|
|
||||||
| # Check for model_name in kwargs (ChatOpenAI attribute) | ||||||
| if "model_name" in kwargs and kwargs["model_name"]: | ||||||
| model_name = str(kwargs["model_name"]) | ||||||
|
|
||||||
| # Check for openai_api_base in kwargs (ChatOpenAI attribute) | ||||||
| if "openai_api_base" in kwargs and kwargs["openai_api_base"]: | ||||||
| base_url = str(kwargs["openai_api_base"]) | ||||||
|
|
||||||
| # Case 2: For other providers, parse `repr`, a string representation of the provider class. We don't have | ||||||
| # a reference to the actual class, so we need to parse the string representation. | ||||||
| if "repr" in serialized and isinstance(serialized["repr"], str): | ||||||
| repr_str = serialized["repr"] | ||||||
|
|
||||||
| # Extract model name. We expect the property to be formatted like model='...' or model_name='...', | ||||||
| # and check for single and double quotes. | ||||||
| if not model_name: | ||||||
| match = re.search(r"model(?:_name)?=['\"]([^'\"]+)['\"]", repr_str) | ||||||
| if match: | ||||||
| model_name = match.group(1) | ||||||
|
|
||||||
| # Extract base URL. The propety name may vary between providers, so try common attribute patterns. | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. syntax: Typo: "propety" should be "property"
Suggested change
|
||||||
| # We expect the property to be formatted like property_name='...', and check for single and double quotes. | ||||||
| if not base_url: | ||||||
| url_attrs = [ | ||||||
| "api_base", | ||||||
| "api_host", | ||||||
| "azure_endpoint", | ||||||
| "base_url", | ||||||
| "endpoint", | ||||||
| "endpoint_url", | ||||||
| "openai_api_base", | ||||||
| ] | ||||||
| for attr in url_attrs: | ||||||
| match = re.search(rf"{attr}=['\"]([^'\"]+)['\"]", repr_str) | ||||||
| if match: | ||||||
| base_url = match.group(1) | ||||||
| break | ||||||
|
|
||||||
| return model_name, base_url | ||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
style: The exact same logging logic appears in both
on_llm_start(lines 68-75) andon_chat_model_start(lines 118-125). Extract to a helper method to reduce duplication and improve maintainability.