File size: 2,153 Bytes
cc91688
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77ed5d2
cc91688
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import openai
import time
import math

class Model:
    def make_text_prompt(self, text: str, target_language: str) -> str:
        # Create a text prompt for translation
        return f"Translate to {target_language}: {text}"

    def translate_prompt(self, content, target_language: str) -> str:
        # Generate a translation prompt based on the content type
        if isinstance(content, (str, list)):
            return self.make_text_prompt(content, target_language)

    def make_request(self, prompt):
        # Subclasses must implement this method to send the request
        raise NotImplementedError("Subclasses must implement the make_request method")


class OpenAIModel(Model):
    def __init__(self, model_name: str, api_key: str):
        self.model_name = model_name
        # openai.api_key = api_key

    def make_translation_request(self, prompt):
        attempts = 0
        while attempts < 3:
            try:
                if self.model_name == "gpt-3.5-turbo":
                    response = openai.ChatCompletion.create(
                        model=self.model_name,
                        messages=[
                            {"role": "user", "content": prompt}
                        ]
                    )
                    translation = response.choices[0].message['content'].strip()
                else:
                    response = openai.ChatCompletion.create(
                        model=self.model_name,
                        prompt=prompt,
                        max_tokens=150,
                        temperature=0
                    )
                    translation = response.choices[0].text.strip()

                return translation, True
            except openai.error.RateLimitError:
                attempts += 1
                if attempts < 3:
                    wait_time = math.pow(2, attempts)  # exponential backoff
                    print("Rate limit reached. Waiting for 60 seconds before retrying.")
                    time.sleep(60)
                else:
                    raise Exception("Rate limit reached. Maximum attempts exceeded.")
        return "", False