File size: 2,749 Bytes
5ddfe7e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
from dotenv import load_dotenv
import openai


class OpenAiManager:
    def __init__(self):
        load_dotenv()
        openai.api_key = os.getenv("OPENAI_API_KEY")
        openai.organization = os.getenv("OPENAI_ORGANIZATION")

    def get_completion(
        self,
        prompt,
        model="text-davinci-003",
        max_tokens=128,
        temperature=0,
    ):
        response = None
        try:
            response = openai.Completion.create(
                prompt=prompt,
                max_tokens=max_tokens,
                model=model,
                temperature=temperature,
            )["choices"][0]["text"]

        except Exception as err:
            print(f"Sorry, There was a problem \n\n {err}")

        return response

    def get_chat_completion(self, prompt, model="gpt-3.5-turbo"):
        response = None
        try:
            response = (
                openai.ChatCompletion.create(
                    model=model,
                    messages=[
                        {
                            "role": "system",
                            "content": prompt,
                        }
                    ],
                )
                .choices[0]
                .message.content.strip()
            )

        except Exception as err:
            print(f"Sorry, There was a problem \n\n {err}")

        return response

    def get_embedding(self, prompt, model="text-embedding-ada-002"):
        prompt = prompt.replace("\n", " ")

        embedding = None
        try:
            embedding = openai.Embedding.create(input=[prompt], model=model)["data"][0][
                "embedding"
            ]

        except Exception as err:
            print(f"Sorry, There was a problem {err}")

        return embedding

    def get_embeddings(self, prompts, model="text-embedding-ada-002"):
        prompts = [prompt.replace("\n", " ") for prompt in prompts]

        embeddings = None
        try:
            embeddings = openai.Embedding.create(input=prompts, model=model)["data"]

        except Exception as err:
            print(f"Sorry, There was a problem {err}")

        return [embedding["embedding"] for embedding in embeddings]

    def shots(self, examples, keywords):
        prompt = []

        for example in examples:
            prompt.append(
                f"""
                keywords: {example.payload["keywords"]}
                script: {example.payload["example"]}
                """
            )

        prompt.append(
            f"""
            keywords: {keywords}
            script: 
            """
        )

        prompt = "\n\n".join(prompt)

        return self.get_chat_completion(prompt=prompt)


openai_manager = OpenAiManager()