File size: 6,893 Bytes
d28f1ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cd5f60b
 
795c4fd
 
 
cd5f60b
 
 
 
 
 
795c4fd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d28f1ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72289eb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d28f1ed
 
 
 
 
 
 
 
 
 
 
 
52214c6
 
 
 
 
 
 
 
 
 
 
 
 
 
883546f
 
 
 
 
 
 
d28f1ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52214c6
 
 
 
 
 
 
883546f
 
 
 
 
 
 
d28f1ed
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
"""LLM service - Factory for creating LLM instances."""
from typing import Optional
from langchain_openai import ChatOpenAI
from langchain_mistralai import ChatMistralAI
from langchain_core.language_models.chat_models import BaseChatModel
from domain.enums import ModelName, ModelProvider
from config import settings


class LLMService:
    """Service for managing LLM instances across different providers."""
    
    def __init__(self):
        """Initialize LLM service."""
        self._openai_api_key = settings.openai_api_key
        self._mistralai_api_key = settings.mistralai_api_key
    
    def get_llm(
        self,
        model_name: ModelName,
        temperature: float = 0.7,
        streaming: bool = False,
        max_tokens: Optional[int] = None
    ) -> BaseChatModel:
        """
        Factory method to create an LLM instance based on model name.
        
        Args:
            model_name: Model enum value
            temperature: Sampling temperature (0.0 to 2.0)
            streaming: Enable streaming mode
            max_tokens: Maximum tokens to generate
            
        Returns:
            LLM instance (ChatOpenAI or ChatMistralAI)
            
        Raises:
            ValueError: If model provider is unknown
        """
        provider = model_name.provider
        
        if provider == ModelProvider.OPENAI:
            return self._create_openai_llm(
                model_name=model_name.value,
                temperature=temperature,
                streaming=streaming,
                max_tokens=max_tokens
            )
        elif provider == ModelProvider.MISTRALAI:
            return self._create_mistralai_llm(
                model_name=model_name.value,
                temperature=temperature,
                streaming=streaming,
                max_tokens=max_tokens
            )
        else:
            raise ValueError(f"Unknown provider: {provider}")
    
    def _create_openai_llm(
        self,
        model_name: str,
        temperature: float,
        streaming: bool,
        max_tokens: Optional[int]
    ) -> ChatOpenAI:
        """Create OpenAI LLM instance.

        Some OpenAI models (e.g., `gpt-5`) have specific parameter requirements:
        - Only support default temperature (1.0)
        - Use 'max_completion_tokens' instead of 'max_tokens'
        """
        effective_temperature = temperature
        # Coerce to default temperature for models that don't allow custom values
        if model_name.startswith("gpt-5"):
            effective_temperature = 1.0

        # For gpt-5 models, use max_completion_tokens instead of max_tokens
        if model_name.startswith("gpt-5"):
            return ChatOpenAI(
                model=model_name,
                temperature=effective_temperature,
                streaming=streaming,
                max_completion_tokens=max_tokens,
                api_key=self._openai_api_key
            )
        else:
            return ChatOpenAI(
                model=model_name,
                temperature=effective_temperature,
                streaming=streaming,
                max_tokens=max_tokens,
                api_key=self._openai_api_key
            )
    
    def _create_mistralai_llm(
        self,
        model_name: str,
        temperature: float,
        streaming: bool,
        max_tokens: Optional[int]
    ) -> ChatMistralAI:
        """Create Mistral AI LLM instance."""
        return ChatMistralAI(
            model=model_name,
            temperature=temperature,
            streaming=streaming,
            max_tokens=max_tokens,
            mistral_api_key=self._mistralai_api_key
        )
    
    @staticmethod
    def supports_streaming(model_name: ModelName) -> bool:
        """
        Check if a model supports streaming.
        
        Args:
            model_name: Model enum value
            
        Returns:
            True if model supports streaming, False otherwise
        """
        models = LLMService.list_available_models()
        for model in models:
            if model["name"] == model_name.value:
                return model.get("supports_streaming", False)
        return False

    @staticmethod
    def list_available_models() -> list[dict]:
        """
        List all available models with their metadata.
        
        Returns:
            List of model information dictionaries
        """
        models = []
        
        # OpenAI models
        openai_models = [
            # {
            #     "name": ModelName.GPT_5.value,
            #     "provider": "openai",
            #     "description": "GPT-5",
            #     "supports_streaming": False,
            #     # "context_window": 128000
            # },
            # {
            #     "name": ModelName.GPT_5_CHAT.value,
            #     "provider": "openai",
            #     "description": "GPT-5 Chat",
            #     "supports_streaming": True,
            #     # "context_window": 128000
            # },
            # {
            #     "name": ModelName.GPT_4.value,
            #     "provider": "openai",
            #     "description": "GPT-4",
            #     "supports_streaming": True,
            #     # "context_window": 128000
            # },
            # {
            #     "name": ModelName.GPT_4_TURBO.value,
            #     "provider": "openai",
            #     "description": "GPT-4 Turbo - Fast and powerful",
            #     "supports_streaming": True,
            #     "context_window": 128000
            # },
            # {
            #     "name": ModelName.GPT_4.value,
            #     "provider": "openai",
            #     "description": "GPT-4 - High quality",
            #     "supports_streaming": True,
            #     "context_window": 8192
            # },
            # {
            #     "name": ModelName.GPT_35_TURBO.value,
            #     "provider": "openai",
            #     "description": "GPT-3.5 Turbo - Fast and efficient",
            #     "supports_streaming": True,
            #     "context_window": 16385
            # }
        ]
        
        # Mistral AI models
        mistral_models = [
            {
                "name": ModelName.MISTRAL_LARGE.value,
                "provider": "mistralai",
                "description": "Mistral Large",
                "supports_streaming": True,
                # "context_window": 32000
            },
            {
                "name": ModelName.MAGISTRAL_MEDIUM.value,
                "provider": "mistralai",
                "description": "Magistral Medium (reasonning)",
                "supports_streaming": True,
                # "context_window": 32000
            }
        ]
        
        models.extend(openai_models)
        models.extend(mistral_models)
        
        return models


# Singleton instance
llm_service = LLMService()