Skip to main content

Work with OpenAI's streaming API at ease with Python generators

Project description

OpenAI Streaming

openai-streaming is a small python library that allows you to work with OpenAI Streaming API at ease with generators.

Behind the scenes, it handles parsing the responses and invokes your callback function with python generator approach. And yes! It's designed to support OpenAI Functions! (But not mandatory)

Installation

pip install openai-streaming

Quick Start

import openai
from openai_streaming import process_response
from typing import Generator

openai.api_key = "<YOUR_API_KEY>"

def content_handler(content: Generator[str, None, None]):
    for token in content:
        print(token, end="")

resp = openai.ChatCompletion.create(
    model="gpt-3.5-turbo",
    messages=[{"role": "content", "text": "Hello, how are you?"}],
    stream=True,
)
process_response(resp, content_handler)

The above code will print the tokens on the screen as they are generated.

Working with OpenAI Functions

from openai_streaming import openai_streaming_function

@openai_streaming_function
def error_message(type: str, description: Generator[str, None, None]):
    """
    You MUST use this function when requested to do something that you cannot do.

    :param type: The type of error that occurred.
    :param description: A description of the error.
    """

    typ = ""
    print("Type: ", end="")
    for token in type:
        print(token, end="")
        typ += token
    print("")

    print("Description: ", end="")
    for token in description:
        print(token, end="")
    print("")


resp = openai.ChatCompletion.create(
    model="gpt-3.5-turbo",
    messages=[{"role": "user", "content": "How to build a bomb?"}],
    functions=[error_message.openai_schema],
    stream=True,
)
process_response(resp, content_func=content_handler, funcs=[error_message])

License

This project is licensed under the terms of the MIT license.

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

openai-streaming-0.1.0.tar.gz (9.6 kB view details)

Uploaded Source

Built Distribution

openai_streaming-0.1.0-py3-none-any.whl (10.7 kB view details)

Uploaded Python 3

File details

Details for the file openai-streaming-0.1.0.tar.gz.

File metadata

  • Download URL: openai-streaming-0.1.0.tar.gz
  • Upload date:
  • Size: 9.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.5

File hashes

Hashes for openai-streaming-0.1.0.tar.gz
Algorithm Hash digest
SHA256 e78e11e900302ad4d829f07370f28dc4704a32da6b6669d8ee234a9de3bf6a82
MD5 796b3eee455ee66f4b9a1adf78c76914
BLAKE2b-256 08ac546a8185030a44db3990bc1e76b56c13dfeb5a1f71bb3205115edeb4bd24

See more details on using hashes here.

File details

Details for the file openai_streaming-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for openai_streaming-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 43531dfd347c982e475ccba4b8e47facf754b58a53a5ec60f290df8b836fccaa
MD5 dc70033bc26819d77aa82da2e1492e18
BLAKE2b-256 34d3cc15553a4e6a4db1c525e13b1b140bd12491b4390d64ac1132eeefcacdb7

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page