Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: evaluation json parsing #907

Merged
merged 1 commit into from
Dec 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading