File size: 5,657 Bytes
44c5e78
 
 
 
bb37148
 
 
82b42e7
 
bb37148
10efc14
bb37148
dd37946
44c5e78
 
b716bb2
a14ad21
 
 
 
 
 
 
 
 
 
 
44c5e78
 
9c64311
21f117b
 
 
 
44c5e78
 
9c64311
 
 
 
 
10efc14
 
 
9c64311
10efc14
44c5e78
 
 
 
 
 
b4870cf
 
 
 
 
 
8d91150
b4870cf
44c5e78
6500788
21f117b
 
 
 
 
3a1b095
bb37148
1f691c1
3a1b095
 
bb37148
dcbfe03
3a1b095
 
 
16c8074
bb37148
5095a2b
e85c831
5095a2b
 
 
16c8074
5095a2b
bb37148
3a1b095
5095a2b
16c8074
 
 
6500788
16c8074
63998ab
e4f31be
 
 
 
8d91150
 
44c5e78
 
 
 
 
 
 
 
a2e9c80
44c5e78
4c9b469
0fdaf32
 
 
 
 
b286bf5
 
4c9b469
 
e85c831
44c5e78
 
1bbf682
 
 
a2e9c80
44c5e78
4c9b469
 
c05a7fe
8d91150
 
4c9b469
63998ab
 
 
44c5e78
e85c831
4b7ddb7
 
82cb440
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
import aiohttp
import json
import urllib

from networks import (
    ChathubRequestPayloadConstructor,
    ConversationRequestHeadersConstructor,
    MessageParser,
    OpenaiStreamOutputer,
)
from conversations import ConversationStyle
from utils.logger import logger
from utils.enver import enver


class ConversationConnector:
    """
    Input params:

    - `sec_access_token`, `client_id`, `conversation_id`
        - Generated by `ConversationCreator`
    - `invocation_id` (int):
      - For 1st request, this value must be `0`.
      - For all requests after, any integer is valid.
      - To make it simple, use `1` for all requests after the 1st one.
    """

    def __init__(
        self,
        conversation_style: str = ConversationStyle.PRECISE.value,
        sec_access_token: str = "",
        client_id: str = "",
        conversation_id: str = "",
        invocation_id: int = 0,
        cookies={},
    ):
        conversation_style_enum_values = [
            style.value for style in ConversationStyle.__members__.values()
        ]

        if conversation_style.lower() not in conversation_style_enum_values:
            self.conversation_style = ConversationStyle.PRECISE.value
        else:
            self.conversation_style = conversation_style.lower()
        print(f"Model: [{self.conversation_style}]")

        self.sec_access_token = sec_access_token
        self.client_id = client_id
        self.conversation_id = conversation_id
        self.invocation_id = invocation_id
        self.cookies = cookies

    async def wss_send(self, message):
        serialized_websocket_message = json.dumps(message, ensure_ascii=False) + "\x1e"
        await self.wss.send_str(serialized_websocket_message)

    async def init_handshake(self):
        await self.wss_send({"protocol": "json", "version": 1})
        await self.wss.receive_str()
        await self.wss_send({"type": 6})

    async def connect(self):
        self.quotelized_sec_access_token = urllib.parse.quote(self.sec_access_token)
        self.ws_url = (
            f"wss://sydney.bing.com/sydney/ChatHub"
            f"?sec_access_token={self.quotelized_sec_access_token}"
        )
        self.aiohttp_session = aiohttp.ClientSession(cookies=self.cookies)
        headers_constructor = ConversationRequestHeadersConstructor()
        enver.set_envs(proxies=True)
        self.wss = await self.aiohttp_session.ws_connect(
            self.ws_url,
            headers=headers_constructor.request_headers,
            proxy=enver.proxy,
        )
        await self.init_handshake()

    async def send_chathub_request(self, prompt: str, system_prompt: str = None):
        payload_constructor = ChathubRequestPayloadConstructor(
            prompt=prompt,
            conversation_style=self.conversation_style,
            client_id=self.client_id,
            conversation_id=self.conversation_id,
            invocation_id=self.invocation_id,
            system_prompt=system_prompt,
        )
        self.connect_request_payload = payload_constructor.request_payload
        await self.wss_send(self.connect_request_payload)

    async def stream_chat(
        self, prompt: str = "", system_prompt: str = None, yield_output=False
    ):
        await self.connect()
        await self.send_chathub_request(prompt=prompt, system_prompt=system_prompt)
        message_parser = MessageParser(outputer=OpenaiStreamOutputer())
        has_output_role_message = False
        if yield_output and not has_output_role_message:
            has_output_role_message = True
            yield message_parser.outputer.output(content="", content_type="Role")
        while not self.wss.closed:
            response_lines_str = await self.wss.receive_str()
            if isinstance(response_lines_str, str):
                response_lines = response_lines_str.split("\x1e")
            else:
                continue
            for line in response_lines:
                if not line:
                    continue
                data = json.loads(line)
                # Stream: Meaningful Messages
                if data.get("type") == 1:
                    if yield_output:
                        output = message_parser.parse(data, return_output=True)
                        if isinstance(output, list):
                            for item in output:
                                yield item
                        else:
                            if output:
                                yield output
                    else:
                        message_parser.parse(data)
                # Stream: List of all messages in the whole conversation
                elif data.get("type") == 2:
                    if data.get("item"):
                        # item = data.get("item")
                        # logger.note("\n[Saving chat messages ...]")
                        pass
                # Stream: End of Conversation
                elif data.get("type") == 3:
                    finished_str = "\n[Finished]"
                    logger.success(finished_str)
                    self.invocation_id += 1
                    await self.wss.close()
                    await self.aiohttp_session.close()
                    if yield_output:
                        yield message_parser.outputer.output(
                            content=finished_str, content_type="Finished"
                        )
                    break
                # Stream: Heartbeat Signal
                elif data.get("type") == 6:
                    continue
                # Stream: Not Implemented
                else:
                    continue