This project demonstrates how to use OpenAI's GPT model to process a list of strings and return valid English words using the API. The project also tracks the number of tokens used during the API request.
- Python 3.x: The programming language used for the implementation.
- httpx: A modern Python HTTP client used to send requests to the OpenAI API.
- OpenAI API: A service that provides access to the GPT models for text processing.
/token-count-api
│
├── main.py # Main Python script to interact with the OpenAI API and track tokens
├── requirements.txt # Python dependencies for the project
├── LICENSE # License file for the project
├── README.md # This documentation file
-
Clone the repository (or copy the code) to your local machine.
-
Install the required dependencies:
pip install -r requirements.txt
-
Run the script:
python token_count.py
This will send a request to OpenAI’s API with a predefined list of strings, asking for valid English words. It will also print the number of tokens used during the request.
This project integrates with OpenAI’s API to process messages and return valid English words. The script constructs a JSON payload and sends a POST request to the endpoint using the httpx library.
- Endpoint:
https://api.openai.com/v1/chat/completions - Model:
gpt-4o-mini - Request Format: A list of strings passed as the content of the message, with the goal of extracting valid English words.
{
"role": "user",
"content": "List only the valid English words from these: y5W1djP1, eQ0GWWg, rhVr, ffXzPDs, zTkSH8Sb, JaKjT5"
}{
"usage": {
"total_tokens": 20
}
}Running the script will print the following output:
Number of tokens: 20
This output shows the number of tokens consumed by the API request, which is useful for tracking usage and costs.
This project is licensed under the MIT License.
Feel free to open issues or create pull requests if you'd like to contribute to this project!
For any questions or feedback, feel free to reach out.
/token-count-api
│
├── token_count.py # Main Python script to interact with the OpenAI API and track tokens
├── requirements.txt # Python dependencies for the project
├── LICENSE # License file for the project
├── README.md # This documentation file
httpx==0.23.0