Search code examples
pythonopenai-api

Trying to Insert an input function using Python in OpenAI


import os
import openai
openai.api_key = 'API_Key'
Question = "\\n\\nQ: input("Enter Question")?\\nA:",
response = openai.Completion.create(
model="text-davinci-003",
prompt= Question,
temperature=0,
max_tokens=100,
top_p=1,
frequency_penalty=0.0,
presence_penalty=0.0,
stop=\["\\n"\]
)
print( str(response\['choices'\]\[0\]\['text'\]))`
ERROR
File "C:\\Users\\yus\\PycharmProjects\\pythonProject\\TEST.py", line 6
Question = "\\n\\nQ: "Input_Question"  \\nA:"
^
SyntaxError: invalid syntax
Process finished with exit code 1

Expecting an input function to ask to enter a question and then print the answer using OpenAI.


Solution

  • this is how is done

     def main():
          form = cgi.FieldStorage()
          IN = form.getvalue("query")
          QT = f"Q: {IN}  ? A:"
          response = openai.Completion.create(
            model="text-davinci-003",
            prompt=QT,
            temperature=0,
            max_tokens=64,
            top_p=1,
            frequency_penalty=0,
            presence_penalty=0,
            stop=["\n\n"]
          )
          print(str(response['choices'][0]['text']))
       
      if __name__ == "__main__":
          main()