Skip to main content

A simple tracker for OpenAI API calls for Pype App

Project description

OpenAI Tracker

Python Poetry License: MIT PyPI version GitHub issues GitHub stars

A simple tracker for OpenAI API calls with built-in dashboard integration and unique instance tracking.

Features

  • Easy integration with OpenAI API
  • Built-in dashboard for analytics
  • Unique instance tracking
  • Support for multiple trackers in a single application

Installation

You can install the OpenAI Tracker using pip:

pip install pypeprompts

Or if you're using Poetry:

poetry add pypeprompts

Usage

Basic Usage

For simple use cases where you want all tracked calls to share the same instance ID:

from openai import OpenAI
from pypeprompts import PromptAnalyticsTracker

client = OpenAI(api_key="your-api-key")

# Initialize the PromptAnalyticsTracker
tracker = PromptAnalyticsTracker(
    name="OpenAI API Tracker",
    api_key=analytics_api_key,
)

@tracker.track_prompt
def generate_text(prompt: str) -> str:
    try:
        response = client.chat.completions.create(
            model="gpt-4",
            messages=[{"role": "user", "content": prompt}],
        )
        return response.choices[0].message.content
    except Exception as e:
        print(f"Error calling OpenAI API: {e}")
        return

# Use the function
response = generate_text("Tell me a poem")
print(response)

Advanced Usage

For cases where you want to track different parts of your application separately:

from openai import OpenAI
from pypeprompts import PromptAnalyticsTracker

client = OpenAI(api_key="your-api-key")

# Create separate trackers for different parts of your application
user_tracker = PromptAnalyticsTracker(
    name="User prompts tracker",
    api_key=analytics_api_key,
)
admin_tracker = PromptAnalyticsTracker(
    name="Admin prompts tracker",
    api_key=analytics_api_key,
)

@user_tracker.track_prompt
def user_generate_response(prompt):
    completion = client.chat.completions.create(
        model="gpt-3.5-turbo",
        messages=[{"role": "user", "content": prompt}]
    )
    return completion.choices[0].message.content

@admin_tracker.track_prompt
def admin_generate_response(prompt):
    completion = client.chat.completions.create(
        model="gpt-4",
        messages=[{"role": "system", "content": prompt}]
    )
    return completion.choices[0].message.content

# Use the functions
user_response = user_generate_response("Tell me a joke")
admin_response = admin_generate_response("Explain quantum computing")

Configuration

Detailed configuration options and environment variables...

Dashboard

Information about accessing and using the built-in dashboard...

Contributing

We welcome contributions to OpenAI Tracker! Please see our Contributing Guide for more details.

Testing

To run the tests, use the following command:

poetry run pytest

Changelog

See the CHANGELOG.md file for details on what has changed.

License

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

Acknowledgments

  • OpenAI for their excellent API
  • Contributors who have helped to improve this project

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

pypeprompts-0.0.3.tar.gz (4.8 kB view details)

Uploaded Source

Built Distribution

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

pypeprompts-0.0.3-py3-none-any.whl (5.5 kB view details)

Uploaded Python 3

File details

Details for the file pypeprompts-0.0.3.tar.gz.

File metadata

  • Download URL: pypeprompts-0.0.3.tar.gz
  • Upload date:
  • Size: 4.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.11.5 Darwin/19.6.0

File hashes

Hashes for pypeprompts-0.0.3.tar.gz
Algorithm Hash digest
SHA256 ff1c21b9d67c5df35224bba7aa2baac382b5f3fa5e3106e308482c1fe4c00555
MD5 f281da21a528cca2d31fac76fc01a000
BLAKE2b-256 1f1afb286027dba5c7b2acfd5f07aed91fdee4651477e5d6f8036d96beecf024

See more details on using hashes here.

File details

Details for the file pypeprompts-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: pypeprompts-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 5.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.11.5 Darwin/19.6.0

File hashes

Hashes for pypeprompts-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 b7364475bd4efd99619cd499a50903113a44048d96652902af88a1f04bde1cde
MD5 779f5adb731d2f1008c4e48c53f96c95
BLAKE2b-256 e32152c521505d30a74fcc0877a6920384af514d4b8541224dd694b55ed323c3

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