I need some help from all you JSON wizards out there…
I’ve recently added the Claude AI model from Anthropic into my app. It’s so much faster to work with than GPT, but it has some quirks that make it a little more difficult to work with in terms of the JSON call.
Claude’s API requires the formatting of a request to be:
{
"prompt": "\n\nHuman: [this is where the human's prompt goes]\n\nAssistant:"
"model": <model>,
"stop_sequences": [
"\n\nHuman:"
]
}
Now, with OpenAI, I would just wrap the user’s prompt in a :formatted-as-JSON-safe and that would get rid of any JSON-breaking characters (most times… this is another topic altogether). But since Claude requires the \n\nHuman: and \n\nAssistant: parts inside the request, doing a “formatted-as-JSON-safe” breaks that because it escapes the necessary \n\n formatting.
I’m pulling my hair out trying to figure out how to format these requests so my app users stop getting formatting error messages. Doing a simple :find&replace for things like quotation marks just isn’t working.
Solution 1
Could you use regex? First, format as JSON safe. Then, extract with Regex. The regex code .(.*). will match everything except the first and last characters which are the opening and closing quotation marks (highlighted in green). Do this for the Human and Assistant prompts.