File size: 1,705 Bytes
55f3e5a
 
 
3e8b0ee
 
 
55f3e5a
 
 
 
3e8b0ee
 
 
 
 
 
 
55f3e5a
 
 
 
 
3e8b0ee
55f3e5a
 
 
 
 
 
3e8b0ee
 
 
55f3e5a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3e8b0ee
 
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
from modules.presets import COMPLETION_URL, BALANCE_API_URL, USAGE_API_URL, API_HOST
import os
import queue

class State:
    interrupted = False
    multi_api_key = False
    completion_url = COMPLETION_URL
    balance_api_url = BALANCE_API_URL
    usage_api_url = USAGE_API_URL

    def interrupt(self):
        self.interrupted = True

    def recover(self):
        self.interrupted = False

    def set_api_host(self, api_host):
        self.completion_url = f"https://{api_host}/v1/chat/completions"
        self.balance_api_url = f"https://{api_host}/dashboard/billing/credit_grants"
        self.usage_api_url = f"https://{api_host}/dashboard/billing/usage"
        os.environ["OPENAI_API_BASE"] = f"https://{api_host}/v1"

    def reset_api_host(self):
        self.completion_url = COMPLETION_URL
        self.balance_api_url = BALANCE_API_URL
        self.usage_api_url = USAGE_API_URL
        os.environ["OPENAI_API_BASE"] = f"https://{API_HOST}/v1"
        return API_HOST

    def reset_all(self):
        self.interrupted = False
        self.completion_url = COMPLETION_URL
    
    def set_api_key_queue(self, api_key_list):
        self.multi_api_key = True
        self.api_key_queue = queue.Queue()
        for api_key in api_key_list:
            self.api_key_queue.put(api_key)

    def switching_api_key(self, func):
        if not hasattr(self, "api_key_queue"):
            return func
        
        def wrapped(*args, **kwargs):
            api_key = self.api_key_queue.get()
            args = list(args)[1:]
            ret = func(api_key, *args, **kwargs)
            self.api_key_queue.put(api_key)
            return ret

        return wrapped
        

state = State()