OpenRouter AI & LLM Integrations
Relevant source files
The following files were used as context for generating this wiki page:
- src/lib/ai/openrouter.ts
- src/app/[locale]/admin/api/event-creations/ai/route.ts
- src/app/[locale]/admin/integrations/_components/AdminIntegrationsForm.tsx
- src/app/[locale]/admin/api/home-featured-events/context-news/route.ts
- src/app/[locale]/admin/events/calendar/_components/useAdminCreateEventForm.tsx
- tests/unit/openrouter.test.ts
OpenRouter AI and LLM Integrations provide the platform with advanced artificial intelligence capabilities for automating market creation, validating content, and researching real-world news. By leveraging the OpenRouter API, the system can interact with a variety of Large Language Models (LLMs) to ensure that prediction market rules are objective and deterministic, while also providing administrators with tools to generate featured content efficiently.
The integration serves three primary functions: generating structured resolution rules based on Polymarket-style samples, performing strict content validation (language, dates, and logic), and conducting live web searches to find relevant news articles for featured markets.
Architecture and Core Integration
The integration is centered around a library that abstracts the OpenRouter API, allowing for model discovery and chat completions with support for advanced features like web search and referrer tracking.
Core Components
requestOpenRouterCompletion: The primary function for sending messages to LLMs via OpenRouter. It handles headers for site identification (viaX-TitleandHTTP-Referer) and supports web search parameters.fetchOpenRouterModels: A utility to retrieve a list of available models from OpenRouter, specifically filtering for those that support theweb_search_optionsparameter.AdminIntegrationsForm: The UI component where administrators configure the API key and select their preferred model.
Connection Workflow
The system identifies itself to OpenRouter using the site name and URL configured in the project settings. This ensures that the platform receives appropriate attribution and follows OpenRouter's best practices for referrers.
AI-Driven Market Creation
The platform uses AI to assist administrators in creating complex prediction markets. This is primarily handled via the generate_rules mode in the event creation AI route.
Rules Generation Logic
When generating rules, the system fetches real-world samples from the Polymarket Gamma API to use as "few-shot" context for the LLM. This ensures that the generated rules follow a specific, professional, and deterministic style suitable for prediction markets.
| Parameter | Role | Description |
|---|---|---|
mode | Request Mode | Either generate_rules or check_content. |
mainCategorySlug | Context | Used to filter samples from Gamma API. |
marketMode | Logic | Dictates whether rules are for binary or multi-market events. |
temperature | LLM Config | Hardcoded to 0 for rule generation to ensure consistency. |
Content Validation and Safety
The check_content mode acts as a strict validator for human-created or AI-generated content. It evaluates several critical dimensions:
- Language: Checks for English proficiency while ignoring proper nouns.
- Date Coherence: Validates that the
endDateIsois consistent with the event's context (e.g., ensuring a 2026 World Cup event doesn't end in 2025). - Safety: Rejects content that encourages or incentivizes real-world harm (violence, terrorism, etc.).
- Deterministic Logic: Ensures resolution rules are objective and lack arbitrary fallbacks.
Validation States
The validation process uses both local Zod schema checks and LLM-based reasoning.
AI News Research (Web Search)
The system integrates with OpenRouter's web search capabilities to research context for featured markets on the home page carousel.
Research Workflow
When an administrator requests news context, the system constructs a prompt containing the event title, slug, and optional news source hints. It utilizes the web_search parameter in the OpenRouter completion options to find recent, relevant articles.
| Feature | Implementation |
|---|---|
| Search Context | Configurable as low, medium, or high. |
| Source Filtering | AI is instructed to avoid homepages, RSS feeds, and social profiles. |
| URL Validation | Results are passed through fetchHomeFeaturedNewsMetadata for verification. |
Configuration and Management
Administrators manage the AI integration via the Integrations tab in the Admin panel. This form allows for the configuration of the OpenRouter API Key and the selection of specific models.
Key Configuration Options
- API Key: Required for all AI features. Stored securely and masked in the UI.
- Preferred Model: A dropdown populated dynamically via
fetchOpenRouterModels. Administrators can select specific high-performance models or allow the system to "Let OpenRouter decide." - Web Search Requirements: Models displayed in the UI are filtered to include only those supporting
web_search_options.
Sources: src/app/[locale]/admin/integrations/_components/AdminIntegrationsForm.tsx:320-370, src/lib/ai/openrouter.ts:150-180
Summary
The OpenRouter AI & LLM integration is a multi-faceted system designed to enhance the quality and efficiency of prediction market operations. By combining traditional programmatic validation with LLM reasoning and real-time web search, the project ensures that events are created with professional standards, validated for safety and logic, and supported by current real-world data.