File size: 14,614 Bytes
d803be1
bb1d601
d803be1
 
 
 
 
 
 
 
 
 
 
bb1d601
 
 
d803be1
 
 
 
bb1d601
 
d803be1
 
 
 
 
 
bb1d601
 
d803be1
bb1d601
7406911
 
 
 
bb1d601
7402de3
bb1d601
 
 
 
7402de3
d803be1
bb1d601
 
 
 
 
7402de3
bb1d601
 
 
 
7406911
bb1d601
 
 
 
 
 
 
 
 
7406911
 
 
 
 
 
 
 
 
 
bb1d601
 
7406911
bb1d601
 
 
7406911
bb1d601
 
 
 
7406911
bb1d601
 
 
7406911
bb1d601
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7406911
bb1d601
 
 
7406911
bb1d601
 
 
7406911
bb1d601
 
 
7406911
bb1d601
 
 
7406911
bb1d601
 
 
 
 
d803be1
 
 
 
bb1d601
7406911
 
 
 
7402de3
bb1d601
d803be1
 
6f80de5
 
d803be1
 
7406911
d803be1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bb1d601
 
 
 
 
 
 
 
d803be1
 
 
 
6f80de5
d803be1
 
 
6f80de5
d803be1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bb1d601
d803be1
bb1d601
 
 
 
 
 
 
d803be1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7402de3
d803be1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
import os
from typing import Tuple, Optional
from langchain.prompts.prompt import PromptTemplate
from langchain.retrievers.multi_query import MultiQueryRetriever

from langchain_aws import BedrockEmbeddings
from langchain_aws.chat_models.bedrock_converse import ChatBedrockConverse
from langchain_cohere import ChatCohere
from langchain_fireworks.chat_models import ChatFireworks
from langchain_fireworks.embeddings import FireworksEmbeddings
from langchain_groq.chat_models import ChatGroq
from langchain_openai import ChatOpenAI
from langchain_openai.embeddings import OpenAIEmbeddings
from langchain_anthropic.chat_models import ChatAnthropic
from langchain_mistralai.chat_models import ChatMistralAI
from langchain_mistralai.embeddings import MistralAIEmbeddings
from langchain_ollama.chat_models import ChatOllama
from langchain_ollama.embeddings import OllamaEmbeddings
from langchain_cohere.embeddings import CohereEmbeddings
from langchain_cohere.chat_models import ChatCohere
from langchain_huggingface import ChatHuggingFace, HuggingFaceEndpoint
from langchain_community.embeddings import HuggingFaceInferenceAPIEmbeddings
from langchain_openai.embeddings import OpenAIEmbeddings
from langchain_google_genai import ChatGoogleGenerativeAI
from langchain_google_genai.embeddings import GoogleGenerativeAIEmbeddings
from langchain_community.chat_models import ChatPerplexity
from langchain_together import ChatTogether
from langchain_together.embeddings import TogetherEmbeddings
from langchain.chat_models.base import BaseChatModel
from langchain.embeddings.base import Embeddings

def split_provider_model(provider_model: str) -> Tuple[str, Optional[str]]:
    """
    Split the provider and model name from a string.
    returns Tuple[str, Optional[str]]
    """
    parts = provider_model.split(":", 1)
    provider = parts[0]
    if len(parts) > 1:
        model = parts[1] if parts[1] else None
    else:
        model = None
    return provider, model

def get_model(provider_model: str, temperature: float = 0.7) -> BaseChatModel:
    """
    Get a model from a provider and model name.
    returns BaseChatModel
    """
    provider, model = split_provider_model(provider_model)
    try:
        match provider.lower():
            case 'anthropic':
                if model is None:
                    model = "claude-3-5-haiku-20241022"
                chat_llm = ChatAnthropic(model=model, temperature=temperature)
            case 'bedrock':
                if model is None:
                    model = "us.anthropic.claude-3-5-haiku-20241022-v1:0"
                chat_llm = ChatBedrockConverse(model=model, temperature=temperature)
            case 'cohere':
                if model is None:
                    model = 'command-r-plus'
                chat_llm = ChatCohere(model=model, temperature=temperature)

            case 'deepseek':
                if model is None:
                    model='deepseek-chat'
                chat_llm = ChatOpenAI(
                    model=model, 
                    openai_api_key=os.getenv("DEEPSEEK_API_KEY"), 
                    openai_api_base='https://api.deepseek.com',
                    max_tokens=8192
                )
            case 'fireworks':
                if model is None:
                    model = 'accounts/fireworks/models/llama-v3p3-70b-instruct'
                chat_llm = ChatFireworks(model_name=model, temperature=temperature, max_tokens=120000)
            case 'googlegenerativeai':
                if model is None:
                    model = "gemini-2.0-flash-exp"
                chat_llm = ChatGoogleGenerativeAI(model=model, temperature=temperature, 
                                                  max_tokens=None, timeout=None, max_retries=2,)
            case 'groq':
                if model is None:
                    model = 'qwen-2.5-32b'
                chat_llm = ChatGroq(model_name=model, temperature=temperature)
            case 'huggingface' | 'hf':
                if model is None:
                    model = 'Qwen/Qwen2.5-72B-Instruct'
                llm = HuggingFaceEndpoint(
                    repo_id=model,
                    temperature=temperature,
                )
                chat_llm = ChatHuggingFace(llm=llm)
            case 'ollama':
                if model is None:
                    model = 'llama3.1'
                chat_llm = ChatOllama(model=model, temperature=temperature)
            case 'openai':
                if model is None:
                    model = "gpt-4o-mini"
                chat_llm = ChatOpenAI(model=model, temperature=temperature)
            case 'openrouter':
                if model is None:
                    model = "cognitivecomputations/dolphin3.0-mistral-24b:free"
                chat_llm = ChatOpenAI(model=model, temperature=temperature, base_url="https://openrouter.ai/api/v1", api_key=os.getenv("OPENROUTER_API_KEY"))
            case 'mistralai' | 'mistral':
                if model is None:
                    model = "mistral-small-latest"
                chat_llm = ChatMistralAI(model=model, temperature=temperature)
            case 'perplexity':
                if model is None:
                    model = 'sonar'
                chat_llm = ChatPerplexity(model=model, temperature=temperature)
            case 'together':
                if model is None:
                    model = 'meta-llama/Llama-3.3-70B-Instruct-Turbo-Free'
                chat_llm = ChatTogether(model=model, temperature=temperature)
            case 'xai':
                if model is None:
                    model = 'grok-2-1212'
                chat_llm = ChatOpenAI(model=model,api_key=os.getenv("XAI_API_KEY"), base_url="https://api.x.ai/v1", temperature=temperature)
            case _:
                raise ValueError(f"Unknown LLM provider {provider}")
    except Exception as e:
        raise ValueError(f"Unexpected error with {provider}: {str(e)}")
    
    return chat_llm


def get_embedding_model(provider_model: str) -> Embeddings:
    """
    Get an embedding model from a provider and model name.
    returns Embeddings
    """
    provider, model = split_provider_model(provider_model)
    match provider.lower():
        case 'bedrock':
            if model is None:
                model = "amazon.titan-embed-text-v2:0"
            embedding_model = BedrockEmbeddings(model_id=model)
        case 'cohere':
            if model is None:
                model = "embed-multilingual-v3.0"
            embedding_model = CohereEmbeddings(model=model)
        case 'fireworks':
            if model is None:
                model = 'nomic-ai/nomic-embed-text-v1.5'
            embedding_model = FireworksEmbeddings(model=model)
        case 'ollama':
            if model is None:
                model = 'nomic-embed-text:latest'
            embedding_model = OllamaEmbeddings(model=model)
        case 'openai':
            if model is None:
                model = "text-embedding-3-small"
            embedding_model = OpenAIEmbeddings(model=model)
        case 'googlegenerativeai':
            if model is None:
                model = "models/embedding-001"
            embedding_model = GoogleGenerativeAIEmbeddings(model=model)
        case 'groq':
            embedding_model = OpenAIEmbeddings(model="text-embedding-3-small")
        case 'huggingface' | 'hf':
            if model is None:
                model = 'sentence-transformers/all-MiniLM-L6-v2'
            embedding_model = HuggingFaceInferenceAPIEmbeddings(model_name=model, api_key=os.getenv("HUGGINGFACE_API_KEY"))
        case 'mistral':
            if model is None:
                model = "mistral-embed"
            embedding_model = MistralAIEmbeddings(model=model)
        case 'perplexity':
            raise ValueError(f"Cannot use Perplexity for embedding model")
        case 'together':
            if model is None:
                model = 'togethercomputer/m2-bert-80M-2k-retrieval'
            embedding_model = TogetherEmbeddings(model=model)
        case _:
            raise ValueError(f"Unknown LLM provider {provider}")

    return embedding_model


import unittest
from unittest.mock import patch
from models import get_embedding_model  # Make sure this import is correct

class TestGetEmbeddingModel(unittest.TestCase):

    @patch('models.BedrockEmbeddings')
    def test_bedrock_embedding(self, mock_bedrock):
        result = get_embedding_model('bedrock')
        mock_bedrock.assert_called_once_with(model_id='cohere.embed-multilingual-v3')
        self.assertEqual(result, mock_bedrock.return_value)

    @patch('models.CohereEmbeddings')
    def test_cohere_embedding(self, mock_cohere):
        result = get_embedding_model('cohere')
        mock_cohere.assert_called_once_with(model='embed-english-light-v3.0')
        self.assertEqual(result, mock_cohere.return_value)

    @patch('models.FireworksEmbeddings')
    def test_fireworks_embedding(self, mock_fireworks):
        result = get_embedding_model('fireworks')
        mock_fireworks.assert_called_once_with(model='nomic-ai/nomic-embed-text-v1.5')
        self.assertEqual(result, mock_fireworks.return_value)

    @patch('models.OllamaEmbeddings')
    def test_ollama_embedding(self, mock_ollama):
        result = get_embedding_model('ollama')
        mock_ollama.assert_called_once_with(model='nomic-embed-text:latest')
        self.assertEqual(result, mock_ollama.return_value)

    @patch('models.OpenAIEmbeddings')
    def test_openai_embedding(self, mock_openai):
        result = get_embedding_model('openai')
        mock_openai.assert_called_once_with(model='text-embedding-3-small')
        self.assertEqual(result, mock_openai.return_value)

    @patch('models.GoogleGenerativeAIEmbeddings')
    def test_google_embedding(self, mock_google):
        result = get_embedding_model('googlegenerativeai')
        mock_google.assert_called_once_with(model='models/embedding-001')
        self.assertEqual(result, mock_google.return_value)

    @patch('models.TogetherEmbeddings')
    def test_together_embedding(self, mock_together):
        result = get_embedding_model('together')
        mock_together.assert_called_once_with(model='BAAI/bge-base-en-v1.5')
        self.assertEqual(result, mock_together.return_value)

    def test_invalid_provider(self):
        with self.assertRaises(ValueError):
            get_embedding_model('invalid_provider')

    def test_groq_provider(self):
        with self.assertRaises(ValueError):
            get_embedding_model('groq')

    def test_perplexity_provider(self):
        with self.assertRaises(ValueError):
            get_embedding_model('perplexity')


import unittest
from unittest.mock import patch
from models import get_model  # Make sure this import is correct

class TestGetModel(unittest.TestCase):

    @patch('models.ChatBedrockConverse')
    def test_bedrock_model_no_specific_model(self, mock_bedrock):
        result = get_model('bedrock')
        mock_bedrock.assert_called_once_with(model=None, temperature=0.0)
        self.assertEqual(result, mock_bedrock.return_value)

    @patch('models.ChatBedrockConverse')
    def test_bedrock_model_with_specific_model(self, mock_bedrock):
        result = get_model('bedrock:specific-model')
        mock_bedrock.assert_called_once_with(model='specific-model', temperature=0.0)
        self.assertEqual(result, mock_bedrock.return_value)

    @patch('models.ChatCohere')
    def test_cohere_model(self, mock_cohere):
        result = get_model('cohere')
        mock_cohere.assert_called_once_with(model='command-r-plus', temperature=0.0)
        self.assertEqual(result, mock_cohere.return_value)

    @patch('models.ChatFireworks')
    def test_fireworks_model(self, mock_fireworks):
        result = get_model('fireworks')
        mock_fireworks.assert_called_once_with(
            model_name='accounts/fireworks/models/llama-v3p1-8b-instruct',
            temperature=0.0,
            max_tokens=120000
        )
        self.assertEqual(result, mock_fireworks.return_value)

    @patch('models.ChatGoogleGenerativeAI')
    def test_google_model(self, mock_google):
        result = get_model('googlegenerativeai')
        mock_google.assert_called_once_with(
            model="gemini-1.5-pro",
            temperature=0.0,
            max_tokens=None,
            timeout=None,
            max_retries=2
        )
        self.assertEqual(result, mock_google.return_value)

    @patch('models.ChatGroq')
    def test_groq_model(self, mock_groq):
        result = get_model('groq')
        mock_groq.assert_called_once_with(model_name='llama-3.1-8b-instant', temperature=0.0)
        self.assertEqual(result, mock_groq.return_value)

    @patch('models.ChatOllama')
    def test_ollama_model(self, mock_ollama):
        result = get_model('ollama')
        mock_ollama.assert_called_once_with(model='llama3.1', temperature=0.0)
        self.assertEqual(result, mock_ollama.return_value)

    @patch('models.ChatOpenAI')
    def test_openai_model(self, mock_openai):
        result = get_model('openai')
        mock_openai.assert_called_once_with(model_name='gpt-4o-mini', temperature=0.0)
        self.assertEqual(result, mock_openai.return_value)

    @patch('models.ChatPerplexity')
    def test_perplexity_model(self, mock_perplexity):
        result = get_model('perplexity')
        mock_perplexity.assert_called_once_with(model='llama-3.1-sonar-small-128k-online', temperature=0.0)
        self.assertEqual(result, mock_perplexity.return_value)

    @patch('models.ChatTogether')
    def test_together_model(self, mock_together):
        result = get_model('together')
        mock_together.assert_called_once_with(model='meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo', temperature=0.0)
        self.assertEqual(result, mock_together.return_value)

    def test_invalid_provider(self):
        with self.assertRaises(ValueError):
            get_model('invalid_provider')

    def test_custom_temperature(self):
        with patch('models.ChatOpenAI') as mock_openai:
            result = get_model('openai', temperature=0.5)
            mock_openai.assert_called_once_with(model_name='gpt-4o-mini', temperature=0.5)
            self.assertEqual(result, mock_openai.return_value)

    def test_custom_model(self):
        with patch('models.ChatOpenAI') as mock_openai:
            result = get_model('openai/gpt-4')
            mock_openai.assert_called_once_with(model_name='gpt-4', temperature=0.0)
            self.assertEqual(result, mock_openai.return_value)

if __name__ == '__main__':
    unittest.main()