Giter Club home page Giter Club logo

openai-pygenerator's Introduction

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

openai-pygenerator's People

Contributors

grit-app[bot] avatar phelps-sg avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

gear273

openai-pygenerator's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.