File size: 2,070 Bytes
79ebcf2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
from langchain_openai import ChatOpenAI
from langchain_community.embeddings import HuggingFaceEmbeddings
from langchain_together import Together
from langchain_community.embeddings import HuggingFaceInferenceAPIEmbeddings

from dotenv import load_dotenv

load_dotenv()

class PersonalModels:
    def get_chat_model(self, 
                       model: str = 'mistralai/Mistral-7B-Instruct-v0.2', 
                       api_key: str = os.environ.get('TOGETHER_API_KEY', ''), 
                       max_tokens: int = 1000, 
                       temperature: float = 0.0, 
                       verbose: bool = True) -> ChatOpenAI:
        llm = ChatOpenAI(
                        api_key=api_key,
                        base_url="https://api.together.xyz/v1",
                        model=model,
                        max_tokens=max_tokens,
                        temperature=temperature,
                        verbose=verbose
                        )
        return llm

    def get_embedding_model(self, 
                            model: str = 'intfloat/multilingual-e5-large') -> HuggingFaceInferenceAPIEmbeddings:
        embeddings = HuggingFaceInferenceAPIEmbeddings(api_key=os.environ.get('HUGGINGFACEHUB_API_TOKEN', ''), model_name=model)
        # embeddings = HuggingFaceEmbeddings(model_name=model,model_kwargs = {'device': 'cuda'})
        # print('modelo está na gpu?',next(embeddings.model.parameters()).is_cuda)
        return embeddings

    def get_rag_model(self, 
                      model: str = 'mistralai/Mistral-7B-Instruct-v0.2', 
                      api_key: str = os.environ.get('TOGETHER_API_KEY', ''), 
                      max_tokens: int = 1000, 
                      temperature: float = 0.0, 
                      top_k: int = 20, 
                      verbose: bool = True) -> Together:
        llm = Together(
        model=model,
        temperature=temperature,
        max_tokens=max_tokens,
        top_k=top_k,
        together_api_key=api_key,
        verbose=verbose
        )
        return llm