Skip to main content

SDK for tracking and testing OpenAI API calls

Project description

Playgent

PyPI Version Python Versions

Record and test your OpenAI agent sessions.

Installation

pip install playgent

Quick Start

Step 1: Instrument your agent to record sessions

import json
from typing import List
import dotenv
import os

dotenv.load_dotenv()

from playgent.openai import OpenAI  # Drop-in replacement for OpenAI
from playgent import record, session

client = OpenAI()  # Automatically tracks all OpenAI calls

# Your agent functions here
def add_to_todo_list(task: str) -> str:
    # ... your implementation
    pass

@record  # This decorator records the entire session
def infer(input_text: str, model: str = "gpt-4", tools_list: List = None):
    messages = [{"role": "user", "content": input_text}]

    # Your agent logic with OpenAI calls
    response = client.chat.completions.create(
        model=model,
        messages=messages,
        tools=tools_list
    )

    # Process response and handle tool calls...
    return response

# Use session context manager to auto-create sessions
with session() as sess:
    result = infer("Add 'buy milk' to my todo list")

Step 2: Run tests via replay

After annotating sessions with expected outcomes on the Playgent dashboard:

from playgent import replay_test, evaluate
from example_agent import infer

# Test using a recorded session ID
test_case_id = "6996b53a-1ebf-403a-8770-10ed001391c3"

with replay_test(test_case_id) as (session_id, endpoint_events):
    for event in endpoint_events:
        infer(**event.arguments)
    result = evaluate(test_case_id, session_id)

That's it! Two steps:

  1. Instrument - Add @record decorator and use playgent.openai.OpenAI
  2. Test - Replay annotated sessions and evaluate results

Environment Setup

Create a .env file:

PLAYGENT_API_KEY=your-playgent-api-key
OPENAI_API_KEY=your-openai-api-key

Full Examples

See example_agent.py and example_agent_test.py for complete working examples.

License

MIT

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

playgent-0.1.1.tar.gz (17.6 kB view details)

Uploaded Source

Built Distribution

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

playgent-0.1.1-py3-none-any.whl (15.8 kB view details)

Uploaded Python 3

File details

Details for the file playgent-0.1.1.tar.gz.

File metadata

  • Download URL: playgent-0.1.1.tar.gz
  • Upload date:
  • Size: 17.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for playgent-0.1.1.tar.gz
Algorithm Hash digest
SHA256 86b7a7a004e52c23febcb5a3e70ec91eae1b8dacca80f963126d5a18ffedbf8f
MD5 9a76b965d949b763b91247118303ba18
BLAKE2b-256 e29bc5956cec79a469094e2960f3171823f70b217963cb9f7881acead7754182

See more details on using hashes here.

File details

Details for the file playgent-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: playgent-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 15.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for playgent-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 1f73b0fe832c940958cdfa58a9dd38647318d162dbb6379ee316ea6cad565c48
MD5 e443108482276aa4dee8132306c466f0
BLAKE2b-256 3677e00b41a6d21e4713cb27c67d39adf2c522a1db7e6dac5f0ebf2cf3acc05e

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