Hi there, I’m trying to set up Claude Sonnet through the API connector. I want to include message history with each API call. The messages are being stored in our bubble database already. How do I modify this JSON body to do that?
{
“model”: “claude-3-5-sonnet-20240620”,
“max_tokens”: 1024,
“system”: “only reply in Spanish”,
“messages”: [
{“role”: “user”, “content”: “Hello there.”},
{“role”: “assistant”, “content”: “Hi, I’m Claude. How can I help you?”},
{“role”: “user”, “content”: “Can you explain LLMs in plain English?”}
]
}
Hi! You can use the :format as text operator in Bubble. This is how it can be done:
{
“model”: “claude-3-5-sonnet-20240620”,
“max_tokens”: 1024,
“system”: “only reply in Spanish”,
“messages”: [
Do a search for messages:format as text
]
}
So the final json body would look like this? Do I want to format as text or as json? I included a screenshot of how our database is structured now
{
“model”: “claude-3-5-sonnet-20240320”,
“max_tokens”: 1024,
“system”: “You are a helpful AI assistant.”,
“messages”: If(ThreadExists,
Do a search for Messages:
Filter by thread equals :threadId
Sort by Created Date ascending format as json
{“role”: “This Message’s from who”, “content”: “This Message’s text itself”},
[{“role”: “user”, “content”: “Initial user message”}]
)
}
You should use the format as text operator
I don’t think Bubble has a format as JSON operator atm. It has JSON-safe but it sometimes breaks for no reason.
In order to put “do a search for messages” within the API call, do I need to do any special punctuation? Or do I need to add it as a workflow in bubble? Or will it just work as you outlined?
Good call - but I’ve noticed that formatted as JSON-safe often doesn’t work. I might finaaally find time this weekend to raise a bug report with Bubble though
Empty string is fine, but in a non-empty string, it doesn’t escape special characters properly, which ultimately results in an error at the API connector level
:format as JSON safe is not the problem. Your issues lies with :format as text where newlines will break JSON formatting.
So, suppose you have an array to use in API connector such as [Search:format as text]
Inside format as text, you have some JSON like {“key1”: This Thing’s X:formatted as JSON safe, “key2”: This Thing’s Y:formatted as JSON safe}. This will work fine.
However, if you added a new line between key 1 and key 2, as you might if you ran it through a JSON ‘beautifier, that’ll break the JSON formatting, because the newline is not JSON safe.
However, this, up until a few months back, used to fail in some instances which is why I had to use reg-ex and/or find and replace which wasn’t convenient. Idk if they have made any changes though, will have to check