Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Async calls using new SDK by OpenAI #9

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions mock_test.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import asyncio

from openai_multi_client import OpenAIMultiClient, Payload, OpenAIMultiOrderedClient


Expand All @@ -12,7 +11,7 @@ async def mock(payload: Payload):
rand_fail = random.random()
if rand_fail < 0.3:
raise Exception("Mocked exception")
return {"response": f"mocked {payload.metadata['id']}"}
return {"response": f"mocked {payload.metadata.get('id')}"}

if ordered:
api = OpenAIMultiOrderedClient(custom_api=mock, max_retries=3, retry_multiplier=2)
Expand All @@ -35,9 +34,10 @@ def put_data():
i += 1
if response.failed:
failed += 1
print(f"Failed {response.metadata['id']}: {i}/100")
print(f"Failed {response.metadata.get('id')}: {i}/100")
else:
print(f"Got response {response.metadata['id']}: {i}/100")
print(f"Got response {response.metadata.get('id')}: {i}/100")


print('*' * 20)
print(f"Total failed: {failed}/100")
Expand Down
16 changes: 9 additions & 7 deletions openai_multi_client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@

from aioprocessing import AioJoinableQueue, AioQueue
from tenacity import wait_random_exponential, stop_after_attempt, AsyncRetrying, RetryError
import openai
from openai import AsyncOpenAI

aclient = AsyncOpenAI()

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -76,17 +78,17 @@ async def _process_payload(self, payload: Payload) -> Payload:
if self._mock_api:
payload.response = await self._mock_api(payload)
elif payload.endpoint == "completions":
payload.response = await openai.Completion.acreate(**payload.data)
payload.response = await aclient.completions.create(**payload.data)
elif payload.endpoint == "chat.completions" or payload.endpoint == "chats":
payload.response = await openai.ChatCompletion.acreate(**payload.data)
payload.response = await aclient.chat.completions.create(**payload.data)
elif payload.endpoint == "embeddings":
payload.response = await openai.Embedding.acreate(**payload.data)
payload.response = await aclient.embeddings.create(**payload.data)
elif payload.endpoint == "edits":
payload.response = await openai.Edit.acreate(**payload.data)
payload.response = await aclient.edits.create(**payload.data)
elif payload.endpoint == "images":
payload.response = await openai.Image.acreate(**payload.data)
payload.response = await aclient.images.generate(**payload.data)
elif payload.endpoint == "fine-tunes":
payload.response = await openai.FineTune.acreate(**payload.data)
payload.response = await aclient.fine_tunes.create(**payload.data)
else:
raise ValueError(f"Unknown endpoint {payload.endpoint}")
logger.debug(f"Processed {payload}")
Expand Down
2 changes: 1 addition & 1 deletion real_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def on_success(result: Payload):
print(f"Failed {pid}")
else:
print(f"Got response for {pid}:",
result.response['choices'][0]['message']['content'])
result.response.choices[0].message.content)


def test_callback():
Expand Down