Skip to content

Commit b97ece9

Browse files
committed
feat: Migrate chat completion and tool calling from Cerebras to Groq API, add asyncio event loop compatibility, and update tool schema.
1 parent ff9de5e commit b97ece9

2 files changed

Lines changed: 24 additions & 17 deletions

File tree

LocalBot.py

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,25 @@ class ConversationBufferWindowMemory:
3333
def __init__(self, return_messages: bool = True, k: int = 5):
3434
self.chat_memory = _ChatMemory(k=k)
3535

36-
from cerebras.cloud.sdk import Cerebras
36+
from groq import Groq
3737
from dotenv import load_dotenv
3838

3939
load_dotenv()
4040
TOKEN = os.getenv("TOKEN")
4141
WOLF = os.getenv("WOLF")
4242
WEATHER_API_KEY = os.getenv("WEATHER_API_KEY")
43-
CEREBRAS_API_KEY = os.getenv("CEREBRAS_API_KEY")
43+
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
4444

4545
intents = discord.Intents.default()
4646
intents.message_content = True
47+
48+
# Python 3.14 removed implicit event loop creation in asyncio.get_event_loop().
49+
# py-cord 2.6.1 still relies on it during Bot.__init__, so we create one explicitly.
50+
try:
51+
asyncio.get_running_loop()
52+
except RuntimeError:
53+
asyncio.set_event_loop(asyncio.new_event_loop())
54+
4755
bot = commands.Bot(command_prefix="$", intents=intents)
4856

4957
conversation_memory = {}
@@ -310,7 +318,7 @@ def __init__(self, return_messages: bool = True, k: int = 5):
310318
"description": "The user mention or ID (optional).",
311319
}
312320
},
313-
"required": [],
321+
"required": ["user"],
314322
"additionalProperties": False,
315323
},
316324
"strict": True,
@@ -344,7 +352,7 @@ def __init__(self, return_messages: bool = True, k: int = 5):
344352
},
345353
]
346354

347-
cerebras_client = Cerebras(api_key=CEREBRAS_API_KEY) if CEREBRAS_API_KEY else None
355+
groq_client = Groq(api_key=GROQ_API_KEY) if GROQ_API_KEY else None
348356

349357

350358
async def send_response(ctx, message):
@@ -442,7 +450,7 @@ async def generate_chat_completion(
442450
prompt: str,
443451
is_tool_followup: bool = False,
444452
) -> Optional[str]:
445-
"""Generate a chat completion response using Cerebras with native tool calling."""
453+
"""Generate a chat completion response using Groq with native tool calling."""
446454
try:
447455
global conversation_memory
448456
context_key = server_id if server_id else f"DM-{channel_id}-{user_id}"
@@ -454,14 +462,14 @@ async def generate_chat_completion(
454462

455463
memory = conversation_memory[context_key]
456464

457-
if not cerebras_client:
465+
if not groq_client:
458466
await send_response(
459467
ctx,
460-
"Cerebras client is not configured. Please check your CEREBRAS_API_KEY.",
468+
"Groq client is not configured. Please check your GROQ_API_KEY.",
461469
)
462470
return None
463471

464-
# Build consistent messages format for Cerebras
472+
# Build consistent messages format for Groq
465473
messages = [{"role": "system", "content": system_prompt}]
466474
chat_history = memory.chat_memory.messages
467475
for msg in chat_history:
@@ -472,7 +480,7 @@ async def generate_chat_completion(
472480
# Add the current prompt
473481
messages.append({"role": "user", "content": prompt})
474482

475-
models_to_try = ["gpt-oss-120b", "llama-3.3-70b"]
483+
models_to_try = ["openai/gpt-oss-120b", "openai/gpt-oss-20b"]
476484
response_text = None
477485
last_error = None
478486

@@ -484,7 +492,7 @@ async def generate_chat_completion(
484492
max_depth = 5
485493

486494
while tool_use_depth < max_depth:
487-
response = cerebras_client.chat.completions.create(
495+
response = groq_client.chat.completions.create(
488496
messages=curr_messages,
489497
model=model_name,
490498
tools=TOOLS,
@@ -521,15 +529,15 @@ async def generate_chat_completion(
521529
continue
522530

523531
if not response_text:
524-
raise last_error or Exception("Failed to get response from Cerebras models")
532+
raise last_error or Exception("Failed to get response from Groq models")
525533

526534
memory.chat_memory.add_user_message(prompt)
527535
memory.chat_memory.add_ai_message(response_text)
528536

529537
return response_text
530538

531539
except Exception as e:
532-
print(f"Cerebras completion error: {e}")
540+
print(f"Groq completion error: {e}")
533541
await send_response(ctx, "I encountered an error processing your request.")
534542
return None
535543

@@ -539,7 +547,7 @@ async def handle_tool_call(
539547
tool_call,
540548
send_directly: bool = False,
541549
) -> str:
542-
"""Handle native tool calls from Cerebras."""
550+
"""Handle native tool calls from Groq."""
543551
try:
544552
tool_name = tool_call.function.name
545553
tool_arguments = json.loads(tool_call.function.arguments)
@@ -602,7 +610,6 @@ async def handle_tool_call(
602610
statuses = [
603611
"Ask me anything! 💭",
604612
"Weather forecasts 🌤️",
605-
"Powered by Cerebras AI 🧠",
606613
"Number guessing 🎲",
607614
"Rolling dice 🎯",
608615
"Flipping coins 🪙",

requirements.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Discord Bot Framework
2-
py-cord>=2.6.1
2+
py-cord==2.6.1
33
audioop-lts # Required for py-cord on Python 3.13+
44

55
# Environment & Configuration
@@ -8,5 +8,5 @@ python-dotenv
88
# HTTP Client
99
aiohttp
1010

11-
# AI/ML - Cerebras
12-
cerebras_cloud_sdk
11+
# AI/ML - Groq
12+
groq

0 commit comments

Comments
 (0)