Skip to main content

openai-pygenerator

GitHub Workflow Status GitHub release (latest by date) GitHub

Import Note

The openai Python API versions 1.0.0 and later now incorporate retry functionality and type annotations. This package has been migrated to the new API, but should only be used as a temporary measure to ensure backward compatibility. If you are using this package in production, consider rewriting your code so that it uses the new openai API directly.

Overview

This is a simple type-annotated wrapper around the OpenAI Python API which:

  • provides configurable retry functionality,
  • reduces the default timeout from 10 minutes to 20 seconds (configurable),
  • provides a simple class to manage chat session state, and
  • provides a generator over completions.

It can also be used to chain together completions from different prompts in a very straightforward functional-programming style using Python generators.

Installation

pip install openai-pygenerator

Basic usage

In the example below we will retry automatically if there is a RateLimitError.

from openai_pygenerator import ChatSession
 
session = ChatSession()
solution = session.ask("What is the square root of 256?")
print(solution)
working = session.ask("Show your working")
print(working)
print("Transcript:")
print(session.transcript)

Completion pipelines and overriding parameters

from typing import Iterable

from openai_pygenerator import (
    ChatSession,
    Completions,
    completer,
    content,
    next_completion,
    user_message,
)

high_temp_completions = completer(temperature=0.8)


def heading(message: str, margin: int = 80) -> None:
    print()
    print("-" * margin)
    print(message)
    print("-" * margin)
    print()


def example_square_root(session: ChatSession) -> None:
    solution = session.ask("What is the square root of 256?")
    print(solution)
    working = session.ask("Show your working")
    print(working)

    heading("Session transcript:")
    print(session.transcript)


def creative_answer(prompt: str, num_completions: int = 1) -> Completions:
    return high_temp_completions([user_message(prompt)], n=num_completions)


def pick_color(num_completions: int) -> Completions:
    return creative_answer(
        "Pick a color at random and then just tell me your choice, e.g. 'red'",
        num_completions,
    )


def generate_sentence(color_completions: Completions) -> Iterable[str]:
    for color_completion in color_completions:
        color = content(color_completion)
        result = next_completion(
            creative_answer(f"Write a sentence about the color {color}.")
        )
        if result is not None:
            yield content(result)


if __name__ == "__main__":
    heading("Find square root - using environment variables for parameters")
    example_square_root(session=ChatSession())

    heading("Find square root - overriding temperature, max_tokens, max_retries")
    example_square_root(
        session=ChatSession(
            generate=completer(temperature=0.5, max_tokens=300, max_retries=5)
        )
    )

    heading("Example completion pipeline")
    for sentence in generate_sentence(pick_color(num_completions=10)):
        print(sentence)

Running

export OPENAI_API_KEY=<key>
python src/openai_pygenerator/example.py

Configuration

To override default parameters use the following shell environment variables:

export GPT_MODEL=gpt-3.5-turbo
export GPT_TEMPERATURE=0.2
export GPT_MAX_TOKENS=500
export GPT_MAX_RETRIES=5
export GPT_REQUEST_TIMEOUT_SECONDS=20
export OPENAI_API_KEY=<key>
python src/openai_pygenerator/example.py

Metadata

Release files for openai-pygenerator 0.6.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for openai-pygenerator 0.6.2
File Size Uploaded
openai_pygenerator-0.6.2.tar.gz 5.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for openai-pygenerator 0.6.2
File Interpreter ABI Platform
openai_pygenerator-0.6.2-py3-none-any.whl Python 3 none any Details

Total release size: 12.7 kB

Release files / openai_pygenerator-0.6.2.tar.gz

Download URL openai_pygenerator-0.6.2.tar.gz
Size 5.0 kB
Tags Source
SHA-256 checksum
How to use checksums
995dc28daad10dc8ff566cb13cbf126b03557f4a60ccdae88e1c0504685c4d61
BLAKE2b-256 checksum
How to use checksums
89c88d42ab3b9932796a6293af9854cc94abebd58937cc4e490a3ad78e7cb1d9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.7.1 CPython/3.11.0 Linux/6.2.0-1015-azure

Release files / openai_pygenerator-0.6.2-py3-none-any.whl

Download URL openai_pygenerator-0.6.2-py3-none-any.whl
Size 7.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
01726f04494cb53d0f6d1aa91e98d89026e991f8de465cfe88d308936120067e
BLAKE2b-256 checksum
How to use checksums
ce57e15830f787d8a611de350a755d37094ae89985380bcd29e71518f7c6042d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.7.1 CPython/3.11.0 Linux/6.2.0-1015-azure

Release history Release notifications | RSS feed

This release

0.6.2 This release

2 release files

0.6.1

2 release files

0.6.0

2 release files

0.5.7

2 release files

0.5.6

2 release files

0.5.5

2 release files

0.5.4

2 release files

0.5.3

2 release files

0.5.2

2 release files

0.5.1

2 release files

0.5.0

2 release files

0.4.9

2 release files

0.4.8

2 release files

0.4.7

2 release files

0.4.6

2 release files

0.4.5

2 release files

0.4.4

2 release files

0.4.3

2 release files

0.4.2

2 release files

0.4.1

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.2.2

2 release files

0.1.2

1 release file

0.1.1

2 release files

0.1.0

2 release files

0.0.12

2 release files

0.0.11

2 release files

0.0.10

2 release files

0.0.9

2 release files

0.0.8

2 release files

0.0.1

2 release files

0.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page