mirror of
https://github.com/NicolasBohn/NexQuant.git
synced 2026-07-28 16:07:46 +00:00
5090c6153f
* File structure for supporting litellm * more litellm support * feat: Add CachedAPIBackend class and dynamic API backend retrieval function * fix: update benchmark folder path and add default values for architecture and hyperparameters * feat: add LiteLLMAPIBackend and DeprecBackend ; changed structure of the project ; with bus * fix : deprec_backend * feat: Add LiteLLMAPIBackend class and related features; update configuration and test cases. * feat: Enhance LiteLLMAPIBackend with encoder support and dynamic argument handling;Enhance log Colors * lint * fix lint... * fix: Lint * fix:make auto-lint * fix:test oai * fix:redundant _abckend.py * fix: Optimize LiteLLMAPIBackend on token counting functiona, and clean up unused code;add test on this function * feat: Add LiteLLMSettings class and update model settings usage * fix: Update LiteLLMSettings environment variable prefix and model configurations * fix : gitignore * test: Consolidate and relocate test files for litellm backend and oai * fix : lint * fix: lint * auto lint * lint * LINT * lint * chore: remove deprecated backend configuration comments * refactor: Remove unused functions and imports from deprec.py and llm_utils.py * refactor: Move md5_hash function from deprec.py to llm_utils.py * chore: Remove extra newline and add missing import in deprec.py * lint * refactor: Move md5_hash function to utils module * lint * lint * lint --------- Co-authored-by: Young <afe.young@gmail.com> Co-authored-by: Yihua Chen <v-yihuachen@microsoft.com>
57 lines
2.1 KiB
Python
57 lines
2.1 KiB
Python
import json
|
|
from typing import Any, Callable, Type, TypeVar, Union, cast
|
|
|
|
from rdagent.core.exception import FormatError
|
|
from rdagent.log import rdagent_logger as logger
|
|
|
|
T = TypeVar("T")
|
|
|
|
|
|
def build_cls_from_json_with_retry(
|
|
cls: Type[T],
|
|
system_prompt: str,
|
|
user_prompt: str,
|
|
retry_n: int = 5,
|
|
init_kwargs_update_func: Callable[[dict[str, Any]], dict[str, Any]] | None = None,
|
|
**kwargs: dict,
|
|
) -> T:
|
|
"""
|
|
Parameters
|
|
----------
|
|
cls : Type[T]
|
|
The class type to be instantiated with the response data.
|
|
system_prompt : str
|
|
The initial prompt provided to the system for context.
|
|
user_prompt : str
|
|
The prompt given by the user to guide the response generation.
|
|
retry_n : int
|
|
The number of attempts to retry in case of failure.
|
|
init_kwargs_update_func : Union[Callable[[dict], dict], None]
|
|
A function that takes the initial keyword arguments as input and returns the updated keyword arguments.
|
|
This function can be used to modify the response data before it is used to instantiate the class.
|
|
|
|
**kwargs
|
|
Additional keyword arguments passed to the API call.
|
|
|
|
Returns
|
|
-------
|
|
T
|
|
An instance of the specified class type created from the response data.
|
|
"""
|
|
from rdagent.oai.llm_utils import APIBackend # avoid circular import
|
|
|
|
for i in range(retry_n):
|
|
# currently, it only handle exception caused by initial class
|
|
resp = APIBackend().build_messages_and_create_chat_completion(
|
|
user_prompt=user_prompt, system_prompt=system_prompt, json_mode=True, **kwargs # type: ignore[arg-type]
|
|
)
|
|
try:
|
|
resp_dict = json.loads(resp)
|
|
if init_kwargs_update_func:
|
|
resp_dict = init_kwargs_update_func(resp_dict)
|
|
return cls(**resp_dict)
|
|
except Exception as e:
|
|
logger.warning(f"Attempt {i + 1}: The previous attempt didn't work due to: {e}")
|
|
user_prompt = user_prompt + f"\n\nAttempt {i + 1}: The previous attempt didn't work due to: {e}"
|
|
raise FormatError("Unable to produce a JSON response that meets the specified requirements.")
|