OpenAI API Chatbot for ChatGPT

Go on an adventure into AI! In this tutorial, you’ll use the OpenAI ChatGPT API to create a chatbot in Android and create your own experience with this user-friendly machine learning technology. By arjuna sky kok.

Leave a rating/review
Download materials
Save for later
Share
You are currently viewing page 4 of 4 of this article. Click here to view the first page.

Limits to Your Adventure

ChatGPT has a context length limit, so consider your session’s chatting history. Either cut the older chatting history or summarize it somehow. For example, the model you’ve used in this tutorial, gpt-3.5-turbo, has a 4,096-token context length. It means your prompt and the completion result should not exceed 4,046 tokens. The longer your prompt, the shorter the completion result limit is.

Learn more about how to count tokens in this OpenAI article. OpenAI has other models that can handle more than 4,096 tokens, such as gpt-3.5-turbo-16k, which can handle 16,384 tokens.

Adding Another Parameter

One more useful parameter, temperature, adjusts the randomness of the response. The lower the temperature value, the less random or more deterministic the result will be. This parameter can take a value from 0 to 2. The default value is 1.

To add another parameter like temperature, open ChatModel.kt and replace // TODO: Add temperature with the following code:

  val temperature: Double

This adds another item to the ChatRequest payload you send to the OpenAI API server. Then, you need to initialize the ChatRequest properly in ChatRetriever.kt.. Find the line below in the retrieveChatmethod:

  val requestBody = ChatRequest("gpt-3.5-turbo", messages)

Then, replace it with the following code:

  val temperature = 0.0
  val requestBody = ChatRequest("gpt-3.5-turbo", messages, temperature)

In the code above, you construct a ChatRequest object with the additional parameter temperature.

Build and run the application. Then, ask “Can you walk from Tokyo to Mount Fuji?”:

Using the temperature parameter

Next, restart the application and ask the same question. You get the same reply.

Now, change the temperature value from 0.0 to 1.0 in the ChatRetriever class retrieveChatmethod:

    val temperature = 1.0

Build and run the application. Then, ask the same question:

Using 1.0 for the temperature value

Then, restart the application. Ask the same question again:

Different result from using 1.0 for the temperature value

Although the core message remains the same, the wording differs.

And there you have it! You’re ready to dispense a trek story up to Mount Fuji, as any been-there travel agent could give. What will be your next adventure with ChatGPT?

Where to Go From Here?

Download the completed project files by clicking the Download Materials button at the top or bottom of the tutorial.

Congratulations! You have successfully constructed Chatty Bot, an Android chatbot application using the OpenAI API. Throughout this tutorial, you learned how to build the chat dialog UI and compose API calls to the OpenAI API server.

However, you need not stop there. You can add other features, such as obtaining audio inputs. Typing text can be tedious, whereas speaking is more convenient. OpenAI provides a speech recognition API service known as Whisper. Your chatbot will be more user-friendly if you implement this feature.

This project uses the Retrofit library. However, this tutorial does not cover its usage. If you would like to learn more about it, enroll in a Retrofit course.

Likewise, if you need help understanding some of the Jetpack Compose concepts used in this tutorial, check out our book Jetpack Compose by tutorials.

We hope this tutorial proves useful to you. If you have any questions or comments, please join the forum discussion below!