Introduction
Do you want to chat with AI? Generate an image from your command? Well, this is the extension for you!
This is an extension that allows you to integrate OpenAI and GPT-3 with AppInventor. You can ask OpenAI questions, chat with it, and even generate images!
Stats
Built with: Extension Template
Released on: 2023-01-14T16:00:00Z
Version: 5
Documentation
Method blocks
Chat
Chats with OpenAI. The
prompt
parameter specifies what the query/question is, such as "What color is the sky?" or even better questions.
The prompt parameter tells the extension the question that you want to ask.
The model parameter defines the language model that you will use. Here are some possible combinations.
A1: gpt-3.5-turbo
A2: gpt-3.5-turbo-0301Note: in order to use the models B1 to B4, you must sign up for a waitlist and wait for OpenAI for approval. Find the waitlist here: https://openai.com/waitlist/gpt-4-api
B1: gpt-4
B2: gpt-4-0314
B3: gpt-4-32k
B4: gpt-4-32k-0314
The apiKey parameter tells the extension the API key of your OpenAI account.
The maxTokens parameter limit the length of the answer that you will get. For example, in order to get an answer of around one or two sentences, set this as 250 to be safe. You may get a short or imcomplete answer if you set this too low. All free users can spend 150K tokens every minute.
Do you want an informative or a creative answer? The
temperature
parameter defines this. This should be a decimal between 0 and 1, with values closer to 1 returning more informative answers, and values closer to 0 returning more creative answers.The role parameter specifies the role of the user - do you want to talk to the OpenAI bot as a user or an assistant?
Parameters: prompt = text, apiKey = text, maxTokens = text, temperature = number (double)
GenerateImage
Generates an image according to the prompt. The prompt tells the extension what the image is about, such as "Monkey holding a banana". The apiKey tells the extension your OpenAI API key. The size parameter defines the size of the returned image. Please only use helper blocks for this part.
Parameters: prompt = text, apiKey = text
ModerateText
The moderation API is a service provided by OpenAI that allows you to check whether a text contains inappropriate or offensive information. Put the text that you want to analyze in the input parameter, and supply the block with an API key.
Parameters: input = text, apiKey = text
Stream
Functions the same as the Chat block, but gets the response as chunks so that you do not have to wait until OpenAI finishes generating your response. This is like the "typing" view you see in ChatGPT. The id parameter is just for you to keep track of the response; you can set it to any integer. The response won't be affected by the ID parameter. For the other parameters please check the documentation for the Chat block.
Credit to @David_Ningthoujam for reference and his help.
Parameters: id = number (int), prompt = text, apiKey = text, maxTokens = text, temperature = number (double)
Event blocks
Error
This event is fired when an error has occurred.
Parameters: error = text, block = text
FinishedStream
This event is fired when all chunks of the stream are returned via the GotStream event.
GeneratedImage
This event is fired when the extension has generated an image! You can feed an Image component directly with this URL.
Parameters: imageUrl = text
GotStream
This event is fired when OpenAI has responded to your stream request. The response parameter is the responded chunk.
Parameters: id = number (int), response = text
ModeratedText
This event is fired when OpenAI has moderated the text you inputted in the ModerateText block! 'flagged' refers to whether the text contains indecent information in general.
categoryFlags
includes a dictionary of 7 types of inappropriate information (hate, hate/threatening, self-harm, sexual, sexual/minors, violence, violence/graphic). These 7 are the keys of the dictionary. Corresponding to them are boolean values for whether the text has this type of inappropriate information. Sample response:
categoryScores
includes the same 7 types as the keys, but corresponding to them are decimals from 0 to 1. Values closer to 0 mean that the text violates less in this category; vice versa.Parameters: flagged = boolean, categoryFlags = dictionary, categoryScores = dictionary
RespondedToChat
This event is fired when the extension has responded to your question. The tokensSpent parameter indicates how many tokens you have spent from your OpenAI account in order to ask this question.
Parameters: response = text, tokensSpent = number (int)
Downloads
AIX:
com.gordonlu.openai.aix (134.1 KB)
AIA:
OpenAIV5.aia (146.6 KB)
π History
V4: com.gordonlu.openai.aix (124.7 KB)
V3: com.gordonlu.openai.aix (120.0 KB)
V2: com.gordonlu.openai.aix (114.2 KB)
FAQ
Q1: Why is my answer incomplete? Is this a bug in the extension?
A1: No, this is not a bug. Try to increase your maxTokens parameter for this. The maxTokens parameter judges how long the response should be, and with lower tokens, you get shorter, or maybe incomplete, answers.
Q2: How do I obtain an API key?
A2: @Dayron_Miranda was generous enough to give us a few steps. Not all countries support OpenAI though, for example, we Chinese do not get to use OpenAI.
Go to the OpenAI website (https://openai.com/).
Click on the βDevelopersβ button in the top right corner of the page.
Click on the βAPIβ button in the top right corner of the page.
Click on the βGet API Keyβ button in the top right corner of the page.
Fill in the required information and click on the βCreate API Keyβ button.
Once you have your API key, you can test GPT chat by using one of the many libraries or sample codes available on the OpenAI website, or by making a call to the API endpoint directly with the API key. In this instance, copy the API key for this extension to work.
Q3: Is there a quota for this?
A3: Yes, unfortunately. Please see Pricing for details.
Q4: Why do I get a "No value for choices" error?
A4: This is caused maybe because you include a special character. Do not use double quotes in your prompts ("), use single quotes ('). Also check if you completed the setup correctly.
Q5: Where is the source code?
A5: It's in this repository in GitHub.
Credits
Thanks again to @Kumaraswamy for guidance and help in creating the extension, also @shreyash for his tutorials in creating the extension! (it's here: Learn how to create extensions - Resources - Kodular Community)
Also, special thanks to @Dayron_Miranda, @AyProductions, @Darko and a donor who wished to remain anyonymous, who all provided an API key for me to test this extension, and also participating in the beta-test program himself. Shoutout to him! Without his help, this extension would not be possible. (OpenAI is not supported in Hong Kong)
Please, if you like this extension or if you want to support me, click the beautiful Like button of this extension, this took two whole mornings and one whole afternoon to make, and I genuinely appreciate your kindness. Have a nice day