mirror of
https://github.com/labring/FastGPT.git
synced 2025-07-31 19:38:41 +00:00
perf: quote response
This commit is contained in:
@@ -4,8 +4,7 @@ import type { InitChatResponse, InitShareChatResponse } from './response/chat';
|
||||
import { RequestPaging } from '../types/index';
|
||||
import type { ShareChatSchema } from '@/types/mongoSchema';
|
||||
import type { ShareChatEditType } from '@/types/app';
|
||||
import { Obj2Query } from '@/utils/tools';
|
||||
import type { QuoteItemType } from '@/pages/api/openapi/kb/appKbSearch';
|
||||
import type { QuoteItemType } from '@/pages/api/openapi/modules/kb/search';
|
||||
import type { Props as UpdateHistoryProps } from '@/pages/api/chat/history/updateChatHistory';
|
||||
|
||||
/**
|
||||
@@ -39,7 +38,7 @@ export const updateHistoryQuote = (params: {
|
||||
contentId: string;
|
||||
quoteId: string;
|
||||
sourceText: string;
|
||||
}) => GET(`/chat/history/updateHistoryQuote`, params);
|
||||
}) => PUT(`/chat/history/updateHistoryQuote`, params);
|
||||
|
||||
/**
|
||||
* 删除一句对话
|
||||
|
@@ -1,6 +1,8 @@
|
||||
import { sseResponseEventEnum } from '@/constants/chat';
|
||||
import { getErrText } from '@/utils/tools';
|
||||
import { parseStreamChunk } from '@/utils/adapt';
|
||||
import { QuoteItemType } from '@/pages/api/openapi/modules/kb/search';
|
||||
import { rawSearchKey } from '@/constants/chat';
|
||||
|
||||
interface StreamFetchProps {
|
||||
url?: string;
|
||||
@@ -14,88 +16,94 @@ export const streamFetch = ({
|
||||
onMessage,
|
||||
abortSignal
|
||||
}: StreamFetchProps) =>
|
||||
new Promise<{ responseText: string; errMsg: string; newHistoryId: string | null }>(
|
||||
async (resolve, reject) => {
|
||||
try {
|
||||
const response = await window.fetch(url, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
signal: abortSignal.signal,
|
||||
body: JSON.stringify({
|
||||
...data,
|
||||
stream: true
|
||||
})
|
||||
});
|
||||
new Promise<{
|
||||
responseText: string;
|
||||
errMsg: string;
|
||||
newHistoryId: string | null;
|
||||
[rawSearchKey]: QuoteItemType[];
|
||||
}>(async (resolve, reject) => {
|
||||
try {
|
||||
const response = await window.fetch(url, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
signal: abortSignal.signal,
|
||||
body: JSON.stringify({
|
||||
...data,
|
||||
stream: true
|
||||
})
|
||||
});
|
||||
|
||||
if (response.status !== 200) {
|
||||
const err = await response.json();
|
||||
return reject(err);
|
||||
}
|
||||
if (!response?.body) {
|
||||
throw new Error('Request Error');
|
||||
}
|
||||
|
||||
if (!response?.body) {
|
||||
throw new Error('Request Error');
|
||||
}
|
||||
const reader = response.body?.getReader();
|
||||
|
||||
const reader = response.body?.getReader();
|
||||
// response data
|
||||
let responseText = '';
|
||||
let rawSearch: QuoteItemType[] = [];
|
||||
let errMsg = '';
|
||||
const newHistoryId = response.headers.get('newHistoryId');
|
||||
|
||||
// response data
|
||||
let responseText = '';
|
||||
let errMsg = '';
|
||||
const newHistoryId = response.headers.get('newHistoryId');
|
||||
|
||||
const read = async () => {
|
||||
try {
|
||||
const { done, value } = await reader.read();
|
||||
if (done) {
|
||||
if (response.status === 200) {
|
||||
return resolve({
|
||||
responseText,
|
||||
errMsg,
|
||||
newHistoryId
|
||||
});
|
||||
} else {
|
||||
return reject('响应过程出现异常~');
|
||||
}
|
||||
}
|
||||
const chunkResponse = parseStreamChunk(value);
|
||||
|
||||
chunkResponse.forEach((item) => {
|
||||
// parse json data
|
||||
const data = (() => {
|
||||
try {
|
||||
return JSON.parse(item.data);
|
||||
} catch (error) {
|
||||
return item.data;
|
||||
}
|
||||
})();
|
||||
|
||||
if (item.event === sseResponseEventEnum.answer && data !== '[DONE]') {
|
||||
const answer: string = data?.choices?.[0].delta.content || '';
|
||||
onMessage(answer);
|
||||
responseText += answer;
|
||||
} else if (item.event === sseResponseEventEnum.error) {
|
||||
errMsg = getErrText(data, '流响应错误');
|
||||
}
|
||||
});
|
||||
read();
|
||||
} catch (err: any) {
|
||||
if (err?.message === 'The user aborted a request.') {
|
||||
const read = async () => {
|
||||
try {
|
||||
const { done, value } = await reader.read();
|
||||
if (done) {
|
||||
if (response.status === 200 && !errMsg) {
|
||||
return resolve({
|
||||
responseText,
|
||||
errMsg,
|
||||
newHistoryId
|
||||
newHistoryId,
|
||||
rawSearch
|
||||
});
|
||||
} else {
|
||||
return reject({
|
||||
message: errMsg || '响应过程出现异常~',
|
||||
responseText
|
||||
});
|
||||
}
|
||||
reject(getErrText(err, '请求异常'));
|
||||
}
|
||||
};
|
||||
read();
|
||||
} catch (err: any) {
|
||||
console.log(err);
|
||||
const chunkResponse = parseStreamChunk(value);
|
||||
|
||||
reject(getErrText(err, '请求异常'));
|
||||
}
|
||||
chunkResponse.forEach((item) => {
|
||||
// parse json data
|
||||
const data = (() => {
|
||||
try {
|
||||
return JSON.parse(item.data);
|
||||
} catch (error) {
|
||||
return item.data;
|
||||
}
|
||||
})();
|
||||
|
||||
if (item.event === sseResponseEventEnum.answer && data !== '[DONE]') {
|
||||
const answer: string = data?.choices?.[0].delta.content || '';
|
||||
onMessage(answer);
|
||||
responseText += answer;
|
||||
} else if (item.event === sseResponseEventEnum.appStreamResponse) {
|
||||
rawSearch = data?.[rawSearchKey] ? data[rawSearchKey] : rawSearch;
|
||||
} else if (item.event === sseResponseEventEnum.error) {
|
||||
errMsg = getErrText(data, '流响应错误');
|
||||
}
|
||||
});
|
||||
read();
|
||||
} catch (err: any) {
|
||||
if (err?.message === 'The user aborted a request.') {
|
||||
return resolve({
|
||||
responseText,
|
||||
errMsg,
|
||||
newHistoryId,
|
||||
rawSearch
|
||||
});
|
||||
}
|
||||
reject(getErrText(err, '请求异常'));
|
||||
}
|
||||
};
|
||||
read();
|
||||
} catch (err: any) {
|
||||
console.log(err);
|
||||
|
||||
reject(getErrText(err, '请求异常'));
|
||||
}
|
||||
);
|
||||
});
|
||||
|
Reference in New Issue
Block a user