Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save 0ut0flin3/cdc50eb1018c0da1824fa26334ccd4e8 to your computer and use it in GitHub Desktop.
Save 0ut0flin3/cdc50eb1018c0da1824fa26334ccd4e8 to your computer and use it in GitHub Desktop.

In this short guide, I'll show you how you can make variables in your code accessible from GPT-3, then be modified based on GPT-3 responses.

In the guide below, Python will be used for examples, but the method can be used for any programming language.

So, let's say we have a Python variable called FLAG with value False, so FLAG=False. Let's imagine that we want the value of the FLAG variable to become True only if we ask GPT-3 for something specific, or rather, if GPT-3 will answer us with a certain sentence. To achieve this we must instruct GPT-3 to always respond in the exact same way if we dictate a specific request to it. For example, let's pretend that in our program there is a game mode called "hard mode" and that the FLAG variable should be False if it is off and True if it is on. We can simply tell GPT-3 to always respond with the string ::HARD_MODE_ENABLED:: if we ask it to enable "hard mode" and to always respond with the string ::HARD_MODE_DISABLED:: if we ask it to disable it. Then to modify the FLAG variable we could simply do something like this:

if response.choices[0].text=='::HARD_MODE_ENABLED::':
   FLAG=True

if response.choices[0].text=='::HARD_MODE_DISABLED::':
   FLAG=False

the whole code will be something like this:

import openai
openai.api_key='sk-XXX'

FLAG=False
while True:
      q=input("\n\nME: > ")


      response = openai.Completion.create(
                        model="text-davinci-003",

                        prompt="Human: If I ask you to enable hard mode always reply with '::HARD_MODE_ENABLED::', if I ask you to disable hard mode always reply with '::HARD_MODE_DISABLED::'\nAI:Ok, I will do\nHuman: "+q+"\nAI:\n",
                        temperature=1,
                        max_tokens=3000,
                        top_p=1,

                        frequency_penalty=0.0,
                        presence_penalty=0.0,
                        stop=[" Human:", " AI:"],

                      )

      
      if response.choices[0].text=='::HARD_MODE_ENABLED::':
         FLAG=True

      if response.choices[0].text=='::HARD_MODE_DISABLED::':
         FLAG=False
      
      print("\n\nAI: "+response.choices[0].text)

Practically, with this code, if you ask GPT to activate the "hard mode" the variable FLAG in your code will assume the value True, while if you ask to deactivate it it will assume a value False. Lo and behold, you've given GPT-3 access to your code's variables!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment