summaryrefslogtreecommitdiffstats
path: root/g4f/.v1/unfinished/t3nsor/__init__.py
blob: 9b588e982231638b8aafb5491e28fb0236f133e0 (plain) (blame)
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
from time import time

from requests import post

headers = {
    'authority': 'www.t3nsor.tech',
    'accept': '*/*',
    'accept-language': 'en,fr-FR;q=0.9,fr;q=0.8,es-ES;q=0.7,es;q=0.6,en-US;q=0.5,am;q=0.4,de;q=0.3',
    'cache-control': 'no-cache',
    'content-type': 'application/json',
    'origin': 'https://www.t3nsor.tech',
    'pragma': 'no-cache',
    'referer': 'https://www.t3nsor.tech/',
    'sec-ch-ua': '"Chromium";v="112", "Google Chrome";v="112", "Not:A-Brand";v="99"',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': '"macOS"',
    'sec-fetch-dest': 'empty',
    'sec-fetch-mode': 'cors',
    'sec-fetch-site': 'same-origin',
    'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36',
}


class T3nsorResponse:
    class Completion:
        class Choices:
            def __init__(self, choice: dict) -> None:
                self.text = choice['text']
                self.content = self.text.encode()
                self.index = choice['index']
                self.logprobs = choice['logprobs']
                self.finish_reason = choice['finish_reason']

            def __repr__(self) -> str:
                return f'''<__main__.APIResponse.Completion.Choices(\n    text           = {self.text.encode()},\n    index          = {self.index},\n    logprobs       = {self.logprobs},\n    finish_reason  = {self.finish_reason})object at 0x1337>'''

        def __init__(self, choices: dict) -> None:
            self.choices = [self.Choices(choice) for choice in choices]

    class Usage:
        def __init__(self, usage_dict: dict) -> None:
            self.prompt_tokens = usage_dict['prompt_chars']
            self.completion_tokens = usage_dict['completion_chars']
            self.total_tokens = usage_dict['total_chars']

        def __repr__(self):
            return f'''<__main__.APIResponse.Usage(\n    prompt_tokens      = {self.prompt_tokens},\n    completion_tokens  = {self.completion_tokens},\n    total_tokens       = {self.total_tokens})object at 0x1337>'''

    def __init__(self, response_dict: dict) -> None:
        self.response_dict = response_dict
        self.id = response_dict['id']
        self.object = response_dict['object']
        self.created = response_dict['created']
        self.model = response_dict['model']
        self.completion = self.Completion(response_dict['choices'])
        self.usage = self.Usage(response_dict['usage'])

    def json(self) -> dict:
        return self.response_dict


class Completion:
    model = {
        'model': {
            'id': 'gpt-3.5-turbo',
            'name': 'Default (GPT-3.5)'
        }
    }

    def create(
            prompt: str = 'hello world',
            messages: list = []) -> T3nsorResponse:
        response = post('https://www.t3nsor.tech/api/chat', headers=headers, json=Completion.model | {
            'messages': messages,
            'key': '',
            'prompt': prompt
        })

        return T3nsorResponse({
            'id': f'cmpl-1337-{int(time())}',
            'object': 'text_completion',
            'created': int(time()),
            'model': Completion.model,
            'choices': [{
                'text': response.text,
                'index': 0,
                'logprobs': None,
                'finish_reason': 'stop'
            }],
            'usage': {
                'prompt_chars': len(prompt),
                'completion_chars': len(response.text),
                'total_chars': len(prompt) + len(response.text)
            }
        })


class StreamCompletion:
    model = {
        'model': {
            'id': 'gpt-3.5-turbo',
            'name': 'Default (GPT-3.5)'
        }
    }

    def create(
            prompt: str = 'hello world',
            messages: list = []) -> T3nsorResponse:
        print('t3nsor api is down, this may not work, refer to another module')

        response = post('https://www.t3nsor.tech/api/chat', headers=headers, stream=True, json=Completion.model | {
            'messages': messages,
            'key': '',
            'prompt': prompt
        })

        for chunk in response.iter_content(chunk_size=2046):
            yield T3nsorResponse({
                'id': f'cmpl-1337-{int(time())}',
                'object': 'text_completion',
                'created': int(time()),
                'model': Completion.model,

                'choices': [{
                    'text': chunk.decode(),
                    'index': 0,
                    'logprobs': None,
                    'finish_reason': 'stop'
                }],

                'usage': {
                    'prompt_chars': len(prompt),
                    'completion_chars': len(chunk.decode()),
                    'total_chars': len(prompt) + len(chunk.decode())
                }
            })