Skip to content

Commit

Permalink
Merge pull request #907 from cygaar/fix_evaulation_parsing
Browse files Browse the repository at this point in the history
fix: evaluation json parsing
  • Loading branch information
cygaar authored Dec 7, 2024
2 parents 7bd0892 + af7591b commit 216e312
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
2 changes: 1 addition & 1 deletion packages/client-twitter/src/interactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ Response options are RESPOND, IGNORE and STOP .
{{agentName}} should respond to messages that are directed at them, or participate in conversations that are interesting or relevant to their background, IGNORE messages that are irrelevant to them, and should STOP if the conversation is concluded.
{{agentName}} is in a room with other users and wants to be conversational, but not annoying.
{{agentName}} should RESPOND to messages that are directed at them, or participate in conversations that are interesting or relevant to their background.
{{agentName}} must RESPOND to messages that are directed at them, a command towards them, or participate in conversations that are interesting or relevant to their background.
If a message is not interesting or relevant, {{agentName}} should IGNORE.
Unless directly RESPONDing to a user, {{agentName}} should IGNORE messages that are very short or do not contain much information.
If a user asks {{agentName}} to stop talking, {{agentName}} should STOP.
Expand Down
22 changes: 14 additions & 8 deletions packages/core/src/parsing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,34 +60,40 @@ Your response must include the JSON block.`;
export function parseJsonArrayFromText(text: string) {
let jsonData = null;

// First try to parse with the original JSON format
const jsonBlockMatch = text.match(jsonBlockPattern);

if (jsonBlockMatch) {
try {
jsonData = JSON.parse(jsonBlockMatch[1]);
// Replace single quotes with double quotes before parsing
const normalizedJson = jsonBlockMatch[1].replace(/'/g, '"');
jsonData = JSON.parse(normalizedJson);
} catch (e) {
console.error("Error parsing JSON:", e);
return null;
}
} else {
const arrayPattern = /\[\s*{[\s\S]*?}\s*\]/;
}

// If that fails, try to find an array pattern
if (!jsonData) {
const arrayPattern = /\[\s*['"][^'"]*['"]\s*\]/;
const arrayMatch = text.match(arrayPattern);

if (arrayMatch) {
try {
jsonData = JSON.parse(arrayMatch[0]);
// Replace single quotes with double quotes before parsing
const normalizedJson = arrayMatch[0].replace(/'/g, '"');
jsonData = JSON.parse(normalizedJson);
} catch (e) {
console.error("Error parsing JSON:", e);
return null;
}
}
}

if (Array.isArray(jsonData)) {
return jsonData;
} else {
return null;
}

return null;
}

/**
Expand Down

0 comments on commit 216e312

Please sign in to comment.