enh: retrieval query generation
This commit is contained in:
+40
-5
@@ -348,15 +348,16 @@ export const generateEmoji = async (
|
||||
return null;
|
||||
};
|
||||
|
||||
export const generateSearchQuery = async (
|
||||
export const generateQueries = async (
|
||||
token: string = '',
|
||||
model: string,
|
||||
messages: object[],
|
||||
prompt: string
|
||||
prompt: string,
|
||||
type?: string = 'web_search'
|
||||
) => {
|
||||
let error = null;
|
||||
|
||||
const res = await fetch(`${WEBUI_BASE_URL}/api/task/query/completions`, {
|
||||
const res = await fetch(`${WEBUI_BASE_URL}/api/task/queries/completions`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
Accept: 'application/json',
|
||||
@@ -366,7 +367,8 @@ export const generateSearchQuery = async (
|
||||
body: JSON.stringify({
|
||||
model: model,
|
||||
messages: messages,
|
||||
prompt: prompt
|
||||
prompt: prompt,
|
||||
type: type
|
||||
})
|
||||
})
|
||||
.then(async (res) => {
|
||||
@@ -385,7 +387,40 @@ export const generateSearchQuery = async (
|
||||
throw error;
|
||||
}
|
||||
|
||||
return res?.choices[0]?.message?.content.replace(/["']/g, '') ?? prompt;
|
||||
|
||||
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 "queries" key, return the queries array; otherwise, return an empty array
|
||||
if (parsed && parsed.queries) {
|
||||
return Array.isArray(parsed.queries) ? parsed.queries : [];
|
||||
} 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 generateMoACompletion = async (
|
||||
|
||||
Reference in New Issue
Block a user