Commit f506afc4 authored by nextime's avatar nextime

Enhance prompt and parser to prevent XML/code block responses

- Clarified prompt to prevent XML tags and code blocks around JSON output
- Specified that formatting restrictions apply only to JSON wrapper, not answer content
- Enhanced JavaScript parser to detect JSON in span elements and other containers
- Added comprehensive container detection including spans, divs, and paragraphs
- Improved JSON detection to handle responses outside of code blocks
- Maintained backward compatibility with existing code block detection
parent 12dedbaf
Pipeline #175 canceled with stages
......@@ -370,28 +370,29 @@ async def forward_to_chatbot(chatbot_name, config, prompt):
# Generate unique ID for this request using UUID for better uniqueness
request_id = str(uuid.uuid4()).replace('-', '')[:16] # 16-character unique ID
# Create JSON-based prompt with unique ID and base64 encoding - optimized to prevent explanations
# Create JSON-based prompt with unique ID and base64 encoding - enhanced to prevent XML/code blocks
json_instruction = f'''SYSTEM INSTRUCTION - FOLLOW EXACTLY:
You must respond ONLY with a JSON object. Do NOT write any explanation, introduction, or commentary.
REQUIRED FORMAT (copy this structure exactly):
IMPORTANT: These formatting restrictions apply ONLY to text before/after the JSON, NOT to your actual answer content:
- Do NOT use <pre><code> tags around the JSON
- Do NOT use XML formatting like <ask_followup_question> around the JSON
- Do NOT use markdown code blocks around the JSON
- Do NOT add explanatory text before or after the JSON
- Do NOT use any HTML tags around the JSON
Your actual answer content (which gets base64 encoded) can contain any format you need to properly answer the question.
REQUIRED FORMAT (output as plain text, not in code blocks):
{{"response": "BASE64_ENCODED_ANSWER", "id": "{request_id}"}}
PROCESS:
1. Generate your complete answer to the user's question
2. Encode that answer in base64
1. Generate your complete answer to the user's question (any format needed)
2. Encode that ENTIRE answer in base64
3. Place the base64 string in the "response" field
4. Use the exact ID: {request_id}
5. Output ONLY the JSON - no other text
DO NOT:
- Write "I'll encode this in JSON" or similar explanations
- Add any text before or after the JSON
- Explain the encoding process
- Use markdown formatting around the JSON
RESPOND ONLY WITH THE JSON OBJECT.
5. Output ONLY the JSON as plain text - no surrounding tags or formatting
USER QUESTION:
'''
......@@ -587,8 +588,9 @@ async def detect_json_response_with_id(page, container_selector, request_id, pro
'main div[role="article"]',
'div[class*="css-"]',
'span[class*="css-"]',
'span', // Enhanced span detection for JSON
'pre', 'code', // Include code blocks for JSON
'p', 'div', 'span'
'p', 'div'
].join(', '));
let allTexts = [];
......@@ -688,20 +690,23 @@ async def detect_json_response_with_id(page, container_selector, request_id, pro
const getCompleteJsonResponse = () => {
console.log(`🔍 Searching for complete JSON with ID: ${requestId}`);
// First priority: Check HTML formatted code blocks
const codeBlocks = container.querySelectorAll([
// First priority: Check all possible JSON containers including spans
const jsonContainers = container.querySelectorAll([
'span', // Check spans first - common for direct JSON output
'div', // Check divs for JSON content
'pre code.language-json',
'pre code[class*="json"]',
'code.language-json',
'code[class*="json"]',
'pre code',
'code'
'code',
'p' // Also check paragraphs
].join(', '));
for (const codeBlock of codeBlocks) {
const jsonText = codeBlock.textContent ? codeBlock.textContent.trim() : '';
for (const container of jsonContainers) {
const jsonText = container.textContent ? container.textContent.trim() : '';
if (jsonText && jsonText.includes(requestId)) {
console.log(`📋 Found code block with request ID: ${jsonText.substring(0, 150)}...`);
console.log(`📋 Found container with request ID: ${jsonText.substring(0, 150)}...`);
if (isCompleteValidJson(jsonText)) {
const jsonObj = JSON.parse(jsonText);
console.log(`✅ Returning validated response for ID: ${jsonObj.id}`);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment