refac: query generation

This commit is contained in:
Timothy Jaeryang Baek 2024-11-24 18:03:58 -08:00
parent a3a205abd1
commit 907cf61da7
2 changed files with 6 additions and 8 deletions

View File

@ -972,7 +972,7 @@ DEFAULT_QUERY_GENERATION_PROMPT_TEMPLATE = """### Task:
Based on the chat history, determine whether a search is necessary, and if so, generate a 1-3 broad search queries to retrieve comprehensive and updated information. If no search is required, return an empty list.
### Guidelines:
- Respond exclusively with a JSON object.
- Respond **EXCLUSIVELY** with a JSON object. Any form of extra commentary, explanation, or additional text is prohibited.
- If a search query is needed, return an object like: { "queries": ["query1", "query2"] } where each query is distinct and concise.
- If no search query is necessary, output should be: { "queries": [] }
- Default to suggesting a search query to ensure accurate and updated information, unless it is definitively clear no search is required.
@ -981,7 +981,8 @@ Based on the chat history, determine whether a search is necessary, and if so, g
- Today's date is: {{CURRENT_DATE}}
### Output:
JSON format: {
Strictly return in JSON format:
{
"queries": ["query1", "query2"]
}

View File

@ -391,16 +391,13 @@ export const generateQueries = async (
// 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('}');
const jsonStartIndex = response.indexOf('{');
const jsonEndIndex = response.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);
const jsonResponse = response.substring(jsonStartIndex, jsonEndIndex + 1);
// Step 5: Parse the JSON block
const parsed = JSON.parse(jsonResponse);