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.2.tar.gz (5.0 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.2-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pypeprompts-0.0.2.tar.gz
  • Upload date:
  • Size: 5.0 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.2.tar.gz
Algorithm Hash digest
SHA256 58fe9709893916b2c8561074c92728b48d94760183c839df600fb98764bd81d2
MD5 9a2cb5bb417a13d5ce6d69b3a39affdc
BLAKE2b-256 24814c32a0ef901a3050500571b97b58fd19437f244f7b19f151c354fb11273e

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pypeprompts-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 5.8 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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 d1914a66f5f028b82a9b74f0e55adbd723fd4e5cb92cab5de83daffdeb88def2
MD5 2a19f14a796cdbff2e7600ab67f72852
BLAKE2b-256 37f1c7523167de70a2b525e3b8846bc1dd792144b7fc8bb07e91c1ff16bf9727

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