Skip to main content

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)

License

This project is licensed under the MIT License. See the LICENSE file for details.

Author

Aidan Coughlan - dev@farfromavocados.com

Project Links

# 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:

```python
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)

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

danai-0.3.9.tar.gz (13.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

danai-0.3.9-py3-none-any.whl (14.4 kB view details)

Uploaded Python 3

File details

Details for the file danai-0.3.9.tar.gz.

File metadata

  • Download URL: danai-0.3.9.tar.gz
  • Upload date:
  • Size: 13.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.6

File hashes

Hashes for danai-0.3.9.tar.gz
Algorithm Hash digest
SHA256 0f03de67c3f6161c7fed54f643c41d482713eb03c4f5bb58b3a28bfcc08b0780
MD5 ceba95ff181eb37954f21e802e56f61b
BLAKE2b-256 5387ee7270853a624beab6bdc4edd31af2c3b09a256d5d00b7412cc1195f958a

See more details on using hashes here.

File details

Details for the file danai-0.3.9-py3-none-any.whl.

File metadata

  • Download URL: danai-0.3.9-py3-none-any.whl
  • Upload date:
  • Size: 14.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.6

File hashes

Hashes for danai-0.3.9-py3-none-any.whl
Algorithm Hash digest
SHA256 deafe7d6e3e987b2d5733b5d76fbbc003e9bb2f5f7bb65163fd2ecbc15daa5ae
MD5 ffa17732aa8d1c47a18e2940d22fc2fc
BLAKE2b-256 11329fcad2face12c3f7d6bbb9e7ffefec811be0a6e74e3e9d14b9ce7cde9301

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page