Update networks/message_streamer.py
Browse files- networks/message_streamer.py +23 -38
 
    	
        networks/message_streamer.py
    CHANGED
    
    | 
         @@ -26,24 +26,15 @@ class MessageStreamer: 
     | 
|
| 26 | 
         
             
                        self.model = model
         
     | 
| 27 | 
         
             
                    else:
         
     | 
| 28 | 
         
             
                        self.model = "default"
         
     | 
| 29 | 
         
            -
             
     | 
| 30 | 
         
            -
             
     | 
| 31 | 
         
            -
                        self.stop_sequences = self.STOP_SEQUENCES_MAP.get(self.model, "")
         
     | 
| 32 | 
         | 
| 33 | 
         
             
                def parse_line(self, line):
         
     | 
| 34 | 
         
            -
                     
     | 
| 35 | 
         
            -
             
     | 
| 36 | 
         
            -
             
     | 
| 37 | 
         
            -
             
     | 
| 38 | 
         
            -
             
     | 
| 39 | 
         
            -
                        # Check if 'token' key exists and return its 'text' value
         
     | 
| 40 | 
         
            -
                        if 'token' in data:
         
     | 
| 41 | 
         
            -
                            return data['token'].get('text', '')  # Return an empty string if 'text' is not found
         
     | 
| 42 | 
         
            -
                        else:
         
     | 
| 43 | 
         
            -
                            return None  # Return None if 'token' key is not present
         
     | 
| 44 | 
         
            -
                    except Exception as e:
         
     | 
| 45 | 
         
            -
                        print(f"An error occurred: {e}")
         
     | 
| 46 | 
         
            -
                        return None
         
     | 
| 47 | 
         | 
| 48 | 
         
             
                def chat_response(
         
     | 
| 49 | 
         
             
                    self,
         
     | 
| 
         @@ -107,6 +98,7 @@ class MessageStreamer: 
     | 
|
| 107 | 
         
             
                    return stream_response
         
     | 
| 108 | 
         | 
| 109 | 
         
             
                def chat_return_dict(self, stream_response):
         
     | 
| 
         | 
|
| 110 | 
         
             
                    final_output = self.message_outputer.default_data.copy()
         
     | 
| 111 | 
         
             
                    final_output["choices"] = [
         
     | 
| 112 | 
         
             
                        {
         
     | 
| 
         @@ -118,28 +110,24 @@ class MessageStreamer: 
     | 
|
| 118 | 
         
             
                            },
         
     | 
| 119 | 
         
             
                        }
         
     | 
| 120 | 
         
             
                    ]
         
     | 
| 121 | 
         
            -
                
         
     | 
| 122 | 
         
             
                    logger.back(final_output)
         
     | 
| 123 | 
         
            -
             
     | 
| 124 | 
         
             
                    final_content = ""
         
     | 
| 125 | 
         
             
                    for line in stream_response.iter_lines():
         
     | 
| 126 | 
         
             
                        if not line:
         
     | 
| 127 | 
         
             
                            continue
         
     | 
| 128 | 
         
            -
                
         
     | 
| 129 | 
         
             
                        content = self.parse_line(line)
         
     | 
| 130 | 
         
            -
             
     | 
| 131 | 
         
            -
                         
     | 
| 132 | 
         
            -
             
     | 
| 133 | 
         
            -
                             
     | 
| 134 | 
         
            -
             
     | 
| 135 | 
         
            -
             
     | 
| 136 | 
         
            -
                             
     | 
| 137 | 
         
            -
             
     | 
| 138 | 
         
            -
                                final_content += content
         
     | 
| 139 | 
         
            -
                
         
     | 
| 140 | 
         
             
                    if self.model in self.STOP_SEQUENCES_MAP.keys():
         
     | 
| 141 | 
         
             
                        final_content = final_content.replace(self.stop_sequences, "")
         
     | 
| 142 | 
         
            -
             
     | 
| 143 | 
         
             
                    final_output["choices"][0]["message"]["content"] = final_content
         
     | 
| 144 | 
         
             
                    return final_output
         
     | 
| 145 | 
         | 
| 
         @@ -148,24 +136,21 @@ class MessageStreamer: 
     | 
|
| 148 | 
         
             
                    for line in stream_response.iter_lines():
         
     | 
| 149 | 
         
             
                        if not line:
         
     | 
| 150 | 
         
             
                            continue
         
     | 
| 151 | 
         
            -
             
     | 
| 152 | 
         
             
                        content = self.parse_line(line)
         
     | 
| 153 | 
         | 
| 154 | 
         
            -
                         
     | 
| 155 | 
         
            -
                
         
     | 
| 156 | 
         
            -
                        if content is not None and content.strip() == self.stop_sequences:
         
     | 
| 157 | 
         
             
                            content_type = "Finished"
         
     | 
| 158 | 
         
             
                            logger.success("\n[Finished]")
         
     | 
| 159 | 
         
             
                            is_finished = True
         
     | 
| 160 | 
         
             
                        else:
         
     | 
| 161 | 
         
             
                            content_type = "Completions"
         
     | 
| 162 | 
         
             
                            logger.back(content, end="")
         
     | 
| 163 | 
         
            -
             
     | 
| 164 | 
         
             
                        output = self.message_outputer.output(
         
     | 
| 165 | 
         
             
                            content=content, content_type=content_type
         
     | 
| 166 | 
         
             
                        )
         
     | 
| 167 | 
         
             
                        yield output
         
     | 
| 168 | 
         
            -
                
         
     | 
| 169 | 
         
            -
                    if not is_finished:
         
     | 
| 170 | 
         
            -
                        yield self.message_outputer.output(content="", content_type="Finished")
         
     | 
| 171 | 
         | 
| 
         | 
|
| 
         | 
| 
         | 
|
| 26 | 
         
             
                        self.model = model
         
     | 
| 27 | 
         
             
                    else:
         
     | 
| 28 | 
         
             
                        self.model = "default"
         
     | 
| 29 | 
         
            +
                    self.model_fullname = self.MODEL_MAP[self.model]
         
     | 
| 30 | 
         
            +
                    self.message_outputer = OpenaiStreamOutputer()
         
     | 
| 
         | 
|
| 31 | 
         | 
| 32 | 
         
             
                def parse_line(self, line):
         
     | 
| 33 | 
         
            +
                    line = line.decode("utf-8")
         
     | 
| 34 | 
         
            +
                    line = re.sub(r"data:\s*", "", line)
         
     | 
| 35 | 
         
            +
                    data = json.loads(line)
         
     | 
| 36 | 
         
            +
                    content = data["token"]["text"]
         
     | 
| 37 | 
         
            +
                    return content
         
     | 
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 38 | 
         | 
| 39 | 
         
             
                def chat_response(
         
     | 
| 40 | 
         
             
                    self,
         
     | 
| 
         | 
|
| 98 | 
         
             
                    return stream_response
         
     | 
| 99 | 
         | 
| 100 | 
         
             
                def chat_return_dict(self, stream_response):
         
     | 
| 101 | 
         
            +
                    # https://platform.openai.com/docs/guides/text-generation/chat-completions-response-format
         
     | 
| 102 | 
         
             
                    final_output = self.message_outputer.default_data.copy()
         
     | 
| 103 | 
         
             
                    final_output["choices"] = [
         
     | 
| 104 | 
         
             
                        {
         
     | 
| 
         | 
|
| 110 | 
         
             
                            },
         
     | 
| 111 | 
         
             
                        }
         
     | 
| 112 | 
         
             
                    ]
         
     | 
| 
         | 
|
| 113 | 
         
             
                    logger.back(final_output)
         
     | 
| 114 | 
         
            +
             
     | 
| 115 | 
         
             
                    final_content = ""
         
     | 
| 116 | 
         
             
                    for line in stream_response.iter_lines():
         
     | 
| 117 | 
         
             
                        if not line:
         
     | 
| 118 | 
         
             
                            continue
         
     | 
| 
         | 
|
| 119 | 
         
             
                        content = self.parse_line(line)
         
     | 
| 120 | 
         
            +
             
     | 
| 121 | 
         
            +
                        if content.strip() == self.stop_sequences:
         
     | 
| 122 | 
         
            +
                            logger.success("\n[Finished]")
         
     | 
| 123 | 
         
            +
                            break
         
     | 
| 124 | 
         
            +
                        else:
         
     | 
| 125 | 
         
            +
                            logger.back(content, end="")
         
     | 
| 126 | 
         
            +
                            final_content += content
         
     | 
| 127 | 
         
            +
             
     | 
| 
         | 
|
| 
         | 
|
| 128 | 
         
             
                    if self.model in self.STOP_SEQUENCES_MAP.keys():
         
     | 
| 129 | 
         
             
                        final_content = final_content.replace(self.stop_sequences, "")
         
     | 
| 130 | 
         
            +
             
     | 
| 131 | 
         
             
                    final_output["choices"][0]["message"]["content"] = final_content
         
     | 
| 132 | 
         
             
                    return final_output
         
     | 
| 133 | 
         | 
| 
         | 
|
| 136 | 
         
             
                    for line in stream_response.iter_lines():
         
     | 
| 137 | 
         
             
                        if not line:
         
     | 
| 138 | 
         
             
                            continue
         
     | 
| 139 | 
         
            +
             
     | 
| 140 | 
         
             
                        content = self.parse_line(line)
         
     | 
| 141 | 
         | 
| 142 | 
         
            +
                        if content.strip() == self.stop_sequences:
         
     | 
| 
         | 
|
| 
         | 
|
| 143 | 
         
             
                            content_type = "Finished"
         
     | 
| 144 | 
         
             
                            logger.success("\n[Finished]")
         
     | 
| 145 | 
         
             
                            is_finished = True
         
     | 
| 146 | 
         
             
                        else:
         
     | 
| 147 | 
         
             
                            content_type = "Completions"
         
     | 
| 148 | 
         
             
                            logger.back(content, end="")
         
     | 
| 149 | 
         
            +
             
     | 
| 150 | 
         
             
                        output = self.message_outputer.output(
         
     | 
| 151 | 
         
             
                            content=content, content_type=content_type
         
     | 
| 152 | 
         
             
                        )
         
     | 
| 153 | 
         
             
                        yield output
         
     | 
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 154 | 
         | 
| 155 | 
         
            +
                    if not is_finished:
         
     | 
| 156 | 
         
            +
                        yield self.message_outputer.output(content="", content_type="Finished")
         
     |