Utility functions for token counting and pricing checks for OpenAI models.
Project description
Danai
Danai is a utility package for token counting and pricing checks for OpenAI models. It provides functions to calculate the cost of tokens generated by OpenAI models, count tokens in text files and strings, and manage pricing data updates.
Features
- Token Counting: Count the number of tokens in text files or strings using specified OpenAI models.
- Pricing Calculation: Calculate the cost of tokens generated by OpenAI models based on pricing data.
- Quick Queries: Quickly generate responses from OpenAI models and print them.
- Directory Summarization: Print directory contents and generate directory trees while ignoring certain files and directories.
- JSON Response Saving: Save OpenAI API responses as JSON files with optional pretty-printing and cost calculation.
Installation
To install Danai, use pip:
pip install danai
Usage
Token Counting
Count tokens in a text file:
from danai import tokencount_file
token_count = tokencount_file("path/to/textfile.txt", model="gpt-4o")
print(f"Token count: {token_count}")
Count tokens in a text string:
from danai import tokencount_text
text = "Your text here"
token_count = tokencount_text(text, model="gpt-4o")
print(f"Token count: {token_count}")
Pricing Calculation
Calculate the cost of tokens generated by an OpenAI model:
from danai import pricecheck
response = ... # Your OpenAI API response object
cost_details = pricecheck(response)
print(cost_details)
Quick Queries
Generate a quick response from an OpenAI model and print it:
from danai import quickprint
prompt = "Your prompt here"
quickprint(prompt, model="gpt-4o-mini")
Directory Summarization
Print the contents of a directory while ignoring certain files and directories:
from danai import print_directory_contents
print_directory_contents(
directory="path/to/directory",
output_dir="path/to/output",
ignore_dirs=[".git", "__pycache__"],
ignore_files=[".DS_Store"],
ignore_extensions=[".pyc"]
)
Generate a directory tree:
from danai import print_directory_tree
print_directory_tree(
directory="path/to/directory",
output_dir="path/to/output",
ignore_dirs=[".git", "__pycache__"]
)
Join summaries of directory contents and tree:
from danai import join_summaries
join_summaries(output_directory="path/to/output")
JSON Response Saving
Save an OpenAI API response as a JSON file:
from danai import jsonsave
response = ... # Your OpenAI API response object
jsonsave(response, filename="response", directory="outputs", overwrite=False, pretty=True, price=True)
CLI Tools
Danai provides several command-line interface (CLI) tools for convenience:
- danai: The main CLI tool.
- tcount: A tool for counting tokens in text files or strings.
- printsetup: A tool for printing setup information.
Using the CLI Tools
Token Counting
To count tokens in a text file using the CLI:
tcount path/to/textfile.txt --model gpt-4o
Print Setup Information
To print setup information using the CLI:
danai printsetup
Requirements
- Python: Requires Python 3.7 or higher.
- Environment Variables: Ensure that the
OPENAI_API_KEYis set as an environment variable. This key is necessary for authenticating with the OpenAI API.
To set the OPENAI_API_KEY environment variable on a Mac, you can add the following line to your .bash_profile or .zshrc file:
export OPENAI_API_KEY="your_openai_api_key"
Then, reload your profile:
source ~/.bash_profile # or source ~/.zshrc
License
This project is licensed under the MIT License. See the LICENSE file for details.
Author
Aidan Coughlan - dev@farfromavocados.com
Project Links
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file danai-0.3.9.1.tar.gz.
File metadata
- Download URL: danai-0.3.9.1.tar.gz
- Upload date:
- Size: 14.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c614d5094d1984300d34a5a337e9edef5c7044a8443c6a97468af5fafda88f3e
|
|
| MD5 |
3176839ba379a542a16d7236be07544e
|
|
| BLAKE2b-256 |
907c336db7ea6d7d1990d64976da948f87f424b40a786b27fe06bc0c125c195b
|
File details
Details for the file danai-0.3.9.1-py3-none-any.whl.
File metadata
- Download URL: danai-0.3.9.1-py3-none-any.whl
- Upload date:
- Size: 14.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
676b9c3f94b405563ebb30b8f0a0be85057d29bc2028ab29386934be85d627b5
|
|
| MD5 |
303de283c36f8a8bc7bd65a8bbd40ed9
|
|
| BLAKE2b-256 |
a6c90751d70aea04333cd7e8b75366b934bcbe232177cd28204babdf7ecaf791
|