Files
OpenHands/opendevin/core/utils/singleton.py
Boxuan Li c68478f470 Customize LLM config per agent (#2756)
Currently, OpenDevin uses a global singleton LLM config and a global singleton agent config. This PR allows customers to configure an LLM config for each agent. A hypothetically useful scenario is to use a cheaper LLM for repo exploration / code search, and a more powerful LLM to actually do the problem solving (CodeActAgent).

Partially solves #2075 (web GUI improvement is not the goal of this PR)
2024-07-09 22:05:54 -07:00

31 lines
1.3 KiB
Python

import dataclasses
class Singleton(type):
_instances: dict = {}
def __call__(cls, *args, **kwargs):
if cls not in cls._instances:
cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)
else:
# allow updates, just update existing instance
# perhaps not the most orthodox way to do it, though it simplifies client code
# useful for pre-defined groups of settings
instance = cls._instances[cls]
for key, value in kwargs.items():
setattr(instance, key, value)
return cls._instances[cls]
@classmethod
def reset(cls):
# used by pytest to reset the state of the singleton instances
for instance_type, instance in cls._instances.items():
print('resetting... ', instance_type)
for field_info in dataclasses.fields(instance_type):
if dataclasses.is_dataclass(field_info.type):
setattr(instance, field_info.name, field_info.type())
elif field_info.default_factory is not dataclasses.MISSING:
setattr(instance, field_info.name, field_info.default_factory())
else:
setattr(instance, field_info.name, field_info.default)