Client Chat GPT API – Python

Code

#gpt.py
import openai,sys

API_KEY = ""
# API_KEY = os.environ['api_key']

def generate_response(api_key, prompt):
    openai.api_key = api_key

    try:
        response = openai.Completion.create(
            engine="text-davinci-003",
            prompt=prompt,
            max_tokens=1024,
            n=1,
            stop=None,
            temperature=0.5,
        )

        message = response["choices"][0]["text"]
    except openai.OpenAIError as error:
        message = "Error: {}".format(error)

    return message

prompt = sys.argv[1]
response = generate_response(API_KEY, prompt)
print(response)

Usage

python3 gpt.py "Qu'est ce que le hacking ?"