@@ -9,7 +9,7 @@ export const createChatCompletion = (options: {
9
9
} ) =>
10
10
new Promise < string > ( ( resolve ) => {
11
11
let combinedResult = '' ;
12
- const error = '发生错误:' ;
12
+ let error = '发生错误:' ;
13
13
const config = getChatGPTConfig ( ) ;
14
14
const request = https . request (
15
15
{
@@ -27,6 +27,7 @@ export const createChatCompletion = (options: {
27
27
res . on ( 'data' , async ( chunk ) => {
28
28
const text = new TextDecoder ( 'utf-8' ) . decode ( chunk ) ;
29
29
const data = text . split ( '\n\n' ) . filter ( ( s ) => s ) ;
30
+ console . log ( data ) ;
30
31
for ( let i = 0 ; i < data . length ; i ++ ) {
31
32
try {
32
33
let element = data [ i ] ;
@@ -43,7 +44,7 @@ export const createChatCompletion = (options: {
43
44
// 处理没有 data 开头
44
45
element = preDataLast + element ;
45
46
}
46
- if ( element . includes ( 'data: ' ) ) {
47
+ if ( element . startsWith ( 'data: ' ) ) {
47
48
if ( element . includes ( '[DONE]' ) ) {
48
49
if ( options . handleChunk ) {
49
50
options . handleChunk ( { text : '' } ) ;
@@ -69,13 +70,18 @@ export const createChatCompletion = (options: {
69
70
}
70
71
} else {
71
72
console . log ( 'no includes data: ' , element ) ;
73
+ if ( options . handleChunk ) {
74
+ options . handleChunk ( {
75
+ text : element ,
76
+ } ) ;
77
+ }
72
78
}
73
79
} catch ( e ) {
74
80
console . error ( {
75
81
e : ( e as Error ) . toString ( ) ,
76
82
element : data [ i ] ,
77
83
} ) ;
78
- // error = (e as Error).toString();
84
+ error = ( e as Error ) . toString ( ) ;
79
85
}
80
86
}
81
87
} ) ;
0 commit comments