matthoffner commited on
Commit
7741ec9
·
1 Parent(s): 02f25ac

Update utils/server/index.ts

Browse files
Files changed (1) hide show
  1. utils/server/index.ts +7 -28
utils/server/index.ts CHANGED
@@ -73,38 +73,19 @@ export const LLMStream = async (
73
 
74
  const stream = new ReadableStream({
75
  async start(controller) {
76
- const onParse = (event: any) => {
77
  if (event.type === 'event') {
78
  const data = event.data;
79
 
80
  try {
81
  const json = JSON.parse(data);
82
-
83
- // New condition to handle Python API format
84
- if (json.choices && json.choices[0].message) {
85
- const text = json.choices[0].message.content;
86
- const finishReason = json.choices[0].finish_reason;
87
-
88
- if (finishReason === 'stop') {
89
- controller.close();
90
- return;
91
- }
92
-
93
- const queue = encoder.encode(text);
94
- controller.enqueue(queue);
95
- }
96
- // Old condition to handle existing format
97
- else if (json.choices && json.choices[0].delta) {
98
- if (json.choices[0].finish_reason != null) {
99
- controller.close();
100
- return;
101
- }
102
-
103
- const text = json.choices[0].delta.content;
104
- const queue = encoder.encode(text);
105
- controller.enqueue(queue);
106
  }
107
-
 
 
108
  } catch (e) {
109
  controller.error(e);
110
  }
@@ -119,7 +100,5 @@ export const LLMStream = async (
119
  },
120
  });
121
 
122
-
123
-
124
  return stream;
125
  };
 
73
 
74
  const stream = new ReadableStream({
75
  async start(controller) {
76
+ const onParse = (event: ParsedEvent | ReconnectInterval) => {
77
  if (event.type === 'event') {
78
  const data = event.data;
79
 
80
  try {
81
  const json = JSON.parse(data);
82
+ if (json.choices[0].finish_reason != null) {
83
+ controller.close();
84
+ return;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85
  }
86
+ const text = json.choices[0].delta.content;
87
+ const queue = encoder.encode(text);
88
+ controller.enqueue(queue);
89
  } catch (e) {
90
  controller.error(e);
91
  }
 
100
  },
101
  });
102
 
 
 
103
  return stream;
104
  };