Hansimov commited on
Commit
4b79bca
1 Parent(s): 8a35d38

:recycle: [Refactor] ChatCompletionRequester: structurize codes of post and process stream data

Browse files
components/buttons_binder.js CHANGED
@@ -109,8 +109,7 @@ class SendUserInputButtonBinder {
109
  });
110
  this.requester.create_messager_components();
111
  start_latest_message_animation();
112
- let requester_post = this.requester.post();
113
- requester_post.then(() => {
114
  this.stop(button);
115
  });
116
  }
 
109
  });
110
  this.requester.create_messager_components();
111
  start_latest_message_animation();
112
+ this.requester.post().then(() => {
 
113
  this.stop(button);
114
  });
115
  }
components/chat_operator.js CHANGED
@@ -85,17 +85,17 @@ export function pop_messager(n = 2) {
85
  return get_active_messager_list().pop(n);
86
  }
87
 
88
- export function update_message(json_chunks, content_displayer = null) {
89
- let content_displayer_updater = new ContentDisplayerUpdater(
90
- content_displayer
91
- );
92
  json_chunks.forEach(function (item) {
93
  let choice = item.choices[0];
94
  let content = choice.delta.content;
95
  let role = choice.delta.role;
96
  let finish_reason = choice.finish_reason;
97
  if (role) {
98
- console.log("role: " + role);
99
  }
100
  if (content) {
101
  content_displayer_updater.update_with_chunk_content(content);
 
85
  return get_active_messager_list().pop(n);
86
  }
87
 
88
+ export function update_message(json_chunks, content_displayer_updater = null) {
89
+ if (content_displayer_updater === null) {
90
+ content_displayer_updater = new ContentDisplayerUpdater();
91
+ }
92
  json_chunks.forEach(function (item) {
93
  let choice = item.choices[0];
94
  let content = choice.delta.content;
95
  let role = choice.delta.role;
96
  let finish_reason = choice.finish_reason;
97
  if (role) {
98
+ // console.log("role: " + role);
99
  }
100
  if (content) {
101
  content_displayer_updater.update_with_chunk_content(content);
networks/llm_requester.js CHANGED
@@ -8,6 +8,7 @@ import {
8
  get_request_messages,
9
  get_selected_llm_model,
10
  get_selected_temperature,
 
11
  } from "../components/chat_operator.js";
12
 
13
  export class ChatCompletionsRequester {
@@ -72,28 +73,28 @@ export class ChatCompletionsRequester {
72
  create_messager("user", this.prompt);
73
  create_messager("assistant", "", this.model, this.temperature);
74
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75
  async post() {
76
  this.construct_request_params();
77
- const response = await fetch(
78
  this.backend_request_endpoint,
79
  this.backend_request_params
80
  );
81
- const reader = response.body.getReader();
82
- let buffer = "";
83
- return reader.read().then(function process({ done, value }) {
84
- if (done) {
85
- return;
86
- }
87
- buffer += stringify_stream_bytes(value);
88
- let boundary = buffer.lastIndexOf("\n");
89
- if (boundary !== -1) {
90
- let input = buffer.substring(0, boundary);
91
- buffer = buffer.substring(boundary + 1);
92
- let json_chunks = jsonize_stream_data(input);
93
- update_message(json_chunks);
94
- }
95
- return reader.read().then(process);
96
- });
97
  }
98
  stop() {
99
  this.controller.abort();
 
8
  get_request_messages,
9
  get_selected_llm_model,
10
  get_selected_temperature,
11
+ ContentDisplayerUpdater,
12
  } from "../components/chat_operator.js";
13
 
14
  export class ChatCompletionsRequester {
 
73
  create_messager("user", this.prompt);
74
  create_messager("assistant", "", this.model, this.temperature);
75
  }
76
+ async handle_read_stream_data(reader) {
77
+ while (true) {
78
+ const { done, value } = await reader.read();
79
+ if (done) {
80
+ break;
81
+ }
82
+ let input = stringify_stream_bytes(value);
83
+ let json_chunks = jsonize_stream_data(input);
84
+ if (!this.content_displayer_updater) {
85
+ this.content_displayer_updater = new ContentDisplayerUpdater();
86
+ }
87
+ update_message(json_chunks, this.content_displayer_updater);
88
+ }
89
+ }
90
  async post() {
91
  this.construct_request_params();
92
+ let response = await fetch(
93
  this.backend_request_endpoint,
94
  this.backend_request_params
95
  );
96
+ let reader = response.body.getReader();
97
+ return this.handle_read_stream_data(reader);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
98
  }
99
  stop() {
100
  this.controller.abort();