Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Generative AI With Amazon Bedrock and Unity3D

Posted on Oct 6 • Originally published at Medium A few days ago, Amazon made a big announcement: the service Bedrock was finally released.If you missed the news, here it is: Amazon decided to enter massively into the generative AI field and invested in the company Anthropic for $4 billion in order to compete with popular AI services like ChatGPT, Dall-E, MidJourney, etc. The first proposal of Amazon is Bedrock, a fully managed AI service that offers multiple foundational models such as Claude, Jurassic, and Stable Diffusion XL, among others.In my previous articles, I created Unity3D applications communicating with ChatGPT and Dall-E. Let's do the same with Bedrock!Do you prefer something more interactive instead of reading? Check my video!Here is the general architecture of our application:We use 3 Amazon services:If you want to get further adding a login mechanism with Cognito, please refer to my previous article: Connecting Unity3D, AWS, and ChatGPT Together.First, we have to enable the foundational models we want to use. By default, all the models are disabled. Go to the Bedrock console and enable the models you will use in the model access section. If you have an updated payment method, activating the models only takes a few minutes.If you don't enable the models in the Bedrock console, you will receive this error in Lambda:If you follow my work, you know I'm a Python guy, so I will use the Python SDK for AWS, Boto3. Good news: Bedrock is available on Boto3; bad news: Lambda runs an old version of Boto3 in which Bedrock does not exist yet.💡 Here is a workaround: creating a new Lambda layer with an up-to-date version of Boto3. This is how to do it:1) In PyCharm, create a new project and install boto3 in a new virtual environment. Boto3 and all the dependencies will be installed.2) Go to the venv folder of your project and copy the lib folder into a new folder called python. Then, zip it.3) Go to the Layers section of the Lambda console and create a new layer. Upload the zip file we have created before and choose the same Python version as your local Python virtual environment for the runtime.We create 2 Lambda functions, one for text generation and the other one for image generation, both with the same version as previously for the runtime (In my case, Python 3.9), and we add the layer we have created before.For the text generation, I use the Claude model and implement it as described in the Amazon documentation.For the image generation, I use the Stable Diffusion XL model and implement it, as described in the Amazon documentation.Note that the model IDs can be found in the Bedrock documentation.In order to allow Lambda to invoke a Bedrock model, we create a new managed policy and add it to the Lambda functions roles:In the API Gateway console, we create a new REST API with 2 resources: image and text. Each resource has a post method integrating with the Lambda functions we have created before.When deploying the API, you will be asked to create a new stage. After the creation of the stage, the final endpoint will be shown. You will have to use this URL in the Unity client to perform the request.The Unity client allows the user to send a message to generate a text or an image as an answer:First, we need to create serializable classes to embed the sending and receiving data:Sending a request in Unity is an asynchronous process. Therefore, we create a coroutine that sends the request to the server:If we ask for an image generation, the result sent by Bedrock is a base64 image. To show it on the Unity UI, we must convert it into bytes and create a Texture2D. Then, we assign the texture to the RawImage component of the UI.Here is the result with text and image generations:Let's check with the AWS Calculator and the Bedrock pricing sheet what our system cost would be for a very pessimistic scenario: you love the app, and you perform 2,000 requests a month. 1,000 for text generation with the Claude model and 1,000 for image generation with Stable Diffusion XL.Total: The total bill for our system would be approximately 28 USD monthly. It's quite affordable!Using Bedrock is very intuitive, and integrating it with other Amazon services is absolutely straightforward. The fact that Bedrock is an in-house product makes the integration easier: there is no need for API keys or external requests to make it work, resulting in greater performance and security.Every code of this article has been tested using Unity 2021.3.3 and Visual Studio Community 2022 for Mac. The mobile device I used to run the Unity app is a Galaxy Tab A7 Lite with Android 11.Thanks for reading until the end!Here is the resource mentioned in the article:Templates let you quickly answer FAQs or store snippets for re-use. Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink. Hide child comments as well Confirm For further actions, you may consider blocking this person and/or reporting abuse Pierre Chollet - Oct 5 Ercan - Sep 26 Ninthsun - Sep 25 Victor Isaac Oshimua - Sep 25 Would you like to become an AWS Community Builder? Learn more about the program and apply to join when applications are open next. Once suspended, aws-builders will not be able to comment or publish posts until their suspension is removed. Once unsuspended, aws-builders will be able to comment and publish posts again. Once unpublished, all posts by aws-builders will become hidden and only accessible to themselves. If aws-builders is not suspended, they can still re-publish their posts from their dashboard. Note: Once unpublished, this post will become invisible to the public and only accessible to Alexandre Bruffa. They can still re-publish the post if they are not suspended. Thanks for keeping DEV Community safe. Here is what you can do to flag aws-builders: aws-builders consistently posts content that violates DEV Community's code of conduct because it is harassing, offensive or spammy. Unflagging aws-builders will restore default visibility to their posts. DEV Community — A constructive and inclusive social network for software developers. With you every step of your journey. Built on Forem — the open source software that powers DEV and other inclusive communities.Made with love and Ruby on Rails. DEV Community © 2016 - 2023. We're a place where coders share, stay up-to-date and grow their careers.



This post first appeared on VedVyas Articles, please read the originial post: here

Share the post

Generative AI With Amazon Bedrock and Unity3D

×

Subscribe to Vedvyas Articles

Get updates delivered right to your inbox!

Thank you for your subscription

×