Skip to content

TokenThrift is a Python library enabling efficient, queue-based management and budgeting of API requests, particularly for AI services like OpenAI. Harness the power of AI, cost-effectively!

Notifications You must be signed in to change notification settings

glm3/TokenThrift

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

4 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

TokenThrift Python Library

TokenThrift is a Python library for queue-based management of API requests to AI services like OpenAI. This can help manage and optimize the cost of using pay-per-use AI services by allowing users to specify a budget and manage requests within that budget.

๐Ÿš€ Features

  • ๐Ÿ“จ Queue-based management of API requests.
  • ๐Ÿ’ฒ Cost estimation and management for each request.
  • ๐Ÿ”ง Customizable request execution and processing.

๐Ÿ“ฆ Installation

First clone the repository to your local machine:

git clone https://github.com/glm3/TokenThrift
cd TokenThrift

Then install the required dependencies:

pip install -r requirements.txt

๐ŸŽฏ Basic Usage

Here's a basic example of how to use the TokenThrift library.

from token_thrift.token_thrift import TokenThrift

def print_idea(completed_request):
    print("============================================")
    print("Topic")
    print(f"{completed_request.prompt}")
    print("============================================")
    print("Generated Idea")
    print(f"{completed_request.completion}")
    print("============================================")
    print("Cost")
    print(f"${completed_request.actual_cost}")
    print("============================================")

def main():
    api_key = "your_openai_api_key"
    budget_in_dollars = 10

    thrift = TokenThrift(api_key, budget_in_dollars)

    topics = [
        "AI in healthcare",
        "Remote work",
        "Electric cars",
        "Dystopian future",
        "Space exploration",
        "Yoga and mental health"
    ]

    for topic in topics:
        prompt = f"Generate an idea about {topic}."
        thrift.enqueue_request(prompt)

    thrift.process_requests_sequentially(print_idea)

    print(thrift.request_statistics)

if __name__ == "__main__":
    main()

In this example, the library is used to generate ideas for different topics within a given budget.

Note: Remember to replace "your_openai_api_key" with your actual OpenAI API key.

๐Ÿค Contributing

We welcome contributions! Please submit PRs for any enhancements or bug fixes.

โš–๏ธ License

This project is licensed under the MIT License.

โœ‰๏ธ Contact If you have any questions, feel free t open an issue.

Please note that this library is still in its early stages, and its API might change in the future. But we're constantly improving it and we welcome feedback and suggestions! Join us in making TokenThrift the go-to Python library for cost-efficient use of AI services! ๐Ÿš€๐ŸŒŸ

About

TokenThrift is a Python library enabling efficient, queue-based management and budgeting of API requests, particularly for AI services like OpenAI. Harness the power of AI, cost-effectively!

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages