I am trying to prompt an LLM model to create a blog. Using Mistral as a base model I setup an instruction to return json as an output, wrote a function that takes the prompt as an argument, and returned generatedPosts as the response from the function (shown below).
const generatePosts = async (taskInstruction) => {
try {
const prompt = taskInstruction;
const chatResponse = await client.chat({
model: 'mistral-large-latest',
response_format: { type: 'json_object' },
messages: [{ role: 'user', content: prompt }],
"stream": true,
"max_tokens": 4500,
"temperature": 0.6,
});
if (!chatResponse) {
console.error('Chat response is null.');
return null;
}
if (!chatResponse.choices || chatResponse.choices.length === 0) {
console.error('Invalid chat response:', chatResponse);
return null;
}
const generatedPosts = chatResponse.choices[0].message.content;
return generatedPosts;
} catch (error) {
console.error('Error generating posts:', error);
return null;
}
};
const categorySlug = item.title.toLowerCase().split(' ').join('-');
const targetCategory = allPosts.find(category => category.slug === categorySlug);
if (!targetCategory) {
console.error(`Category with slug '${categorySlug}' not found.`);
} else {
const categoryId = targetCategory.id;
try {
const jsonResponse = await generatePosts(taskInstruction);
console.log('Generated JSON:', jsonResponse);
// Find the starting index of the JSON object
const startIndex = jsonResponse.indexOf('{');
if (startIndex === -1) {
throw new Error('Invalid JSON response: JSON object not found');
}
// Extract the substring containing only the JSON data
const jsonData = jsonResponse.slice(startIndex);
const cleanedJsonResponse = jsonData.replace(/\n/g, '');
// Parse JSON string into a JavaScript object
const postData = JSON.parse(cleanedJsonResponse);
// Access key/value pairs
const postTitle = postData.postTitle;
const postBody = postData.postBody;
const category = postData.category;
console.log('Post Title:', postTitle);
console.log('Post Body:', postBody);
console.log('Category:', category);
}catch (error){
console.log(error);
}
}
However, testing the code I got an error:
Error creating posts: SyntaxError: Unexpected non-whitespace character after JSON at position 2541 (line 1 column 2542)
at JSON.parse (<anonymous>)
at handler (webpack-internal:///(api)/./pages/api/llm/instructions/createPostData.js:271:43)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async K (/path-tofile/node_modules/next/dist/compiled/next-server/pages-api.runtime.dev.js:21:2946)
at async U.render (/path-tofile/node_modules/next/dist/compiled/next-server/pages-api.runtime.dev.js:21:3827)
at async DevServer.runApi (/path-tofile/node_modules/next/dist/server/next-server.js:554:9)
at async NextNodeServer.handleCatchallRenderRequest (/path-tofile/node_modules/next/dist/server/next-server.js:266:37)
at async DevServer.handleRequestImpl (/path-tofile/node_modules/next/dist/server/base-server.js:789:17)
at async /path-tofile/node_modules/next/dist/server/dev/next-dev-server.js:331:20
at async Span.traceAsyncFn (/path-tofile/node_modules/next/dist/trace/trace.js:151:20)
at async DevServer.handleRequest (/path-tofile/node_modules/next/dist/server/dev/next-dev-server.js:328:24)
at async invokeRender (/path-tofile/node_modules/next/dist/server/lib/router-server.js:174:21)
at async handleRequest (/path-tofile/node_modules/next/dist/server/lib/router-server.js:353:24)
at async requestHandlerImpl (/path-tofile/node_modules/next/dist/server/lib/router-server.js:377:13)
at async Server.requestListener (/pathtofile/node_modules/next/dist/server/lib/start-server.js:140:13⨯ pages/api/llm/instructions/createPostData.js (279:42) @ parse ⨯ SyntaxError: Unexpected non-whitespace character after JSON at position 2541 (line 1 column 2542)
at JSON.parse (<anonymous>)
at handler (webpack-internal:///(api)/./pages/api/llm/instructions/createPostData.js:271:43)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async K (/path-tofile/node_modules/next/dist/compiled/next-server/pages-api.runtime.dev.js:21:2946)
at async U.render (/path-tofile/node_modules/next/dist/compiled/next-server/pages-api.runtime.dev.js:21:3827)
at async DevServer.runApi (/path-tofile/node_modules/next/dist/server/next-server.js:554:9)
at async NextNodeServer.handleCatchallRenderRequest (/path-tofile/node_modules/next/dist/server/next-server.js:266:37)
at async DevServer.handleRequestImpl (/path-tofile/node_modules/next/dist/server/base-server.js:789:17)
at async /path-tofile/node_modules/next/dist/server/dev/next-dev-server.js:331:20
at async Span.traceAsyncFn (/path-tofile/node_modules/next/dist/trace/trace.js:151:20)
at async DevServer.handleRequest (/path-tofile/node_modules/next/dist/server/dev/next-dev-server.js:328:24)
at async invokeRender (/path-tofile/node_modules/next/dist/server/lib/router-server.js:174:21)
at async handleRequest (/path-tofile/node_modules/next/dist/server/lib/router-server.js:353:24)
at async requestHandlerImpl (/path-tofile/node_modules/next/dist/server/lib/router-server.js:377:13)
at async Server.requestListener (/path-tofile/node_modules/next/dist/server/lib/start-server.js:140:13) {
page: '/api/llm/instructions/createPostData'
}
277 |
278 | // Parse JSON string into a JavaScript object
> 279 | const postData = JSON.parse(cleanedJsonResponse);
| ^
280 |
281 | // Access key/value pairs
282 | const postTitle = postData.postTitle;
○ Compiling /_error ...
I am struggling trying to parse the response with the above code, and hoping you can help resolve the issue. Below is a sample of the returned json
Response:
{
"category": "Literary Classics",
"postTitle": "The Enduring Appeal of Jane Austen's 'Pride and Prejudice'",
"postBody": "Discover the timeless charm of Jane Austen's 'Pride and Prejudice' and learn why this literary classic continues to captivate audiences worldwide. Written by Emily Watson.
Table of Contents:
1. Introduction
2. The Unforgettable Characters
3. Austen's Wit and Social Commentary
4. Themes of Love and Marriage
5. Conclusion
Introduction:
'Pride and Prejudice,' first published in 1813, remains one of the most beloved novels in English literature. In this article, we will explore the reasons behind its enduring appeal.
The Unforgettable Characters:
One of the key elements that make 'Pride and Prejudice' a timeless classic is its unforgettable characters. From the quick-witted Elizabeth Bennet to the proud Mr. Darcy, Austen created a cast of characters that readers can't help but fall in love with.
Austen's Wit and Social Commentary:
Austen's keen observations of social norms and her sharp wit make 'Pride and Prejudice' a delightful read. Her commentary on class, gender, and marriage remains relevant even today.
Themes of Love and Marriage:
The novel's exploration of love and marriage continues to resonate with modern audiences. Austen's portrayal of romantic relationships is both realistic and heartwarming, making 'Pride and Prejudice' a favorite among romance readers.
Conclusion:
'Pride and Prejudice' is a literary classic that has stood the test of time. Its memorable characters, witty social commentary, and exploration of love and marriage continue to captivate readers around the world.
Tips:
- Dive deeper into the novel by reading literary analyses and critiques of 'Pride and Prejudice.'
- Watch film and TV adaptations of the novel to see how its themes and characters have been interpreted on screen.
- Join a book club or online forum to discuss 'Pride and Prejudice' with fellow readers.
- Explore other works by Jane Austen to discover more of her timeless stories and characters.
",
"tips": {
"first_tip": "Dive deeper into the novel by reading literary analyses and critiques of 'Pride and Prejudice.'",
"second_tip": "Watch film and TV adaptations of the novel to see how its themes and characters have been interpreted on screen.",
"third_tip": "Join a book club or online forum to discuss 'Pride and Prejudice' with fellow readers.",
"fourth_tip": "Explore other works by Jane Austen to discover more of her timeless stories and characters."
}
}
Thanks in advance.