File size: 11,361 Bytes
d803be1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7402de3
 
 
 
 
d803be1
 
7402de3
d803be1
 
 
 
6f80de5
d803be1
 
 
 
 
 
 
 
 
 
6f80de5
d803be1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7402de3
 
d803be1
 
 
6f80de5
 
d803be1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6f80de5
d803be1
 
 
6f80de5
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
import os
import json
from langchain.schema import SystemMessage, HumanMessage
from langchain.prompts.chat import (
    HumanMessagePromptTemplate,
    SystemMessagePromptTemplate,
    ChatPromptTemplate
)
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_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_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

def split_provider_model(provider_model):
    parts = provider_model.split(':', 1)
    provider = parts[0]
    model = parts[1] if len(parts) > 1 else None
    return provider, model

def get_model(provider_model, temperature=0.0):
    provider, model = split_provider_model(provider_model)
    match provider:
        case 'bedrock':
            if model is None:
                model = "anthropic.claude-3-sonnet-20240229-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 'fireworks':
            if model is None:
                model = 'accounts/fireworks/models/llama-v3p1-8b-instruct'
            chat_llm = ChatFireworks(model_name=model, temperature=temperature, max_tokens=120000)
        case 'googlegenerativeai':
            if model is None:
                model = "gemini-1.5-flash"
            chat_llm = ChatGoogleGenerativeAI(model=model, temperature=temperature, 
                                              max_tokens=None, timeout=None, max_retries=2,)
        case 'groq':
            if model is None:
                model = 'llama-3.1-8b-instant'
            chat_llm = ChatGroq(model_name=model, temperature=temperature)
        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_name=model, temperature=temperature)
        case 'perplexity':
            if model is None:
                model = 'llama-3.1-sonar-small-128k-online'
            chat_llm = ChatPerplexity(model=model, temperature=temperature)
        case 'together':
            if model is None:
                model = 'meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo'
            chat_llm = ChatTogether(model=model, temperature=temperature)
        case _:
            raise ValueError(f"Unknown LLM provider {provider}")
    
    return chat_llm


def get_embedding_model(provider_model):
    provider, model = split_provider_model(provider_model)
    match provider:
        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-english-light-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 '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(self, mock_bedrock):
        result = get_model('bedrock')
        mock_bedrock.assert_called_once_with(
            model="anthropic.claude-3-sonnet-20240229-v1:0",
            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()