A simple calculator that uses OpenAI's function calling to process mathematical expressions and return results in JSON format.
-
Clone this repository:
git clone https://github.com/s2005/openai-calculator.git cd openai-calculator
-
Set up a virtual environment:
# Create virtual environment python3 -m venv venv # Activate virtual environment source venv/bin/activate # For bash/zsh # OR . venv/bin/activate # Alternative method
# Create virtual environment python -m venv venv # Activate virtual environment .\venv\Scripts\activate # For PowerShell # OR venv\Scripts\activate.bat # For Command Prompt
-
Install the requirements:
pip install -r requirements.txt
-
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
Basic usage:
python calculator.py "2 + 2"
With optional parameters:
python calculator.py --model gpt-4 --temperature 0.2 "23 * 45"
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)
{
"expression": "2 + 2",
"result": 4,
"operation_type": "addition"
}
If an error occurs, the output will be in the following format:
{
"error": "error message",
"expression": "original expression",
"status": "failed"
}
- Python 3.6+
- OpenAI API key
- Required packages (see requirements.txt):
- openai>=1.0.0
- python-dotenv>=0.19.0
- argparse>=1.4.0