My Experience with Azure OpenAI and Android

The InfernoRed team has been working with Azure OpenAI on several projects and building some awesome stuff. Here's a recap of how we got started with OpenAI and Android!

My Experience with Azure OpenAI and Android

In this new AI age, the acceleration in artificial intelligence has opened up a new frontier in opportunities for developers and businesses alike. Language models like ChatGPT and other AI technologies are currently experiencing high demand and there are a lot of providers that allow us, developers, to incorporate these new emerging technologies into our existing products with ease. One such tool is Azure OpenAI by Microsoft.

What is Azure OpenAI?

Azure OpenAI is a cloud-based service provided by Microsoft Azure that offers access to advanced artificial intelligence models and tools developed by OpenAI. It allows developers to integrate AI technologies such as LLMs and machine learning into products and applications. With Azure OpenAI, users can deploy, manage, and scale AI models, using Microsoft's cloud infrastructure. To learn more about what Azure OpenAI has to offer click here.

Sounds Awesome! How can I get started? There is one catch, for now...

Microsoft's Azure OpenAI was not available to the general public at the time of writing this blog post. You need a corporate email address account. This means that you will need company authorization and account set up to start using these services. It is likely that over time the Azure OpenAI features will be publicly available and some of the requirements to be used would have been changed. That being said, from now on, this post will assume that you have this account available and that your company has a paid subscription to Azure OpenAI services approved and ready.

Accessing Azure OpenAI Studio

Once you have you account ready, you can visit this portal. In here you will find the Azure OpenAI studio. It will look something like this:

There is a lot to unpack here but for the purpose of this blog, we are going to keep it simple.

Setting up your first deployment

From the start, we can see that we have no active deployments yet. This is where you can select the OpenAI model we want to work with. Some of the models you can choose from are:

  1. GPT 3.5 Turbo
  2. GPT 4
  3. GPT 4 (32K)
  4. Text Embedding Ada

For this specific exercise we are going to use “gpt-35-turbo” since is the model with the less token utilization rate at the moment of writing this blog. If you are going to incorporate image analysis or generate large context windows, gpt-4 is for you. Leave the model version as default and give it a name. Click on "Create". Once the model is deployed, you can see the details like this:

Now its time to have some fun at the Playground. Click "Open in Playground".


Model Playground


There is a lot of information and settings to see here and it can be a little intimidating at first but lets dissect this screen.

First, the setup component allow us to give some context to our model about the specific tasks it was created for. This is what the system message box is for. Let's for example use one of the predefined templates, "JSON Formatter Assistant", this will give us a system message in the form of :

Assistant is an AI chatbot that helps users turn a natural language list into JSON format. After users input a list they want in JSON format, it will provide suggested list of attribute labels if the user has not provided any, then ask the user to confirm them before creating the list.

This way, when we create prompts for our model, the system can better understand the ask and respond more accurately.

Let's do some chatting!

For now lets use the default template. This will give the system message of " You are an AI assistant that helps people find information." In the chat box you can start chatting with the model as any other AI Chatbot.

As you can see, we can see our message request and the system response in chat mode. One neat feature about Azure OpenAI is the ability to see the user request and the system responses in JSON format. This will become very important when implementing OpenAI into our Android Apps. To see this, select "Show JSON". For the previous prompt we can see the JSON response like:

[
  {
    "role": "system",
    "content": "You are an AI assistant that helps people find information."
  },
  {
    "role": "user",
    "content": "How do you even Android?"
  },
  {
    "role": "assistant",
    "content": "\"Android\" can refer to a few different things, but I'll assume you're asking about using an Android smartphone or tablet. To get started with an Android device, here are some basic steps:\n\n1. Power on your device: Press and hold the power button until the screen lights up.\n\n2. Set up your device: Follow the on-screen instructions to set up your device, including connecting to a Wi-Fi network, signing in with your Google account, and customizing your settings.\n\n3. Navigate the interface: Android devices typically have a home screen where you can access your apps and widgets. You can swipe left and right to access different home screens, and swipe down from the top of the screen to access the notification panel.\n\n4. Install apps: Open the Google Play Store app to download and install new apps onto your device.\n\n5. Customize your device: Android devices allow for a high degree of customization, including changing wallpapers, adding widgets, and rearranging app icons.\n\nIf you have specific questions about using an Android device, feel free to ask!"
  }
]

This is cool and all but how can I incorporate this into my Android apps?

We can incorporate this AI model to Android applications using RESTful APIs. We can have a retrofit client that allow us to communicate with the model in-app. This post will assume that you have basic understanding of Retrofit, Kotlin, Moshi and Android Development. Lets give an example:

First, lets create the root data model that we need for this API. All the child data classes can be generated after this one:

@JsonClass(generateAdapter = true)
data class AzureAiResponse(
    val choices: List<Choice> = listOf(),
    val created: Int = 0,
    val id: String = "",
    val model: String = "",
    @Json(name = "object")
    val completionObject: String = "",
    @Json(name = "prompt_filter_results")
    val promptFilterResults: List<PromptFilterResult> = listOf(),
    val usage: Usage = Usage(),
)

Now that we have our data models, we can create the retrofit API like so:

interface AzureOpenAiApi {
    @POST(Constants.AZURE_ENDPOINT)
    suspend fun getAiResponse(
        @Body request: AzureAiRequest,
        @Query("api-version") apiVersion: String = Constants.AZURE_API_VERSION,
        @Query("api-key") apiKey: String = BuildConfig.azureAiKey,
    ): Response<AzureAiResponse>
}

Where is the information that is needed here? Go to the chatbot and click "View Code". Here you can see the the Base URL, API version and API Key needed to make calls. After this it should be a question of creating a response repository in order to use these calls in your app.

One thing to point out is that on the chat playground we can see that for each new entry that we ask our model, the system sends the whole chat thread and then just generates a new response. As a good rule of thumb, we can do the same in our application and each time we do a POST, we send the whole thread. This will give the system more context and it will produce more accurate responses over time.

Conclusion

Azure OpenAI, a service by Microsoft Azure, provides developers access to advanced AI models like GPT 3.5 Turbo and GPT 4. Access requires a corporate email address and company authorization, free personal email accounts such as Gmail, Hotmail, or Outlook will be denied access at this time. Once granted, developers can use Azure OpenAI Studio to set up deployments and interact with AI models. Integration with Android apps is possible through RESTful APIs, using tools like Retrofit and Moshi. By creating appropriate data models and API interfaces, developers can seamlessly incorporate AI capabilities into their applications. Azure OpenAI offers a powerful platform for driving innovation in AI-powered projects.

Happy Coding!