Skip to main content

A light-weight, asynchronous client for OpenAI API - text completion, image generation and embeddings.

Project description

openai-async

A light-weight, asynchronous client for OpenAI API - chat completion, text completion, image generation and embeddings.

For the full documentation, go to the openAI website.

Installation

pip install openai-async

Use

Chat completion

response = await openai_async.chat_complete(
    _OPEN_AI_API_KEY,
    timeout=2,
    payload={
        "model": "gpt-3.5-turbo",
        "messages": [{"role": "user", "content": "Hello!"}],
    },
)
print(response.json()["choices"][0]["message"])
>>> {"role": "assistant", "content": "\n\nHello there! How can I assist you today?"}

Visit the official documentation for more info.

Text completion

import openai_async

response = await openai_async.complete(
    "<API KEY>",
    timeout=2,
    payload={
        "model": "text-davinci-003",
        "prompt": "Correct this sentence: Me like you.",
        "temperature": 0.7,
    },
)
print(response.json()["choices"][0]["text"].strip())
>>> "I like you."

Visit the official documentation for more info.

Image generation

import openai_async
import urllib.request
from PIL import Image

response = await openai_async.generate_img(
    "<API KEY>",
    timeout=8,
    payload={
        "prompt": "a white siamese cat",
        "n": 1,
        "size": "512x512"
    },
)
urllib.request.urlretrieve(response.json()["data"][0]["url"], "img.png")
Image.open("img.png").show()
a cat

Embeddings

import openai_async

response = await openai_async.embeddings(
    <API_KEY>,
    timeout=2,
    payload={"model": "text-embedding-ada-002", "input": "a cat"},
)
print(response.json()["data"][0]["embedding"])
>>> [-0.019408401,
-0.009246278,
-0.014390069,
-0.012294915,
-0.0025609178,
0.021252638,
...]

Visit the official documentation for more info.

Get an API key

To generate an openAI API key, while in the openAI website, click on your username in the top right corner, then go to "View API keys" and create a key.

Disclaimer

This repository has no connection whatsoever to openAI.

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-async-0.0.3.tar.gz (4.0 kB view details)

Uploaded Source

Built Distribution

openai_async-0.0.3-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

Details for the file openai-async-0.0.3.tar.gz.

File metadata

  • Download URL: openai-async-0.0.3.tar.gz
  • Upload date:
  • Size: 4.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.1

File hashes

Hashes for openai-async-0.0.3.tar.gz
Algorithm Hash digest
SHA256 93434f620df0eb9abd27387eb1e2641c0075f2be77318b5ecb2610bd0354d81f
MD5 8aa1fd357a8ef4117ab36fdacc9ca61e
BLAKE2b-256 d4590b05d388c8e57202484a7122e1abf892eeeadc1b53a0b1bddcc7d2e127cb

See more details on using hashes here.

File details

Details for the file openai_async-0.0.3-py3-none-any.whl.

File metadata

File hashes

Hashes for openai_async-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 982ac3e5598344326bc42d2245618779a01db0d092b2c2ef57bee9fb7418f74a
MD5 3c497681dae6a48d982503fdd293a14a
BLAKE2b-256 d4b7753cb5a588e7e9419fdebd8d7b7eb1b2141c6ed6c995fbcc9971f764c6f8

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