AWS Bedrock Claude 2.1 - Return only JSON
December 7, 2023Working with the AWS Bedrock API is an exhilarating experience! I came across an interesting business case where I needed to develop an AI MVP. The MVP generates JSON data based on a prompt and utilizes the anthropic.claude-v2:1 model in AWS Bedrock.
I encountered an issue where the response I received was not pure JSON. It contained additional characters that I couldn't remove like:
" format: {\"one\":\"Supplier\",\"many\":\"Time\"}"
Seeking help from the AWS Community, I was able to find a solution to this problem. In this post, I will share the solution with you.
The Problem
Human: $YOUR_PROMPT . Answer in JSON formatAssistant:{,
This technique is known as "Put words in Claude’s mouth". It involves providing a prompt to Claude and letting it generate the rest of the response on its own. While there may be alternative approaches to solving this issue, I am currently happy with this solution.
Thanks
I would like to express my gratitude to the AWS Community for their invaluable assistance in helping me resolve this issue.
A special thanks goes to Corvus Lee for providing the advice that ultimately solved the problem.
I would also like to thank Ken Collins for bringing the Claude 2 docs sheet to my attention.
Once again, thank you all for your support and contributions.
Conclusion
Working with AWS Bedrock AI is incredibly enjoyable. The field is constantly evolving, and there is always something new to learn. In this post, I demonstrated how to obtain a pure JSON response from AWS Bedrock Claude 2.1. As AI technology continues to advance rapidly, you may not encounter this issue in the future. However, if you are working with Claude 2.1 or newer, be sure to refer to the documentation for more information.
I hope you found this post helpful, and I look forward to sharing more with you in the future.
I love to work on Open Source projects. A lot of my stuff you can already use on https://github.com/mmuller88 . If you like my work there and my blog posts, please consider supporting me on the:
OR
And don't forget to visit my site