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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
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
Algorithm | Hash digest | |
---|---|---|
SHA256 | e78e11e900302ad4d829f07370f28dc4704a32da6b6669d8ee234a9de3bf6a82 |
|
MD5 | 796b3eee455ee66f4b9a1adf78c76914 |
|
BLAKE2b-256 | 08ac546a8185030a44db3990bc1e76b56c13dfeb5a1f71bb3205115edeb4bd24 |
File details
Details for the file openai_streaming-0.1.0-py3-none-any.whl
.
File metadata
- Download URL: openai_streaming-0.1.0-py3-none-any.whl
- Upload date:
- Size: 10.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/4.0.2 CPython/3.11.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 43531dfd347c982e475ccba4b8e47facf754b58a53a5ec60f290df8b836fccaa |
|
MD5 | dc70033bc26819d77aa82da2e1492e18 |
|
BLAKE2b-256 | 34d3cc15553a4e6a4db1c525e13b1b140bd12491b4390d64ac1132eeefcacdb7 |