OpenAI API Chatbot for ChatGPT
- Getting Started
- Opening the Starter Project
- Creating a Chat Messages List
- Displaying the Chat Response
- Talk to Me: Mimic the Back-and-Forth
- Artificial, for Real
- Creating Typing Animation
- Animate Text Output
- Creating OpenAI API Calls
- Messages Overview
- Engage the Bot! (You Won’t Be Assimilated)
- Puttin’ on the Persona
- Chat Completion Mode
- Limits to Your Adventure
- Adding Another Parameter
- Where to Go From Here?
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
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
Build and run the application. Then, ask “Can you walk from Tokyo to Mount Fuji?”:
Next, restart the application and ask the same question. You get the same reply.
Now, change the
temperature value from
1.0 in the
val temperature = 1.0
Build and run the application. Then, ask the same question:
Then, restart the application. Ask the same question again:
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!