| 1 | from langchain_openai import ChatOpenAI |
| 2 | |
| 3 | class LLMSingleton: |
| 4 | _instance = None |
| 5 | _default_model = "gpt-4o" |
| 6 | _alternate_model = "o1-preview" |
| 7 | |
| 8 | @classmethod |
| 9 | def get_instance(cls, model: str = None): |
| 10 | if model is None: |
| 11 | model = cls._default_model |
| 12 | |
| 13 | if cls._instance is None: |
| 14 | cls._instance = ChatOpenAI(model=model, temperature=1) |
| 15 | return cls._instance |
| 16 | |
| 17 | @classmethod |
| 18 | def set_default_model(cls, model: str): |
| 19 | """Set the default model to use when no specific model is requested""" |
| 20 | cls._default_model = model |
| 21 | cls._instance = None # Reset instance to force recreation with new model |
| 22 | |
| 23 | @classmethod |
| 24 | def revert_to_default_model(cls): |
| 25 | """Set the default model to use when no specific model is requested""" |
| 26 | print("Reverting to default model: ", cls._default_model, "Performance will be degraded as Integuru is using non O1 model") |
| 27 | cls._alternate_model = cls._default_model |
| 28 | |
| 29 | @classmethod |
| 30 | def switch_to_alternate_model(cls): |
| 31 | """Returns a ChatOpenAI instance configured for o1-miniss""" |
| 32 | # Create a new instance only if we don't have one yet |
| 33 | cls._instance = ChatOpenAI(model=cls._alternate_model, temperature=1) |
| 34 | |
| 35 | return cls._instance |
| 36 | |
| 37 | llm = LLMSingleton() |
| 38 | |