import requests
def ask_openai(prompt):
url = "https://api.openai.com/v1/engines/davinci/completions"
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer your_openai_api_key_here"
}
data = {
"prompt": prompt,
"max_tokens": 150 # you can change this number to control the length of the output
}
response = requests.post(url, headers=headers, json=data)
# Check the status code of the response
if response.status_code == 200:
print(response.json()['choices'][0]['text'])
else:
print(f"Error: {response.status_code}")
print(response.text)
# Example usage
ask_openai("What is the capital of France?")