Improve response extraction

This commit is contained in:
William Jeynes
2026-04-02 21:02:26 +01:00
parent 10f2644408
commit b37799b3d2
4 changed files with 33 additions and 8 deletions
+9 -1
View File
@@ -3,8 +3,16 @@ import { MessagesState } from "../state";
import { AIMessage, BaseMessage } from "@langchain/core/messages"; import { AIMessage, BaseMessage } from "@langchain/core/messages";
import { rankExampleTriggerEvents } from "../tools/retreiveExamples"; import { rankExampleTriggerEvents } from "../tools/retreiveExamples";
function extractTE(text: string) {
const match = text.match(/<norm>([\s\S]*?)<\/norm>/);
if (!match) throw new Error("Nothing found between <norm> tags");
return match[1].trim();
}
export const triggerEventSetup: GraphNode<typeof MessagesState> = async (state) => { export const triggerEventSetup: GraphNode<typeof MessagesState> = 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? //Now give in-context examples. hopwfully we can self-teach?
let similarityResults = await rankExampleTriggerEvents(state.disinformationTitle) let similarityResults = await rankExampleTriggerEvents(state.disinformationTitle)
+8 -1
View File
@@ -3,6 +3,12 @@ import { MessagesState, ProposedTriggerEventArray } from "../state";
import { logger } from "../utils/logger"; import { logger } from "../utils/logger";
import { jsonrepair } from 'jsonrepair'; import { jsonrepair } from 'jsonrepair';
function extractJSON(text: string) {
const match = text.match(/<json>([\s\S]*?)<\/json>/);
if (!match) throw new Error("No JSON found between <json> tags");
return match[1].trim();
}
export const verificationSetup: GraphNode<typeof MessagesState> = async (state) => { export const verificationSetup: GraphNode<typeof MessagesState> = async (state) => {
if (state.proposedTriggerEvent == undefined) { if (state.proposedTriggerEvent == undefined) {
logger.warn("No trigger events in memory, parsing"); logger.warn("No trigger events in memory, parsing");
@@ -11,7 +17,8 @@ export const verificationSetup: GraphNode<typeof MessagesState> = async (state)
let repaired: string; let repaired: string;
try { try {
repaired = jsonrepair(genResponse); let extracted = extractJSON(genResponse)
repaired = jsonrepair(extracted);
} catch (repairErr: any) { } catch (repairErr: any) {
logger.error("Failed to repair JSON from LLM response."); logger.error("Failed to repair JSON from LLM response.");
logger.error("Original LLM response:\n%s", genResponse); logger.error("Original LLM response:\n%s", genResponse);
+4 -1
View File
@@ -16,4 +16,7 @@ Relevent examples are included in preceeding messages, use these as exact inspir
The claim to normalize is: The claim to normalize is:
###TITLE### ###TITLE###
Produce no other text other than the condensed claim in inverted commas. Nothing should be around the normalised claim. Produce no other text other than the condensed claim, surrounded <norm></norm>
For example: BREAKING: the sky is green!
Becomes: <norm>The sky is green</norm>
+11 -4
View File
@@ -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) Include the date that the event happened ("March 2022" for exmaple)
Use a JSON format with each entry containing "Event,ReasoningWhyRelevant,SearchQuery,Url,Date". Use a JSON format with each entry containing "Event,ReasoningWhyRelevant,SearchQuery,Url,Date".
Return ONLY valid JSON. Return ONLY JSON, no extra text. Wrap it like this:
Do not include explanations. <json>
Do not wrap in markdown. [
Do not label where the json is using colons {
"Event": "Example"
...
}
]
</json>
Multiple tool invocations should be requested at once, if applicable. 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. 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. 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 <json></json>
Lets go through it step by step Lets go through it step by step