Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
itayzit committed Dec 4, 2022
1 parent e686571 commit c57814f
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 1 deletion.
53 changes: 52 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,53 @@
# openai-async
An asynchronous client for openai services - completion, image generation and embedding

An asynchronous client for openai services - completion, image generation and embedding.<br><br>
For the full documentation, go to the [openAI website](https://beta.openai.com/docs/api-reference).


# Installation

`pip install openai-async`

# Use

## Text completion
import openai_async

response = await openai_async.complete(
"<API KEY>",
timeout=1,
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."

## Image generation
import openai_async
import urllib.request
from PIL import Image

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

![A white siamese cat](https://oaidalleapiprodscus.blob.core.windows.net/private/org-1yFGEVR2Z2q0cjpxYXoEQ9mE/user-I8zlH1LhK7LToUCDLQObNQNk/img-63b5z1tNyTg9YFjYvAIGE8Sp.png?st=2022-12-03T22%3A14%3A44Z&se=2022-12-04T00%3A14%3A44Z&sp=r&sv=2021-08-06&sr=b&rscd=inline&rsct=image/png&skoid=6aaadede-4fb3-4698-a8f6-684d7786b067&sktid=a48cca56-e6da-484e-a814-9c849652bcb3&skt=2022-12-03T12%3A00%3A31Z&ske=2022-12-04T12%3A00%3A31Z&sks=b&skv=2021-08-06&sig=m%2BcY1Yo8jY9bJwXccZrbW7k8K7tOBPNT6VMJViiq5oE%3D)


# Getting an API key
To generate an openAI API key, while the openAI website, click your username on the right corner, then go to "View API keys" and create a key.
<br><br>
### Disclaimer

This repository has no connection whatsoever to the official openAI.
1 change: 1 addition & 0 deletions openai_async/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from openai_async.openai_async import complete, generate_img
24 changes: 24 additions & 0 deletions openai_async/openai_async.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import httpx


def _send_to_openai(endpoint_url: str,):
async def send_to_openai(api_key: str, timeout: float, payload: dict) -> httpx.Response:
"""
Send a request to openai.
:param api_key: your api key
:param timeout: timeout in seconds
:param payload: the request body, as detailed here: https://beta.openai.com/docs/api-reference
"""
async with httpx.AsyncClient() as client:
return await client.post(
url=endpoint_url,
json=payload,
headers={"content_type": "application/json", "Authorization": f"Bearer {api_key}"},
timeout=timeout,
)

return send_to_openai


complete = _send_to_openai("https://api.openai.com/v1/completions")
generate_img = _send_to_openai("https://api.openai.com/v1/images/generations")

0 comments on commit c57814f

Please sign in to comment.