[F/OS] 🧠 Artificial Intelligence and OpenAI!

:computer: 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!

:chart_with_upwards_trend: Stats

:hammer_and_wrench: Built with: Extension Template

:calendar: Released on: 2023-01-14T16:00:00Z

:clock1: Version: 2

:books: Documentation

Method blocks

Chat

image

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 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? If the informative parameter is false, the bot will give a creative response. This is useful for questions requiring imagination. Otherwise, if you are asking for facts, set this as true.

Parameters: prompt = text, apiKey = text, maxTokens = text, informative = boolean

GenerateImage

image

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.

Parameters: prompt = text, apiKey = text

Event blocks

Error

image

This event is fired when an error has occurred.

Parameters: error = text

GotImage

image

This event is fired when the extension has generated an image! You can feed an Image component directly with this URL.

Parameters: imageUrl = text

GotResponse

image

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)

:inbox_tray: Downloads

AIX: com.gordonlu.openai.aix (114.2 KB)

AIA:
OpenAi.aia (118.3 KB)

:green_book: 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.

  1. Go to the OpenAI website (https://openai.com/).

  2. Click on the β€œDevelopers” button in the top right corner of the page.

  3. Click on the β€œAPI” button in the top right corner of the page.

  4. Click on the β€œGet API Key” button in the top right corner of the page.

  5. Fill in the required information and click on the β€œCreate API Key” button.

  6. 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.

:+1: 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, who 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 :heart: 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!

25 Likes

Thank you :heart: What about Unsupported countries can you give me ApiKey ?

Unfortunately, no. API keys are not meant to be shared publicly with a lot of people, and me myself is in an unsupported country. I am relying on someone else's key

Gordon,
Great Job!
Can you provide the source code for the image generator?
Peter

Thank you!

Check the link in GitHub. The full extension source code is there. Check lines 98-145.

1 Like

you may have misled us a bit.

i started it up after providing my API key but i couldn't get it to return any good value (see my first 4 entries below).

so i started looking at the java code and it showed the set up was for connecting to GPT-3 - not ChatGPT! i realized that my trial subscription to GPT-3 had expired after my trial in the summer so i established a paid account and as soon as i did, the app started to work again - the AI was starting to respond. However it was obvious i wasnt connected to ChatGPT from its response to my prompt "make me a poem telling me why i should learn python" - sure it responded with a paragraph - not a poem - and some reasons were good and it seemed to have problems with "\n" (newline)! and it charged me 103 tokens!

look at how the real ChatGPT responded:

and it's still free! (use this link for chatgpt: https://chat.openai.com/chat)

this isnt really a connection to ChatGPT but only to GPT-3. also according to the java code, it was using Ada, the cheapest language model ($0.0004 per 1k tokens generated). most everyone else uses Davinci the most powerful model ($0.0200 per 1k tokens generated). as far as i know OpenAI hasn't released the API yet for ChatGPT. And it's still offering a free trial.

just my observation.
(this article explains the difference between GPT-3 and ChatGPT:
OpenAI API Pricing in Words per Dollar | by Ivan Campos | Sopmac Labs | Jan, 2023 | Medium)

Hi manyone, thank you for your comments. I will try to explain everything, hopefully this can shed some light.

The typo is because you set your tokens too low; the API may cram the answer within the token limit and result in an incomplete answer.

Make sure you disable informative.

Hmm...I'm checking, but my beta tester said that he is using a free trial, in fact, for $18. I changed the model to Ada because other users told me that Davinci is too expensive - one call costs $0.02. Calling Davinci for 900 times may end your free trial. However, I will consider adding different models for the extension.

I may have mistaken ChatGPT and GPT-3. I'll investigate into this. Thanks.

I couldn't decide on this, since OpenAI decides how many tokens you would have to spend. However, a factor of this is the length of the answer. If you get a longer answer, you would have to spend more tokens.

I'm getting "No value for choices".
Even on your provided AIA, the same error is coming.

Hi, did you provide a valid API key? Make sure you added a valid API key from OpenAI. Share with me your project in PM if you still find the error. Check the FAQ Q4 for this as well.

Yes, I provided the API.

I'm contacting you in PM. Hope we can sort out the errors.

EDIT: Sorted out, this user has their account expired before today. Please check and make sure your accounts are still available.

1 Like

Hi @manyone,

@fahadboss10 has confirmed that the extension is working. Can you try again and see if there are errors?

1 Like

There's nothing wrong with the extension. It works. I'm just saying that it's misnamed. The title implies that it's connecting to ChatGPT but it is actually connecting to GPT-3.
So you cannot really do all those wonderful things that you read about that you can do with ChatGPT.

1 Like

No value for choices

Is your API key expired already?

How to change the model from ada to davinci?

It's not possible with this version yet, but I will add this feature in the next release.


Corresponding to @fahadboss10's question about how to format the answers with new lines, it's still not possible with this release, as the result returned from the OpenAI server adds line breakers () in weird locations. However, I'll try to figure out a way to sort things out.

2 Likes

Hello everyone, I hereby present you the :star2: version 2 update of this extension.

New features:

  • Better error detection. The "no value for choices" error should be eliminated, and should be related to an error message including more detail, such as, "You exceeded your current quota, please check your plan and billing details.".

  • Choose your models. Previously I have set the only model to Ada, but now you can choose three more models, i.e. Babbage, Curie and Davinci.

    image

  • Renamed extension to OpenAI to stop the confusion around. @Taifun, please kindly update your directory and @Peter, please update the links in your replies in other communities of this extension. Please remember to update the old extension, convert the blocks to the new one and delete the old extension. Thank you for your kindness.

Plans for next release:

  • Include options to resize images to 256 square and 512 square.

@manyone Is this OK now?

4 Likes

yes - it's looking good thank you - it's nice to be able to select the language model.
i want to suggest something too - the informative parameter seems too restricitive if there are only 2 values - straight and exact answers or "mildly creative" answers. i looked at the code and apparently, this is passed to gpt-3 as the temperature parameter which has a value, respectively, of 0 for exact answers and 0.9 for creative answers. i was wondering if it makes sense to replace the informative parameter with the temperature parameter - so the user can select the wildness of the responses as he wishes. it can be presented as a slider , 0.0 to 1.0 (extremely wild to boring).
also, the first demo didn't have an example for image generation. will the new demo show it this time?

1 Like

It's on Screen2; please check again.