File size: 11,029 Bytes
5fed0fc |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 |
from abc import ABC, abstractmethod
from typing import Any, Tuple, Optional
from openai import OpenAI, APITimeoutError
import google.generativeai as genai
import os
from dotenv import load_dotenv
from anthropic import Anthropic, APITimeoutError as AnthropicAPITimeoutError
load_dotenv()
class LLMInterface(ABC):
"""
Abstract base class for integrating Large Language Models (LLMs) into a competitive programming context.
"""
def __init__(self):
"""
Initialize the LLMInterface with a predefined prompt for generating competitive programming solutions.
"""
self.prompt = """
You are a competitive programmer. You will be given a problem statement, please implement a solution in C++. The execution time and memory limit are also stated in the statement so be aware of the complexity of the program. Please wrap the code in ```cpp and ``` so that it is properly formatted. Your response should ONLY contain the C++ code, with no additional explanation or text.
"""
@abstractmethod
def call_llm(self, user_prompt: str) -> Tuple[str, Any]:
"""
Abstract method to interact with the LLM.
"""
pass
def generate_solution(self, problem_statement: str) -> Tuple[str, Any]:
"""
Generates a solution to a given competitive programming problem using the LLM.
"""
user_prompt = self.prompt + problem_statement
response, meta = self.call_llm(user_prompt)
return response, meta
class GPT(LLMInterface):
"""Concrete implementation of LLMInterface using OpenAI chat models."""
def __init__(
self,
model: str = "gpt-5",
reasoning_effort: Optional[str] = "high",
timeout: float = 600.0,
base_url: Optional[str] = None,
api_key: Optional[str] = None,
):
super().__init__()
resolved_key = api_key or os.getenv("OPENAI_API_KEY")
client_kwargs = {"api_key": resolved_key, "timeout": timeout}
if base_url:
client_kwargs["base_url"] = base_url
self.client = OpenAI(**client_kwargs)
self.name = 'gpt'
self.model = model
self.reasoning_effort = reasoning_effort
def call_llm(self, user_prompt: str) -> Tuple[str, Any]:
"""Sends the user prompt to the configured OpenAI model."""
try:
request_kwargs = {
"model": self.model,
"messages": [{"role": "user", "content": user_prompt}],
}
if self.reasoning_effort:
request_kwargs["reasoning_effort"] = self.reasoning_effort
completion = self.client.chat.completions.create(**request_kwargs)
return completion.choices[0].message.content, str(completion)
except APITimeoutError as e:
print(f"OpenAI API request timed out: {e}")
return "", str(e)
except Exception as e:
print(f"An unexpected error occurred while calling the OpenAI API: {e}")
return "", str(e)
class Gemini(LLMInterface):
"""
Concrete implementation of LLMInterface using Google's Gemini 2.5 Pro model.
Attributes:
model (genai.GenerativeModel): Instance for interacting with the Gemini API.
"""
def __init__(self, model: str = 'gemini-2.5-pro', timeout: float = 600.0, api_key: Optional[str] = None):
"""
Initializes the GeminiLLM class by configuring the API key and creating an
instance of the Gemini model.
"""
super().__init__()
try:
key_candidates = [
api_key,
os.getenv("GOOGLE_API_KEY"),
os.getenv("GEMINI_API_KEY"),
]
resolved_key = next((k for k in key_candidates if k), None)
if not resolved_key:
raise ValueError("GOOGLE_API_KEY not found in environment variables.")
self.api_key = resolved_key
genai.configure(api_key=self.api_key)
# Using a powerful and recent model. You can change this to other available models.
self.model_name = model
self.timeout = timeout
self.model = genai.GenerativeModel(self.model_name)
except Exception as e:
print(f"Error during Gemini initialization: {e}")
self.model = None
self.name = 'gemini'
def call_llm(self, user_prompt: str) -> Tuple[str, Any]:
"""
Sends the user prompt to the Gemini model and retrieves the solution.
"""
if not self.model:
return "Error: Model not initialized.", None
try:
if hasattr(self, "api_key") and self.api_key:
genai.configure(api_key=self.api_key)
response = self.model.generate_content(
user_prompt,
request_options={"timeout": self.timeout}
)
solution_text = response.text
return solution_text, response
except Exception as e:
print(f"An error occurred while calling the Gemini API: {e}")
return f"Error: {e}", None
class ClaudeBase(LLMInterface):
"""Shared Anthropic client wrapper."""
def __init__(
self,
model: str,
name: str = 'claude',
max_tokens: int = 32000,
thinking_budget: Optional[int] = 20000,
timeout: float = 600.0,
api_key: Optional[str] = None,
):
super().__init__()
resolved_key = api_key or os.getenv("ANTHROPIC_API_KEY")
self.client = Anthropic(api_key=resolved_key, timeout=timeout)
self.name = name
self.model = model
self.max_tokens = max_tokens
self.thinking_budget = thinking_budget
def call_llm(self, user_prompt: str) -> Tuple[str, Any]:
"""Sends the combined user prompt to Anthropic's model."""
try:
request_kwargs = {
"model": self.model,
"max_tokens": self.max_tokens,
"messages": [{"role": "user", "content": user_prompt}],
}
if self.thinking_budget:
request_kwargs["thinking"] = {
"type": "enabled",
"budget_tokens": self.thinking_budget,
}
completion = self.client.messages.create(**request_kwargs)
final_text = ""
if hasattr(completion, 'content') and completion.content:
for block in completion.content:
if getattr(block, 'type', None) == 'text' and hasattr(block, 'text'):
final_text += block.text
return final_text, str(completion)
except AnthropicAPITimeoutError as e:
print(f"Anthropic API request timed out: {e}")
return "", str(e)
except Exception as e:
print(f"An unexpected error occurred while calling the Anthropic API: {e}")
return "", str(e)
class Claude(ClaudeBase):
def __init__(self, model: str = "claude-sonnet-4-20250514", **kwargs):
super().__init__(model=model, **kwargs)
class Claude_Opus(ClaudeBase):
def __init__(self, model: str = "claude-opus-4-1-20250805", **kwargs):
super().__init__(model=model, **kwargs)
class Claude_Sonnet_4_5(ClaudeBase):
def __init__(self, model: str = "claude-sonnet-4-5-20250929", **kwargs):
super().__init__(model=model, **kwargs)
class DeepSeek(LLMInterface):
"""
Concrete implementation of LLMInterface using DeepSeek's models.
For deepseek-reasoner (R1/V3.2), max_tokens controls total output
including Chain-of-Thought reasoning. Default 32K, max 64K.
"""
def __init__(
self,
model: str = "deepseek-reasoner",
max_tokens: int = 32000,
timeout: float = 600.0,
base_url: str = "https://api.deepseek.com",
api_key: Optional[str] = None,
):
super().__init__()
resolved_key = api_key or os.getenv("DEEPSEEK_API_KEY")
self.client = OpenAI(
api_key=resolved_key,
base_url=base_url,
timeout=timeout
)
self.name = 'deepseek'
self.model = model
self.max_tokens = max_tokens
def call_llm(self, user_prompt: str) -> Tuple[str, Any]:
"""Sends the user prompt to DeepSeek's model."""
try:
request_kwargs = {
"model": self.model,
"messages": [{"role": "user", "content": user_prompt}],
"max_tokens": self.max_tokens,
}
completion = self.client.chat.completions.create(**request_kwargs)
return completion.choices[0].message.content, str(completion)
except APITimeoutError as e:
print(f"DeepSeek API request timed out: {e}")
return "", str(e)
except Exception as e:
print(f"An unexpected error occurred while calling the DeepSeek API: {e}")
return "", str(e)
class Grok(LLMInterface):
"""
Concrete implementation of LLMInterface using xAI's Grok models.
"""
def __init__(
self,
model: str = "grok-4",
reasoning_effort: Optional[str] = "high",
timeout: float = 600.0,
base_url: str = "https://api.x.ai/v1",
api_key: Optional[str] = None,
):
"""
Initializes the Grok class by creating an instance of the OpenAI client
pointed at the Grok API endpoint.
"""
super().__init__()
resolved_key = api_key or os.getenv("XAI_API_KEY") or os.getenv("GROK_API_KEY")
self.client = OpenAI(
api_key=resolved_key,
base_url=base_url,
timeout=timeout
)
self.name = 'grok'
self.model = model
self.reasoning_effort = reasoning_effort
def call_llm(self, user_prompt: str) -> Tuple[str, Any]:
"""
Sends the combined user prompt to Grok's model.
Args:
user_prompt (str): The complete prompt (system + problem).
Returns:
Tuple[str, Any]: The LLM's response and metadata.
"""
try:
# Reverted to the simpler, single-message format
request_kwargs = {
"model": self.model,
"messages": [
{"role": "user", "content": user_prompt}
]
}
if self.reasoning_effort:
request_kwargs["reasoning_effort"] = self.reasoning_effort
completion = self.client.chat.completions.create(**request_kwargs)
return completion.choices[0].message.content, str(completion)
except APITimeoutError as e:
print(f"Grok (xAI) API request timed out: {e}")
return "", str(e)
except Exception as e:
print(f"An unexpected error occurred while calling the Grok (xAI) API: {e}")
return "", str(e)
|