Skip to main content

A utility package for simplifying interactions with OpenAI models.

Project description

Danai

Danai is a comprehensive utility package designed to streamline interactions with OpenAI models. It offers a suite of tools for various tasks including token counting, pricing calculations, quick queries, directory summarisation, and JSON response management.

Danai simplifies the process of moving between different output types – particularly with structured data – and assists with managing and analysing token usage and costs associated with OpenAI's powerful language models.

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, which is routinely updated from OpenAI.
  • Quick Queries: Quickly generate responses from OpenAI models and print them to the console or to timestamped files.
  • Directory Summarisation: Print directory contents to collated markdown files 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 Summarisation

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_KEY is 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

Contributing

We welcome contributions from the community! If you would like to contribute to Danai, please follow these steps:

  1. Fork the repository.
  2. Create a new branch for your feature or bugfix.
  3. Make your changes and commit them with clear and concise messages.
  4. Push your changes to your fork.
  5. Submit a pull request to the main repository.

Please ensure that your code adheres to the project's coding standards and includes appropriate tests.

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.3.tar.gz (16.6 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.3-py3-none-any.whl (17.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: danai-0.3.3.tar.gz
  • Upload date:
  • Size: 16.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for danai-0.3.3.tar.gz
Algorithm Hash digest
SHA256 82baa48fc655db312ce205bd0276ba74c4c57d9bdd638f063cee6506cb44285a
MD5 c648631a93570366784c20b4a46d1262
BLAKE2b-256 8b0077c56a9d784fcb803e0b17c4fd4ad6e3e9daff9b99f382b6bb3b649b657b

See more details on using hashes here.

File details

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

File metadata

  • Download URL: danai-0.3.3-py3-none-any.whl
  • Upload date:
  • Size: 17.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for danai-0.3.3-py3-none-any.whl
Algorithm Hash digest
SHA256 2967b4570a1ebf4b5337eadf571f8f11db41a9a07b1108e238839f4e1d17288a
MD5 99ba00b2f2551a153af4dec5e8981d0e
BLAKE2b-256 6263890b95bd7ab00202c02d8b640f3809ed98ac7a3d0b9ff95e5bf89b84a939

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