This script allows you to experiment with and understand requests and responses to the OpenAI chat completions API. It provides a simple interactive interface where you can ask questions and get responses from the OpenAI model, using your own OpenAI API key. The script maintains conversation context throughout the chat, limited by token size, and includes debugging statements that output the number of tokens used per request, and the number of tokens used across the entire conversation. When the token size limit is reached, the script auto-expires the oldest conversation turn in order to stay below the token limits. This gives the bot a "memory" of recent interactions and allows for the latest context memory to be maintined. The script also allows you to set arbitrary token size limits for experimentation, and includes the full HTTP response payload from the API for inspection, to help you understand how things are working under the hood. Additionally, the script demonstrates basic dynamic prompting, based on GPT-3's determination of conversation sentiment. This is a work in progress and is intended for experimentation and learning only. Enjoy!!!
The following environment variables should be set:
API_KEY
: Your OpenAI API key.LANGUAGE_MODEL
: The name of the language model to use.MAX_RESPONSE_TOKENS
: The maximum number of tokens in the response from the model.TEMPERATURE
: The temperature parameter for generating diverse responses.MAX_CONTEXT_TOKENS
: The maximum number of tokens in the conversation context.
The following dependencies are required:
httpx
: A modern, async HTTP client for Python.rich
: A Python library for rich text and beautiful formatting in the terminal.tiktoken
: A Python library for counting tokens in a string.dotenv
: A Python library for loading environment variables from a.env
file.
You can get your own API key here: https://platform.openai.com/account.
-
Install Homebrew (if not already installed) by following the instructions at https://brew.sh/.
-
Open Terminal and run the following command to install Python:
brew install python
-
Go to the official Python website at https://www.python.org/downloads/windows/.
-
Download the latest version of Python for Windows.
-
Run the downloaded installer and follow the installation instructions.
-
Clone the repository:
git clone <repository_url>
-
Navigate to the project directory:
cd <project_directory>
-
Create a virtual environment (optional but recommended):
python -m venv env
-
Activate the virtual environment:
-
Mac/Linux:
source env/bin/activate
-
Windows:
.\env\Scripts\activate
-
-
Install the required dependencies:
pip install -r requirements.txt
-
Set the required environment variables by creating a
.env
file in the project directory and adding the following lines:API_KEY=<your_api_key> LANGUAGE_MODEL=<language_model_name> MAX_RESPONSE_TOKENS=<max_response_tokens> TEMPERATURE=<temperature> MAX_CONTEXT_TOKENS=<max_context_tokens>
-
Run the script:
python script.py
-
Enter a question when prompted.
-
To exit the script, enter
quit
.
This project is licensed under the MIT License.