From b37799b3d2baa9d0c08d64f24cab518355308195 Mon Sep 17 00:00:00 2001 From: William Jeynes Date: Thu, 2 Apr 2026 21:02:26 +0100 Subject: [PATCH] Improve response extraction --- agent/nodes/triggerEventSetup.ts | 12 ++++++++++-- agent/nodes/verificationSetup.ts | 9 ++++++++- agent/prompts/normalization.txt | 5 ++++- agent/prompts/trigger.txt | 15 +++++++++++---- 4 files changed, 33 insertions(+), 8 deletions(-) diff --git a/agent/nodes/triggerEventSetup.ts b/agent/nodes/triggerEventSetup.ts index 4cf36f8..1367818 100644 --- a/agent/nodes/triggerEventSetup.ts +++ b/agent/nodes/triggerEventSetup.ts @@ -3,9 +3,17 @@ import { MessagesState } from "../state"; import { AIMessage, BaseMessage } from "@langchain/core/messages"; import { rankExampleTriggerEvents } from "../tools/retreiveExamples"; +function extractTE(text: string) { + const match = text.match(/([\s\S]*?)<\/norm>/); + if (!match) throw new Error("Nothing found between tags"); + return match[1].trim(); +} + + export const triggerEventSetup: GraphNode = async (state) => { - let nc = state?.messages?.at(-1)?.content ?? "" //keep a copy of normalized trigger event. Again two things, womp womp - + let raw = state?.messages?.at(-1)?.content ?? "" //keep a copy of normalized trigger event. Again two things, womp womp + let nc = extractTE(raw.toString()) + //Now give in-context examples. hopwfully we can self-teach? let similarityResults = await rankExampleTriggerEvents(state.disinformationTitle) diff --git a/agent/nodes/verificationSetup.ts b/agent/nodes/verificationSetup.ts index 66fe64d..06c7b55 100644 --- a/agent/nodes/verificationSetup.ts +++ b/agent/nodes/verificationSetup.ts @@ -3,6 +3,12 @@ import { MessagesState, ProposedTriggerEventArray } from "../state"; import { logger } from "../utils/logger"; import { jsonrepair } from 'jsonrepair'; +function extractJSON(text: string) { + const match = text.match(/([\s\S]*?)<\/json>/); + if (!match) throw new Error("No JSON found between tags"); + return match[1].trim(); +} + export const verificationSetup: GraphNode = async (state) => { if (state.proposedTriggerEvent == undefined) { logger.warn("No trigger events in memory, parsing"); @@ -11,7 +17,8 @@ export const verificationSetup: GraphNode = async (state) let repaired: string; try { - repaired = jsonrepair(genResponse); + let extracted = extractJSON(genResponse) + repaired = jsonrepair(extracted); } catch (repairErr: any) { logger.error("Failed to repair JSON from LLM response."); logger.error("Original LLM response:\n%s", genResponse); diff --git a/agent/prompts/normalization.txt b/agent/prompts/normalization.txt index 75b2206..0775a09 100644 --- a/agent/prompts/normalization.txt +++ b/agent/prompts/normalization.txt @@ -16,4 +16,7 @@ Relevent examples are included in preceeding messages, use these as exact inspir The claim to normalize is: ###TITLE### -Produce no other text other than the condensed claim in inverted commas. Nothing should be around the normalised claim. \ No newline at end of file +Produce no other text other than the condensed claim, surrounded + +For example: BREAKING: the sky is green! +Becomes: The sky is green \ No newline at end of file diff --git a/agent/prompts/trigger.txt b/agent/prompts/trigger.txt index 96de655..495fe14 100644 --- a/agent/prompts/trigger.txt +++ b/agent/prompts/trigger.txt @@ -17,10 +17,15 @@ Include a url to a source for your trigger event (not a web search, a specific u Include the date that the event happened ("March 2022" for exmaple) Use a JSON format with each entry containing "Event,ReasoningWhyRelevant,SearchQuery,Url,Date". -Return ONLY valid JSON. -Do not include explanations. -Do not wrap in markdown. -Do not label where the json is using colons +Return ONLY JSON, no extra text. Wrap it like this: + +[ + { + "Event": "Example" + ... + } +] + Multiple tool invocations should be requested at once, if applicable. Use your abilities to look between the lines and produce some insightful analysis, thinking both short and long term. @@ -32,4 +37,6 @@ Analysis should only be completed for proposed events that would graner >0.7 poi Since URLs change frequently, use tools to retreive up to date informaiton everytime, provided examples or existing knowledge will be wrong or out of date. +Remember to return just json enclosed by + Lets go through it step by step \ No newline at end of file