forked from itayzit/openai-async
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
77 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from openai_async.openai_async import complete, generate_img |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |