GithubHelp home page GithubHelp logo

kitsunedfox / async-openai Goto Github PK

View Code? Open in Web Editor NEW

This project forked from growthengineai/async-openai

1.0 0.0 0.0 219 KB

Unofficial Async Python client library for the OpenAI API

License: MIT License

Python 100.00%

async-openai's Introduction

async-openai

Unofficial Async Python client library for the OpenAI API based on Documented Specs

Latest Version: PyPI version

Official Client

Features

  • Asyncio based with Sync and Async Support with httpx

  • Supports all API endpoints

  • Strongly typed validation of requests and responses with Pydantic Models with transparent access to the raw response and object-based results.

  • Handles Retries automatically through backoff

  • Supports Local and Remote Cloud Object Storage File Handling Asyncronously through file-io

    • Supports S3: s3://bucket/path/to/file.txt

    • Supports GCS: gs://bucket/path/to/file.txt

    • Supports Minio: minio://bucket/path/to/file.txt

  • Supports limited cost tracking for Completions and Edits requests (when stream is not enabled)


Installation

# Install from stable
pip install async-openai

# Install from dev/latest
pip install git+https://github.com/GrowthEngineAI/async-openai.git

Quick Usage

import asyncio
from async_openai import OpenAI, settings, CompletionResponse

# Environment variables should pick up the defaults
# however, you can also set them explicitly.

# `api_key` - Your OpenAI API key.                  Env: [`OPENAI_API_KEY`]
# `url` - The URL of the OpenAI API.                Env: [`OPENAI_URL`]
# `api_type` - The OpenAI API type.                 Env: [`OPENAI_API_TYPE`]
# `api_version` - The OpenAI API version.           Env: [`OPENAI_API_VERSION`]
# `organization` - The OpenAI organization.         Env: [`OPENAI_ORGANIZATION`]
# `proxies` - A dictionary of proxies to be used.   Env: [`OPENAI_PROXIES`]
# `timeout` - The timeout in seconds to be used.    Env: [`OPENAI_TIMEOUT`]
# `max_retries` - The number of retries to be used. Env: [`OPENAI_MAX_RETRIES`]

OpenAI.configure(
    api_key = "sk-XXXX",
    organization = "org-XXXX",
    debug_enabled = False,
)

# Alternatively you can configure the settings through environment variables
# settings.configure(
#    api_key = "sk-XXXX",
#     organization = "org-XXXX",
# )


# [Sync] create a completion
# Results return a CompletionResult object
result: CompletionResponse = OpenAI.completions.create(
    prompt = 'say this is a test',
    max_tokens = 4,
    stream = True
)

# print the completion text
# which are concatenated together from the result['choices'][n]['text']

print(result.text)

# print the number of choices returned
print(len(result))

# get the cost consumption for the request
print(result.consumption)

# [Async] create a completion
# All async methods are generally prefixed with `async_`

result: CompletionResponse = asyncio.run(
    OpenAI.completions.async_create(
        prompt = 'say this is a test',
        max_tokens = 4,
        stream = True
    )
)

Dependencies

The aim of this library is to be as lightweight as possible. It is built on top of the following libraries:

  • aiohttpx: Unified Async / Sync HTTP Client that wraps around httpx

    • httpx: Async / Sync HTTP Requests

    • lazyops: Provides numerous utility functions for working with Async / Sync code and data structures

  • pydantic: Type Support

  • file-io: Async Cloud-based File Storage I/O

  • backoff: Retries with Exponential Backoff

async-openai's People

Contributors

trisongz avatar

Stargazers

 avatar

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.