chore: watch api
This commit is contained in:
@@ -14,43 +14,3 @@ export async function execChatCompletions(params: any) {
|
||||
export const queryModelsList = async () => {
|
||||
return request(`${OPENAI_MODELS}`);
|
||||
};
|
||||
|
||||
export const fetchChatStream = async (params: any) => {
|
||||
const response = await fetch(`${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);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user