AI & OpenRouter Integration
Overview
The BBX platform integrates with OpenRouter to provide AI-driven insights, match analysis, and automated tournament management. By utilizing the OpenRouter API, the platform can leverage a variety of Large Language Models (LLMs) like GPT-4, Claude, and Llama 3 to assist organizers in interpreting complex Swiss standings or generating commentary for Top Cut brackets.
This integration is built on top of the standard OpenAI SDK, ensuring a familiar developer experience while providing access to the diverse model ecosystem offered by OpenRouter.
Configuration
To enable AI features, you must configure your environment variables. The integration specifically looks for the following keys in your .env file:
| Variable | Description |
| :--- | :--- |
| OPENAI_API_KEY | Your OpenRouter API Key. |
| HTTP-Referer | (Optional) Used by OpenRouter for rankings. Default is http://localhost:3000. |
| X-Title | (Optional) The name identifying your project in the OpenRouter dashboard. Default is BBX-PROJ. |
The OpenRouter Client
The core integration resides in lib/openrouter.js. This module exports a pre-configured instance of the OpenAI client directed to the OpenRouter base URL.
Interface Reference
import { openrouter } from "@/lib/openrouter";
// Usage follows the standard OpenAI SDK pattern
const response = await openrouter.chat.completions.create({
model: "openai/gpt-3.5-turbo", // Or any OpenRouter supported model
messages: [{ role: "user", content: "Analyze these tournament standings..." }],
});
Implementation Usage
The AI integration is designed to work alongside the existing tournament data structures (Participants, Matches, and Standings).
Analyzing Tournament Data
The AI can be used to process the results found in the swiss_standings view or match results from the top_cut stage.
Example: Generating a Tournament Summary
import { openrouter } from "@/lib/openrouter";
async function getTournamentInsights(standings) {
const prompt = `Based on the following Swiss standings, identify the top contenders and potential dark horses: ${JSON.stringify(standings)}`;
const completion = await openrouter.chat.completions.create({
model: "google/gemini-pro-1.5",
messages: [
{
role: "system",
content: "You are an expert Beyblade X tournament analyst."
},
{
role: "user",
content: prompt
}
],
});
return completion.choices[0].message.content;
}
Key Integration Points
- Bracket Prediction: Utilizing match history from the
matchestable to predict Top Cut outcomes. - Seeding Assistance: Analyzing participant performance to suggest optimal seeding for bracket advancement.
- Rulebook Querying: Providing instant clarifications on tournament rules for administrators within the Admin Console.
Best Practices
- Model Selection: Use cheaper models (e.g.,
mistralai/mistral-7b-instruct) for simple data formatting and flagship models (e.g.,anthropic/claude-3-opus) for complex strategic analysis. - Rate Limiting: Since tournament data can be large, ensure you implement proper error handling for OpenRouter's rate limits, especially during live events with high concurrent traffic.
- Privacy: Avoid sending sensitive player data (like email addresses) to the AI; use the truncated UUIDs or display names provided in the
participantstable.