Skip to content

Structured Output OpenAI Showcase. A Prime Numbers Calculator that demonstrates OpenAI's structured output capabilities. This repository is public because current LLM examples often use outdated API calls, and this script aims to help users quickly experiment with structured outputs.

License

Notifications You must be signed in to change notification settings

TLIEPE/demo-openai-primes

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Prime Numbers Calculation Script by TLIEPE

Overview

This script is a showcase of a new approach to generating structured output using OpenAI's beta capabilities for structured outputs in its API. The script demonstrates a comparison between prime numbers generated locally and those generated through the OpenAI API using structured outputs.

The structured output capability of OpenAI helps ensure responses adhere to a defined format, validated using tools like Pydantic. In this case, it ensures a list of prime numbers is returned in a consistent manner.

How It Works

The script calculates prime numbers in two different ways:

  1. OpenAI API (with structured output): The script sends a request to OpenAI's API to provide all prime numbers up to a user-specified limit. The response is validated using a Pydantic model (PrimeNumberResponse) to ensure structured and predictable output.

  2. Local Calculation: The script uses the SymPy library to compute the same prime numbers locally.

Both results are displayed side by side, allowing you to compare the numbers generated by the OpenAI API and the local computation.

Code Flow

  1. API Key Setup: The OpenAI API key is set using an environment variable named OPENAI_API_KEY. Make sure this variable is set correctly before running the script.
  2. User Input: The user is prompted to provide an upper limit for calculating prime numbers.
  3. OpenAI API Request: The script sends a structured request to OpenAI, asking it to generate the prime numbers up to the user-provided limit.
  4. Local Computation: The script also calculates the prime numbers locally using SymPy's primerange function.
  5. Comparison Display: Both sets of results are displayed side by side for easy comparison, indicating whether they match.

Prerequisites

Before running the script, you'll need to set up a virtual environment and install the required libraries.

Setting up a Virtual Environment on macOS

To set up a virtual environment, follow these steps:

  1. Open Terminal
  2. Navigate to your desired directory:
    cd /path/to/your/project
  3. Create a virtual environment named macenv:
    python3 -m venv macenv
  4. Activate the virtual environment:
    source macenv/bin/activate
  5. Install the required libraries:
    pip install openai pydantic sympy

After setting up the virtual environment and installing the libraries, you're ready to run the script.

Running the Script

  1. Make sure the virtual environment is activated (source macenv/bin/activate).
  2. Set the OpenAI API key as an environment variable:
    export OPENAI_API_KEY=your_openai_api_key_here
  3. Run the script:
    python3 main.py

Notes

  • Structured Output Beta Feature: This script leverages OpenAI's beta feature for structured outputs, as detailed here.
  • MIT License: This project is licensed under the MIT License, meaning you are free to use, modify, and distribute it as long as proper credit is given.

License

MIT License. See LICENSE for more details.

Contributing

Feel free to open issues or submit pull requests if you'd like to contribute or have ideas for improving this project.

About

Structured Output OpenAI Showcase. A Prime Numbers Calculator that demonstrates OpenAI's structured output capabilities. This repository is public because current LLM examples often use outdated API calls, and this script aims to help users quickly experiment with structured outputs.

Topics

Resources

License

Stars

Watchers

Forks

Languages