fix: submit completion by fetch

This commit is contained in:
jialin
2024-06-24 19:14:42 +08:00
parent 4dcd11e119
commit bd6b3dcbe3
5 changed files with 164 additions and 39 deletions
+40
View File
@@ -8,3 +8,43 @@ export async function execChatCompletions(params: any) {
data: params
});
}
export const fetchChatStream = async (params: any) => {
const response = await fetch(`/v1${CHAT_API}`, {
method: 'POST',
body: JSON.stringify(params),
headers: {
'Content-Type': 'application/json'
}
});
if (!response.ok) {
throw new Error('Network response was not ok');
return null;
}
const reader = response?.body?.getReader();
const decoder = new TextDecoder('utf-8');
return {
reader,
decoder
};
};
export const receiveChatStream = async (
reader: any,
decoder: TextDecoder,
callback: (data: any) => void
) => {
const { done, value } = await reader.read();
if (done) {
return;
}
let chunk = decoder.decode(value, { stream: true });
if (chunk.startsWith('data: ')) {
chunk = chunk.substring('data: '.length);
}
const item = JSON.parse(chunk?.trim());
callback(item);
await receiveChatStream(reader, decoder, callback);
};