Skip to content

A Python calculator using OpenAI's function calling with JSON output

Notifications You must be signed in to change notification settings

s2005/openai-calculator

Repository files navigation

OpenAI Calculator

A simple calculator that uses OpenAI's function calling to process mathematical expressions and return results in JSON format.

Setup

  1. Clone this repository:

    git clone https://github.com/s2005/openai-calculator.git
    cd openai-calculator
  2. Set up a virtual environment:

    On Linux/macOS:

    # Create virtual environment
    python3 -m venv venv
    
    # Activate virtual environment
    source venv/bin/activate   # For bash/zsh
    # OR
    . venv/bin/activate       # Alternative method

    On Windows:

    # Create virtual environment
    python -m venv venv
    
    # Activate virtual environment
    .\venv\Scripts\activate    # For PowerShell
    # OR
    venv\Scripts\activate.bat  # For Command Prompt
  3. Install the requirements:

    pip install -r requirements.txt
  4. Copy .env.template to .env and add your OpenAI API key:

    cp .env.template .env
    # Edit .env and add your OpenAI API key

    Note: On Windows, you can copy the file using:

    copy .env.template .env

Usage

Basic usage:

python calculator.py "2 + 2"

With optional parameters:

python calculator.py --model gpt-4 --temperature 0.2 "23 * 45"

Command Line Arguments

  • expression: The mathematical expression to evaluate (required)
  • --model: OpenAI model to use (default: gpt-3.5-turbo)
  • --temperature: Temperature for response generation (default: 0.0)

Example Output

{
  "expression": "2 + 2",
  "result": 4,
  "operation_type": "addition"
}

Error Handling

If an error occurs, the output will be in the following format:

{
  "error": "error message",
  "expression": "original expression",
  "status": "failed"
}

Requirements

  • Python 3.6+
  • OpenAI API key
  • Required packages (see requirements.txt):
    • openai>=1.0.0
    • python-dotenv>=0.19.0
    • argparse>=1.4.0

About

A Python calculator using OpenAI's function calling with JSON output

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages