Anthropic json in from the API response in a repeating group

Well don’t you just love :heart_eyes_cat: it when you find a nice new thing that you’ve not needed before.

This is for my reference and anyone that has a need to use tool json in a repeating group (or any other array of data, lists).

When working with the API connector and in particular the Anthropic message api you can create tools that generates nice json you can use from an action

this image, there is s section array, this is handy for importing in to a data type, and you would typically take this and use a recursive backend workflow to grab each of the sections and save them.

However, in this instance I didn’t want to do that, I wanted to give the user the option of what they wanted to save each time before saving to the database from the returned responses.

So… when have initialized, validated you will need to check the type that is now available.

now when you add the repeating group to the page, select the datasource that matches the name in this case “AnthropicMessage input section”.

You can leave the data source empty (well depends on your use case.)

Now from whatever method you trigger the api workflow to send a message, you can then get the data and send it to the RG (or you can set a custom state, list with the same data type as the RG and use that as the source).

Then map out all of the lovely things you need in the RG for the users.

Then after you decide what you want to keep, remove you can run whatever method you desire to save the data to the DB.

Enjoy

1 Like

I’m a bit lost but @stuart4 you always put out great stuff so I want to understand this a bit more…

Is this essentially demonstrating how to setup the API call to provide an output as JSON array?

I think where my personal confusion comes from is not being able to see the body parameter of ‘tool’ which I think is a parameter for specific use when working with claude AI.

1 Like

I am pretty good at confusion, it is one of my top 3 special traits.

Anyways in the interest of trying to unconfuse?

yes, its the output as a Json array, all nicely formatted as per your specification…

When i am sending a message api request to Anthropic (claude), I am specifying to explicitly use a Tool, this tool is producing what would normal be output in the content., and in this case a couple of things, but the sections array. I am alos sending a system message to state that its a to do this.

So this can now be used in the front end repeating group, or used as normal in a be wf.

{
  "body": {
    "id": "msg_01Wvr1r5sXhgsZXBYK7zug1v",
    "type": "message",
    "role": "assistant",
    "model": "claude-3-5-sonnet-20241022",
    "content": [
      {
        "type": "tool_use",
        "id": "toolu_0131dJxwa7NJK6PejvKfqanR",
        "name": "generate_document_sections",
        "input": {
          "DocumentType": "Business Proposal",
          "Industry": "General Business",
          "ProblemStatement": "General business opportunity requiring a formal proposal response",
          "Audience": "Business stakeholders and decision-makers",
          "Requirements": "Standard proposal elements including executive summary, solution details, pricing, and implementation plan",
          "NumberOfSections": 5,
          "sections": [
            {
              "id": "executive-summary",
              "title": "Executive Summary",
              "overview": "High-level overview of the proposal including key benefits and value proposition",
              "estimatedLength": "1-2 pages",
              "order": 1,
              "isRequired": true
            }
          ]
        }
      }
    ],
    "stop_reason": "tool_use",
    "stop_sequence": null,
    "usage": {
      "input_tokens": 801,
      "output_tokens": 539
    }
  },
  "error": {
    "status_code": 200,
    "status_message": "OK",
    "body": "\"{\\\"id\\\":\\\"msg_01Wvr1r5sXhgsZXBYK7zug1v\\\",\\\"type\\\":\\\"message\\\",\\\"role\\\":\\\"assistant\\\",\\\"model\\\":\\\"claude-3-5-sonnet-20241022\\\",\\\"content\\\":[{\\\"type\\\":\\\"tool_use\\\",\\\"id\\\":\\\"toolu_0131dJxwa7NJK6PejvKfqanR\\\",\\\"name\\\":\\\"generate_document_sections\\\",\\\"input\\\":{\\\"DocumentType\\\":\\\"Business Proposal\\\",\\\"Industry\\\":\\\"General Business\\\",\\\"ProblemStatement\\\":\\\"General business opportunity requiring a formal proposal response\\\",\\\"Audience\\\":\\\"Business stakeholders and decision-makers\\\",\\\"Requirements\\\":\\\"Standard proposal elements including executive summary, solution details, pricing, and implementation plan\\\",\\\"NumberOfSections\\\":5,\\\"sections\\\":[{\\\"id\\\":\\\"executive-summary\\\",\\\"title\\\":\\\"Executive Summary\\\",\\\"overview\\\":\\\"High-level overview of the proposal including key benefits and value proposition\\\",\\\"estimatedLength\\\":\\\"1-2 pages\\\",\\\"order\\\":1,\\\"isRequired\\\":true},{\\\"id\\\":\\\"company-background\\\",\\\"title\\\":\\\"Company Background\\\",\\\"overview\\\":\\\"Introduction to the company, relevant experience, and qualifications\\\",\\\"estimatedLength\\\":\\\"1-2 pages\\\",\\\"order\\\":2,\\\"isRequired\\\":true},{\\\"id\\\":\\\"proposed-solution\\\",\\\"title\\\":\\\"Proposed Solution\\\",\\\"overview\\\":\\\"Detailed description of the proposed solution, methodology, and approach\\\",\\\"estimatedLength\\\":\\\"3-4 pages\\\",\\\"order\\\":3,\\\"isRequired\\\":true},{\\\"id\\\":\\\"pricing-terms\\\",\\\"title\\\":\\\"Pricing and Terms\\\",\\\"overview\\\":\\\"Detailed cost breakdown, payment terms, and conditions\\\",\\\"estimatedLength\\\":\\\"1-2 pages\\\",\\\"order\\\":4,\\\"isRequired\\\":true},{\\\"id\\\":\\\"implementation-timeline\\\",\\\"title\\\":\\\"Implementation Timeline\\\",\\\"overview\\\":\\\"Project schedule, milestones, and delivery timeline\\\",\\\"estimatedLength\\\":\\\"1-2 pages\\\",\\\"order\\\":5,\\\"isRequired\\\":true}]}}],\\\"stop_reason\\\":\\\"tool_use\\\",\\\"stop_sequence\\\":null,\\\"usage\\\":{\\\"input_tokens\\\":801,\\\"output_tokens\\\":539}}\""
  },
  "returned_an_error": false,
  "headers": {
    "date": "Mon, 18 Nov 2024 02:32:21 GMT",
    "content-type": "application/json",
    "transfer-encoding": "chunked",
    "connection": "keep-alive",
    "anthropic-ratelimit-requests-limit": "50",
    "anthropic-ratelimit-requests-remaining": "49",
    "anthropic-ratelimit-requests-reset": "2024-11-18T02:32:14Z",
    "anthropic-ratelimit-tokens-limit": "40000",
    "anthropic-ratelimit-tokens-remaining": "40000",
    "anthropic-ratelimit-tokens-reset": "2024-11-18T02:32:21Z",
    "request-id": "req_0195zxvVP",
    "via": "1.1 google",
    "cf-cache-status": "DYNAMIC",
    "x-robots-tag": "none",
    "server": "cloudflare",
    "cf-ray": "8e448c78ef56ef2c-PDX",
    "content-encoding": "gzip"
  }
}

→ the above produces 5 sections that are used in an RG.

Let me know if this is still unclear, I mean i dont even know if this is something unusual or usually difficult. - I only know that its solved a problem for me :slight_smile:

1 Like

Thanks for the additional information. I have some research to do now on Claude and tools…I just started using more AI stuff to produce JSON and this might just be a better approach then what I’ve been doing.

I hope it does, and let me know if you have any issues… ill attempt to help, albeit I am also pretty early in this… but 2 brains an all.

1 Like

One thing to note with this, and it’s obvious but if you forget (FOR FUTURE ME)… when you change the structure of the Tools and ‘initialise’ in the API connector you will lose the ability to reference the data in the front end (from the previous altered structure)… obviously, as you’ve re initialised it and it’s different! … SO you will need to make a new API connector call… OR readjust all your shit.

Did you change the names of the JSON fields or just alter the structure of what is included in the JSON?

I changed this (see image), then re initialised - this is what allows me to see and manipulate the arrays in the front end where you can then use the response back from the api call.

I have some more testing to do on this, because maybe I am not thinking right… and maybe there is a better way, but it works. you just need to be mindful of changes… . It obviously doesn’t give the ability to dynamically change the structure on the fly, but then it makes sense and after its setup, you’d probably not.

1 Like

I think it is actually better if i give more context on "why and “what” here with my thinking on the Tool node. The json structure that you can get anthropic to follow is awesome, and I highly recommend you test it for your needs.

Tools: these are schemas that can be followed to get your data and set structured arrays that you can then use in workflows to do more cool shit.

tools_choice: this forces the LLM (or not) to use the tool, and create structure.

System: make sure it does it with a bit of prompting (but it does anyways, i was just adding more data).

What I was going to do is try to dynamically create the tools and call structure (apparently) on the fly as and when i needed them, then use the output dynamically in the front end repeating groups and things. ← now that can’t work ‘dynamically’ - why? coz you have to initialise the call and save the ‘new’ structure so it becomes available in the front so Bubble know what to expect - duh!

(unless anyone else knows of a way)

To be fair, once you’ve set your tool up (or multiple), you could use the tool_choice in a workflow to turn them on or off each time and preserve the structure after you’ve initialized.

Or copy the API call, create a tool specific for the jon, initialises and use the data structure.

I am working on this as I am playing with it, so feel free to use my experimentation.

Check out the json assistant plugin as it has function to turn json into a bubble thing which might help you to achieve the dynamic aspect

1 Like

This is actually really handy, and was a part of my process that I had not yet started. I am asking the LLMs to return a bunch of question and each requires an answer (but not stored in the bubble DB) to be used in a secondary part to the LLM for processing and returning new data. This can serve as a store of data in a single DB field for when they wish to save and come back to it (or browser crashes, and save to local storage). The application can then do local processing, plus enhanced LLM retrieval and augmentation of data, hopefully saving some $ along the way in WU eh…

1 Like