|
@@ -612,6 +612,78 @@ export const generateTitle = async (
|
|
|
}
|
|
|
};
|
|
|
|
|
|
+export const generateFollowUps = async (
|
|
|
+ token: string = '',
|
|
|
+ model: string,
|
|
|
+ messages: string,
|
|
|
+ chat_id?: string
|
|
|
+) => {
|
|
|
+ let error = null;
|
|
|
+
|
|
|
+ const res = await fetch(`${WEBUI_BASE_URL}/api/v1/tasks/follow_ups/completions`, {
|
|
|
+ method: 'POST',
|
|
|
+ headers: {
|
|
|
+ Accept: 'application/json',
|
|
|
+ 'Content-Type': 'application/json',
|
|
|
+ Authorization: `Bearer ${token}`
|
|
|
+ },
|
|
|
+ body: JSON.stringify({
|
|
|
+ model: model,
|
|
|
+ messages: messages,
|
|
|
+ ...(chat_id && { chat_id: chat_id })
|
|
|
+ })
|
|
|
+ })
|
|
|
+ .then(async (res) => {
|
|
|
+ if (!res.ok) throw await res.json();
|
|
|
+ return res.json();
|
|
|
+ })
|
|
|
+ .catch((err) => {
|
|
|
+ console.error(err);
|
|
|
+ if ('detail' in err) {
|
|
|
+ error = err.detail;
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ });
|
|
|
+
|
|
|
+ if (error) {
|
|
|
+ throw error;
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ // Step 1: Safely extract the response string
|
|
|
+ const response = res?.choices[0]?.message?.content ?? '';
|
|
|
+
|
|
|
+ // Step 2: Attempt to fix common JSON format issues like single quotes
|
|
|
+ const sanitizedResponse = response.replace(/['‘’`]/g, '"'); // Convert single quotes to double quotes for valid JSON
|
|
|
+
|
|
|
+ // Step 3: Find the relevant JSON block within the response
|
|
|
+ const jsonStartIndex = sanitizedResponse.indexOf('{');
|
|
|
+ const jsonEndIndex = sanitizedResponse.lastIndexOf('}');
|
|
|
+
|
|
|
+ // Step 4: Check if we found a valid JSON block (with both `{` and `}`)
|
|
|
+ if (jsonStartIndex !== -1 && jsonEndIndex !== -1) {
|
|
|
+ const jsonResponse = sanitizedResponse.substring(jsonStartIndex, jsonEndIndex + 1);
|
|
|
+
|
|
|
+ // Step 5: Parse the JSON block
|
|
|
+ const parsed = JSON.parse(jsonResponse);
|
|
|
+
|
|
|
+ // Step 6: If there's a "follow_ups" key, return the follow_ups array; otherwise, return an empty array
|
|
|
+ if (parsed && parsed.follow_ups) {
|
|
|
+ return Array.isArray(parsed.follow_ups) ? parsed.follow_ups : [];
|
|
|
+ } else {
|
|
|
+ return [];
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // If no valid JSON block found, return an empty array
|
|
|
+ return [];
|
|
|
+ } catch (e) {
|
|
|
+ // Catch and safely return empty array on any parsing errors
|
|
|
+ console.error('Failed to parse response: ', e);
|
|
|
+ return [];
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
export const generateTags = async (
|
|
|
token: string = '',
|
|
|
model: string,
|