[Refactor]: Add LLMRegistry for llm services (#9589)

Co-authored-by: openhands <openhands@all-hands.dev>
Co-authored-by: Graham Neubig <neubig@gmail.com>
Co-authored-by: Engel Nyst <enyst@users.noreply.github.com>
This commit is contained in:
Rohit Malhotra
2025-08-18 02:11:20 -04:00
committed by GitHub
parent 17b1a21296
commit 25d9cf2890
84 changed files with 2376 additions and 817 deletions

View File

@@ -8,6 +8,7 @@ from typing import Any, Callable
import httpx
from openhands.core.config import LLMConfig
from openhands.llm.metrics import Metrics
with warnings.catch_warnings():
warnings.simplefilter('ignore')
@@ -34,7 +35,6 @@ from openhands.llm.fn_call_converter import (
convert_fncall_messages_to_non_fncall_messages,
convert_non_fncall_messages_to_fncall_messages,
)
from openhands.llm.metrics import Metrics
from openhands.llm.retry_mixin import RetryMixin
__all__ = ['LLM']
@@ -133,6 +133,7 @@ class LLM(RetryMixin, DebugMixin):
def __init__(
self,
config: LLMConfig,
service_id: str,
metrics: Metrics | None = None,
retry_listener: Callable[[int, int], None] | None = None,
) -> None:
@@ -145,11 +146,12 @@ class LLM(RetryMixin, DebugMixin):
metrics: The metrics to use.
"""
self._tried_model_info = False
self.cost_metric_supported: bool = True
self.config: LLMConfig = copy.deepcopy(config)
self.service_id = service_id
self.metrics: Metrics = (
metrics if metrics is not None else Metrics(model_name=config.model)
)
self.cost_metric_supported: bool = True
self.config: LLMConfig = copy.deepcopy(config)
self.model_info: ModelInfo | None = None
self.retry_listener = retry_listener
@@ -408,8 +410,7 @@ class LLM(RetryMixin, DebugMixin):
assert self.config.log_completions_folder is not None
log_file = os.path.join(
self.config.log_completions_folder,
# use the metric model name (for draft editor)
f'{self.metrics.model_name.replace("/", "__")}-{time.time()}.json',
f'{self.config.model.replace("/", "__")}-{time.time()}.json',
)
# set up the dict to be logged