File size: 2,733 Bytes
5623f53
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from dotenv import load_dotenv
from openai.embeddings_utils import get_embeddings, aget_embeddings, get_embedding, aget_embedding
import openai
from typing import List
import os
import asyncio

class EmbeddingModel:
    """
    This class contains functionalities to generate embeddings from the 
    list of texts or text asynchronously or in sync.
    """
    def __init__(self, embeddings_model_name:str = "text-embedding-ada-002"):
        """
        Loads the OpenAI Api key and sets the embedding model
        """
        load_dotenv()
        self.openai_api_key = os.getenv("OPENAI_API_KEY")

        if self.openai_api_key is None:
            raise ValueError("OPENAI_API_KEY environment variables is not set. Please set it to your openAI API key")
        
        openai.api_key = self.openai_api_key
        self.embeddings_model_name = embeddings_model_name
    
    async def async_get_embeddings(self, list_of_text:List[str])->List[List[float]]:
        """
        This function takes in a list of strings and uses openai api 
        aget_embeddings to get the list of embeddings back. The process is asynchronous in nature

        """
        return await aget_embeddings(
            list_of_text = list_of_text, engine = self.embeddings_model_name
        )
    
    async def async_get_embedding(self, text: str) -> List[float]:
        """
        This function takes in a string and uses openai api 
        aget_embedding to get the list of embeddings back. The process is asynchronous in nature

        """
        return await aget_embedding(text=text, engine=self.embeddings_model_name)
    
    def get_embeddings(self, list_of_text: List[str]) -> List[List[float]]:
        """
        This function takes in a list of strings and uses openai api 
        get_embeddings to get the list of embeddings back. The process is synchronous in nature

        """
        return get_embeddings(
            list_of_text=list_of_text, engine=self.embeddings_model_name
        )

    def get_embedding(self, text: str) -> List[float]:
        """
        This function takes in a string and uses openai api 
        get_embedding to get the list of embeddings back. The process is synchronous in nature

        """
        return get_embedding(text=text, engine=self.embeddings_model_name)

if __name__ == "__main__":
    embedding_model = EmbeddingModel()
    print(embedding_model.get_embedding("Hello, world!"))
    print(embedding_model.get_embeddings(["Hello, world!", "Goodbye, world!"]))
    print(asyncio.run(embedding_model.async_get_embedding("Hello, world!")))
    print(
        asyncio.run(
            embedding_model.async_get_embeddings(["Hello, world!", "Goodbye, world!"])
        )
    )