File size: 7,012 Bytes
44c5e78
 
10efc14
44c5e78
 
16c8074
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bb37148
44c5e78
 
16c8074
44c5e78
 
 
9c64311
16c8074
44c5e78
e125cf3
44c5e78
 
 
b977ac3
 
 
 
 
 
 
 
b4870cf
16c8074
44c5e78
 
b4870cf
e125cf3
 
b4870cf
e125cf3
 
d5b8bec
84cebc2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d5b8bec
 
84cebc2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d5b8bec
 
 
 
 
 
 
 
16c8074
d5b8bec
16c8074
 
 
 
d5b8bec
 
 
 
 
c05a7fe
44c5e78
 
 
d5b8bec
44c5e78
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84cebc2
 
 
 
 
 
 
 
 
 
 
 
44c5e78
84cebc2
 
 
 
44c5e78
84cebc2
 
44c5e78
84cebc2
 
44c5e78
 
 
d5b8bec
16c8074
b4870cf
44c5e78
 
 
 
 
 
 
 
 
 
 
e125cf3
44c5e78
 
 
 
 
 
e125cf3
44c5e78
e125cf3
44c5e78
 
 
 
 
 
 
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import random
import uuid
from conversations import ConversationStyle


class SystemPromptContextConstructor:
    # https://github.com/weaigc/bingo/blob/eaebba306d5f68b940e4486ad81897516d0db0f3/src/lib/bots/bing/index.ts#L205-L211
    # https://github.com/weaigc/bingo/blob/eaebba306d5f68b940e4486ad81897516d0db0f3/src/lib/bots/bing/index.ts#L296
    def __init__(self, system_prompt: str = None):
        self.system_prompt = system_prompt
        self.construct()

    def construct(self):
        if self.system_prompt:
            self.system_context = [
                {
                    "author": "user",
                    "description": self.system_prompt,
                    "contextType": "WebPage",
                    "messageType": "Context",
                    "messageId": "discover-web--page-ping-mriduna-----",
                }
            ]
        else:
            self.system_context = None


class ChathubRequestPayloadConstructor:
    def __init__(
        self,
        prompt: str,
        client_id: str,
        conversation_id: str,
        invocation_id: int = 0,
        conversation_style: str = ConversationStyle.PRECISE.value,
        system_prompt: str = None,
    ):
        self.prompt = prompt
        self.client_id = client_id
        self.conversation_id = conversation_id
        self.invocation_id = invocation_id
        self.conversation_style = conversation_style.lower()

        if self.conversation_style.endswith("offline"):
            self.enable_search = False
            self.conversation_style = self.conversation_style.replace("-offline", "")
        else:
            self.enable_search = True

        self.message_id = self.generate_random_uuid()
        self.system_prompt = system_prompt
        self.construct()

    def generate_random_uuid(self):
        return str(uuid.uuid4())

    def generate_random_hex_str(self, length: int = 32) -> str:
        return "".join(random.choice("0123456789abcdef") for _ in range(length))

    def set_options_sets(self):
        options_sets_body = [
            "nlu_direct_response_filter",
            "deepleo",
            "disable_emoji_spoken_text",
            "responsible_ai_policy_235",
            "enablemm",
            "dv3sugg",
            "autosave",
            "iyxapbing",
            "iycapbing",
            "rai289",
            "enflst",
            "enpcktrk",
            "rcaldictans",
            "rcaltimeans",
            "eredirecturl",
        ]

        options_sets_by_styles = {
            "precise": options_sets_body
            + [
                "h3precise",
                "clgalileo",
                "gencontentv3",
            ],
            "balanced": options_sets_body
            + [
                "galileo",
                "saharagenconv5",
            ],
            "creative": options_sets_body
            + [
                "h3imaginative",
                "clgalileo",
                "gencontentv3",
            ],
        }
        self.options_sets = options_sets_by_styles[self.conversation_style]

    def set_search_options(self):
        self.plugins = []
        if self.enable_search:
            self.plugins.append({"id": "c310c353-b9f0-4d76-ab0d-1dd5e979cf68"})
        else:
            self.options_sets.append("nosearchall")

    def set_system_context(self):
        self.system_context = SystemPromptContextConstructor(
            self.system_prompt
        ).system_context

    def construct(self):
        self.set_options_sets()
        self.set_search_options()
        self.set_system_context()

        self.request_payload = {
            "arguments": [
                {
                    "source": "cib",
                    "optionsSets": self.options_sets,
                    "allowedMessageTypes": [
                        "ActionRequest",
                        "Chat",
                        "ConfirmationCard",
                        "Context",
                        "InternalSearchQuery",
                        "InternalSearchResult",
                        "Disengaged",
                        "InternalLoaderMessage",
                        "InvokeAction",
                        "Progress",
                        "RenderCardRequest",
                        "RenderContentRequest",
                        "AdsQuery",
                        "SemanticSerp",
                        "GenerateContentQuery",
                        "SearchQuery",
                    ],
                    "sliceIds": [
                        "techpillscf",
                        "gbaa",
                        "gba",
                        "gbapa",
                        "codecreator",
                        "dlidcf",
                        "specedge",
                        "preall15",
                        "suppsm240-t",
                        "translref",
                        "ardsw_1_9_9",
                        "fluxnosearchc",
                        "fluxnosearch",
                        "1115rai289",
                        "1119backoss0",
                        "124multi2t",
                        "1129gpt4ts0",
                        "kchero50cf",
                        "cacfastapis",
                        "cacdupereccf",
                        "cacmuidarb",
                        "cacfrwebt2cf",
                        "sswebtop2cf",
                    ],
                    "verbosity": "verbose",
                    "scenario": "SERP",
                    "plugins": self.plugins,
                    "previousMessages": self.system_context,
                    "traceId": self.generate_random_hex_str(),
                    "conversationHistoryOptionsSets": [
                        "autosave",
                        "savemem",
                        "uprofupd",
                        "uprofgen",
                    ],
                    "isStartOfSession": self.invocation_id == 0,
                    "requestId": self.message_id,
                    "message": {
                        "author": "user",
                        "inputMethod": "Keyboard",
                        "text": self.prompt,
                        "messageType": "Chat",
                        "requestId": self.message_id,  # "a6ecd3aa-1007-6959-52fb-9e23f34e86be",
                        "messageId": self.message_id,  # "a6ecd3aa-1007-6959-52fb-9e23f34e86be",
                    },
                    "tone": self.conversation_style.capitalize(),
                    "spokenTextMode": "None",
                    "conversationId": self.conversation_id,  # "51D|BingProdUnAuthenticatedUsers|65761F31183134340AFD8F9AF1532EA90DC7F11ED348765DE9BAC956C9BA4669",
                    "participant": {
                        "id": self.client_id,  # "23EBCCB7073868D70172DF780674692D",
                    },
                }
            ],
            "invocationId": str(self.invocation_id),
            "target": "chat",
            "type": 4,
        }