Skip to content

Commit

Permalink
Merge pull request #8 from TeamKillerX/main
Browse files Browse the repository at this point in the history
_
  • Loading branch information
xtsea authored Oct 23, 2024
2 parents 8279ec7 + eaf0816 commit cc40d0a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
2 changes: 1 addition & 1 deletion akenoai/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "1.2.3"
__version__ = "1.2.4"
21 changes: 15 additions & 6 deletions akenoai/openai.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import base64
import logging
import time

import httpx
import aiohttp

logging.basicConfig(level=logging.INFO)
LOGS = logging.getLogger(__name__)


class OpenAI:
api_key = ""

Expand All @@ -23,11 +25,12 @@ async def send_log(text_log: str):
params = {
"text_log": text_log
}
async with httpx.AsyncClient() as client:
response = await client.post(url, params=params)
if response.status_code != 200:
return None
return response.json()
async with aiohttp.ClientSession() as session:
async with session.post(url, params=params) as response:
if response.status != 200:
return None
data = await response.json()
return data["message"]

@classmethod
async def run_image(
Expand All @@ -40,6 +43,7 @@ async def run_image(
):
cls.set_api_key(key)
try:
start_time = time.perf_counter()
client = openai_meta(api_key=cls.api_key)
if run_async:
response = await client.images.generate(
Expand All @@ -55,6 +59,8 @@ async def run_image(
if res is None:
LOGS.warning("Warning: no response API")
LOGS.info(res)
end_time = time.perf_counter()
LOGS.info(f"AIOHTTP: {end_time - start_time:.2f} seconds")
return response.data[0].url if response and response.data else ""
except Exception as e:
return f"Error response: {e}"
Expand All @@ -71,6 +77,7 @@ async def run(
):
cls.set_api_key(key)
try:
start_time = time.perf_counter()
client = openai_meta(api_key=cls.api_key)
if async_is_stream:
answer = ""
Expand All @@ -96,6 +103,8 @@ async def run(
if res is None:
LOGS.warning("Warning: no response API")
LOGS.info(res)
end_time = time.perf_counter()
LOGS.info(f"AIOHTTP: {end_time - start_time:.2f} seconds")
return response.choices[0].message.content or ""
except Exception as e:
return f"Error response: {e}"

0 comments on commit cc40d0a

Please sign in to comment.