Skip to main content
This guide shows you how to update a Flask-based AI app to use the OpenAI API in production while still developing locally with Ollama. By changing only a few environment variables, you can switch between free local development and cost-effective cloud inference.
The image shows the OpenAI developer platform webpage, featuring options to sign up or log in, and information about different AI models like GPT-4o and o1-mini. The sidebar includes links to various capabilities and resources.

1. Create an OpenAI API Key

  1. Sign in or sign up at platform.openai.com.
The image shows a login page for OpenAI, offering options to sign in with an email address, phone, Google, Microsoft, or Apple accounts.
  1. Navigate to Settings → API keys, then click Create new secret key.
  2. Provide a name (e.g., “Ollama app”), assign it to your default project, set permissions, and copy the secret key.
The image shows a webpage for creating a new secret API key on the OpenAI platform, with a form to input details like name, project, and permissions.
  1. Confirm that your new key appears under API keys.
The image shows a webpage from the OpenAI platform displaying API key management, with details of an API key named "ollama-app" including its secret key, creation date, and permissions.
::: note Keep your secret key safe. Do not commit it to version control. :::

2. Choose a Model

Open the Quickstart Guide or the Models Reference to compare models. In this demo, we’ll use gpt-4o-mini.
The image shows a webpage from the OpenAI API documentation, detailing flagship models like GPT-4o and their capabilities, along with a sidebar menu for navigation.

3. Update Your Environment Variables

In your project’s .env file, replace the Ollama endpoint with OpenAI’s and add your secret key:

4. Update the Flask Server

Install the OpenAI Python client and python-dotenv if you haven’t already:
::: warning Always use https://api.openai.com/v1. Requests over HTTP will be rejected with a 403 error. :::

5. Run and Test

  1. Activate your virtual environment:
  2. Start the Flask server:
  3. In your browser, go to http://127.0.0.1:3000, enter a prompt (e.g., “a poem on birds”) and click Generate Poem.
If you accidentally point to http://api.openai.com, you’ll see:
Switching to the https:// endpoint resolves this.
The image shows an AI Poem Generator interface with a text box for input and a button labeled "Generate Poem." Below, there's a section displaying an AI-generated poem.

With just an environment-variable tweak, your app seamlessly transitions from local Ollama LLMs to production-ready OpenAI models.

References

Watch Video