| import os |
| from openai import OpenAI |
| from anthropic import Anthropic |
| import google.generativeai as genai |
| import requests |
| from dotenv import load_dotenv |
|
|
| load_dotenv() |
|
|
| class LLMCaller: |
| def __init__(self, model_name: str = "gpt-4.1-mini-2025-04-14"): |
| """ |
| Initialize LLM model with unified interface |
| |
| Parameters: |
| - model_name: Model name to use, defaults to "gpt-4.1-mini-2025-04-14" |
| """ |
| self.model_name = model_name |
| self._setup_model() |
| |
| def _setup_model(self): |
| """Setup model configuration based on model name""" |
| if self.model_name.startswith("gpt"): |
| self.client = OpenAI( |
| api_key=os.getenv("OPENAI_API_KEY"), |
| base_url="https://api.openai.com/v1" |
| ) |
| self.api_type = "openai" |
| elif self.model_name.startswith("claude"): |
| self.client = Anthropic( |
| api_key=os.getenv("ANTHROPIC_API_KEY") |
| ) |
| self.api_type = "anthropic" |
| elif self.model_name.startswith("deepseek"): |
| self.api_key = os.getenv("DEEPSEEK_API_KEY") |
| self.api_url = "https://api.deepseek.com/v1/chat/completions" |
| self.api_type = "deepseek" |
| elif self.model_name.startswith("gemini"): |
| genai.configure(api_key=os.getenv("GOOGLE_API_KEY")) |
| self.model = genai.GenerativeModel(self.model_name) |
| self.api_type = "gemini" |
| elif self.model_name in ["llama-3-70b", "mixtral-8x7b", "qwen-72b"]: |
| self.api_url = "http://localhost:8000/v1" |
| self.api_type = "local" |
| else: |
| raise ValueError(f"Unsupported model: {self.model_name}") |
|
|
| def call(self, prompt: str) -> str: |
| """ |
| Call LLM API with a single prompt |
| |
| Parameters: |
| - prompt: Input prompt string |
| |
| Returns: |
| - response: Model's response |
| """ |
| try: |
| if self.api_type == "openai": |
| response = self.client.chat.completions.create( |
| model=self.model_name, |
| messages=[ |
| {"role": "system", "content": "You are a mathematical expert specializing in graph theory and persistent homology."}, |
| {"role": "user", "content": prompt} |
| ] |
| ) |
| return response.choices[0].message.content |
| |
| elif self.api_type == "anthropic": |
| response = self.client.messages.create( |
| model=self.model_name, |
| system="You are a helpful assistant.", |
| messages=[{"role": "user", "content": prompt}] |
| ) |
| return response.content[0].text |
| |
| elif self.api_type == "deepseek": |
| headers = { |
| "Authorization": f"Bearer {self.api_key}", |
| "Content-Type": "application/json" |
| } |
| data = { |
| "model": self.model_name, |
| "messages": [ |
| {"role": "user", "content": "/no_think" + prompt} |
| ] |
| } |
| response = requests.post(self.api_url, headers=headers, json=data) |
| response.raise_for_status() |
| return response.json()["choices"][0]["message"]["content"] |
| |
| elif self.api_type == "gemini": |
| response = self.model.generate_content(prompt) |
| return response.text |
| |
| elif self.api_type == "local": |
| data = { |
| "model": self.model_name, |
| "messages": [ |
| {"role": "user", "content": prompt} |
| ] |
| } |
| response = requests.post(self.api_url, json=data) |
| response.raise_for_status() |
| return response.json()["choices"][0]["message"]["content"] |
| |
| except Exception as e: |
| print(f"Error calling {self.model_name} API: {e}") |
| return f"API call error: {str(e)}" |
|
|
| def batch_call(self, prompts: list[str]) -> list[str]: |
| """ |
| Batch call LLM API with multiple prompts |
| |
| Parameters: |
| - prompts: List of input prompts |
| |
| Returns: |
| - responses: List of model responses |
| """ |
| responses = [] |
| for idx, prompt in enumerate(prompts, start=1): |
| print(f"Processing prompt {idx}/{len(prompts)}") |
| response = self.call(prompt) |
| responses.append(response) |
| return responses |