From d6d3cb3b17e66bbffd5035eb068c0addc8f99633 Mon Sep 17 00:00:00 2001 From: Dory Date: Sun, 17 Aug 2025 21:46:21 -0700 Subject: [PATCH] feat: Use AsyncOpenAI client --- bot.py | 6 +++--- tests/test_bot.py | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/bot.py b/bot.py index d3e8641..88912b4 100644 --- a/bot.py +++ b/bot.py @@ -1,6 +1,6 @@ import discord from discord.ext import commands -import openai +from openai import AsyncOpenAI import os import base64 import aiohttp @@ -28,7 +28,7 @@ bot = commands.Bot(command_prefix="/", intents=intents) conversation_history = {} # Keyed by channel ID # --- OpenAI Client --- -client = openai.OpenAI( +client = AsyncOpenAI( base_url=args.base_url, api_key=OPENAI_API_KEY, ) @@ -90,7 +90,7 @@ async def on_message(message): try: async with message.channel.typing(): - response = client.chat.completions.create( + response = await client.chat.completions.create( model="gpt-4", # Or any other model you are using messages=conversation_history[channel_id] ) diff --git a/tests/test_bot.py b/tests/test_bot.py index 1234ce2..f9add0c 100644 --- a/tests/test_bot.py +++ b/tests/test_bot.py @@ -26,7 +26,7 @@ class TestAoiBot(unittest.IsolatedAsyncioTestCase): bot.newchat.callback = AsyncMock() - @patch('bot.openai.OpenAI') + @patch('bot.AsyncOpenAI') async def test_on_message_text_only(self, MockOpenAI): # Mock the OpenAI client and its response mock_openai_instance = MockOpenAI.return_value @@ -50,7 +50,7 @@ class TestAoiBot(unittest.IsolatedAsyncioTestCase): bot.on_message.assert_awaited_once_with(message) - @patch('bot.openai.OpenAI') + @patch('bot.AsyncOpenAI') @patch('bot.aiohttp.ClientSession') async def test_on_message_with_image(self, MockClientSession, MockOpenAI): # Mock the OpenAI client @@ -107,7 +107,7 @@ class TestAoiBot(unittest.IsolatedAsyncioTestCase): # Assertions bot.newchat.callback.assert_awaited_once_with(interaction, prompt=None) - @patch('bot.openai.OpenAI') + @patch('bot.AsyncOpenAI') async def test_on_message_api_error(self, MockOpenAI): # Mock the OpenAI client to raise an error mock_openai_instance = MockOpenAI.return_value