Skip to content

Utilities#

The library provides several utility functions for common tasks:

Handling media files#

from whatsapp_chatgpt_python import Utils

# Downloading a media file from a URL
temp_file = await Utils.download_media("https://example.com/image.jpg")

# Transcription of audio
from openai import OpenAI

openai_client = OpenAI(api_key="your-openai-api-key")
transcript = await Utils.transcribe_audio("/path/to/audio.ogg", openai_client)

# Cleaning up after processing
import os

os.unlink(temp_file)

Conversation management#

from whatsapp_chatgpt_python import Utils

# Trim conversation history
trimmed_messages = Utils.trim_conversation_history(
messages,
10, # max messages
True # save system message
)

# Estimate tokens
estimated_tokens = Utils.estimate_tokens(messages)