Skip to content

Commit a7876f6

Browse files
authored
Implementing "jsonMode" for LLM calls, and from the react app iframe calls (#965)
* Fixes some caching bugs with the llm endpoint as well
1 parent 1af3f96 commit a7876f6

20 files changed

+525
-1144
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ cache
1111
artifact
1212
_deno.lock
1313

14-
!/jumble/integration/cache/llm-api-cache
14+
!/jumble/integration/cache
1515
.claude-prompt.tmp
1616
.pr-desc.tmp
1717
dist/

charm/src/iframe/static.ts

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -165,32 +165,6 @@ ${JSON.stringify(libraries)}
165165
});
166166
})();
167167
168-
window.grabJson = (gstr) => {
169-
// Function to extract and parse JSON from a string
170-
// This handles both raw JSON strings and code blocks with JSON
171-
const jsonRegex = /\`\`\`(?:json)?\s*([\s\S]*?)\s*\`\`\`|({[\s\S]*})/;
172-
const match = gstr.match(jsonRegex);
173-
174-
if (match) {
175-
// Use the first matching group that contains content
176-
const jsonStr = match[1] || match[2];
177-
try {
178-
return JSON.parse(jsonStr);
179-
} catch (e) {
180-
console.error("Failed to parse JSON:", e);
181-
return null;
182-
}
183-
} else {
184-
// If no JSON block found, attempt to parse the entire string
185-
try {
186-
return JSON.parse(gstr);
187-
} catch (e) {
188-
console.error("No valid JSON found in string");
189-
return null;
190-
}
191-
}
192-
}
193-
194168
// Define readWebpage utility with React available
195169
window.readWebpage = (function() {
196170
const inflight = [];
@@ -673,12 +647,23 @@ async function fetchLLMResponse() {
673647
system: 'Translate all the messages to emojis, reply in JSON.',
674648
messages: ['Hi', 'How can I help you today?', 'tell me a joke']
675649
};
676-
// grabJson is available on the window, string -> JSON
677-
const result = grabJson(await llm(promptPayload));
650+
const result = await llm(promptPayload)
678651
console.log('LLM responded:', result);
679652
}
680653
\`\`\`
681654
655+
If you need JSON to be returned from the LLM, you can enable the \`mode: 'json'\` in the \`promptPayload\`.
656+
657+
\`\`\`jsx
658+
const promptPayload = {
659+
system: 'Translate all the messages to emojis, reply in JSON.',
660+
messages: ['Hi', 'How can I help you today?', 'tell me a joke'],
661+
mode: 'json'
662+
};
663+
const result = await llm(promptPayload);
664+
console.log('JSON response from llm:', result);
665+
\`\`\`
666+
682667
## 3. readWebpage Function
683668
684669
\`\`\`jsx

0 commit comments

Comments
 (0)