PYTHON AI Coding Labs

Below is a simple example of how you can use Python to send a prompt to the OpenAI API and display the response.
This example uses the requests library to send a POST request to the OpenAI API.
Ensure that you have installed the requests library (pip install requests) before running this code.
Please replace "your_openai_api_key_here" with your actual OpenAI API key.

import requests

def ask_openai(prompt):
url = ""
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 =, headers=headers, json=data)
# Check the status code of the response
if response.status_code == 200:
print(f"Error: {response.status_code}")

# Example usage
ask_openai("What is the capital of France?")

Important Note:

Ensure to handle the API key securely and don't expose it in the source code directly for production-grade applications.
Use environment variables or configuration files to manage API keys and other sensitive information securely.
Handle the API rate limits and errors appropriately to ensure the robustness of the code.
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
) instead.