DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.
Python ai-agentsai-tutorclawbotcli-tooldeepresearchinteractive-learninglearningmulti-agent-systemsrag
a wrapper on top of lightrag for providing new llm providers
kushal gargf6e6b9fed34d
diff --git a/src/agents/solve/base_agent.py b/src/agents/solve/base_agent.pyindex 826f92571..b82440298 100644--- a/src/agents/solve/base_agent.py+++ b/src/agents/solve/base_agent.py@@ -10,7 +10,7 @@ from pathlib import Pathimport sysfrom typing import Any-from lightrag.llm.openai import openai_complete_if_cache+from .utils.token_tracker import TokenTracker@@ -20,6 +20,7 @@ if str(_project_root) not in sys.path:sys.path.insert(0, str(_project_root))from src.core.core import get_agent_params+from src.core.llm_factory import llm_completefrom src.core.logging import get_loggerfrom src.core.prompt_manager import get_prompt_manager@@ -61,6 +62,7 @@ class BaseAgent(ABC):# Get general LLM configurationself.llm_config = config.get("llm", {})+ self.binding = self.llm_config.get("binding", "openai")# Agent statusself.enabled = True@@ -222,7 +224,10 @@ class BaseAgent(ABC):)kwargs["token_tracker"] = token_tracker_wrapper- response = await openai_complete_if_cache(**kwargs)+ response = await llm_complete(+ binding=self.binding,+ **kwargs+ )# If token_tracker exists but didn't get usage info from API, try using more precise methodif self.token_tracker and token_tracker_wrapper and not token_tracker_wrapper.usage:diff --git a/src/api/routers/llm_provider.py b/src/api/routers/llm_provider.pyindex 8926d8d3d..fe19faafb 100644--- a/src/api/routers/llm_provider.py+++ b/src/api/routers/llm_provider.py@@ -1,9 +1,8 @@from typing import Any, Dict, Listfrom fastapi import APIRouter, HTTPException-from lightrag.llm.openai import openai_complete_if_cachefrom pydantic import BaseModel-+from src.core.llm_factory import llm_complete, llm_fetch_modelsfrom src.core.llm_provider import LLMProvider, provider_managerrouter = APIRouter()@@ -32,7 +31,7 @@ async def add_provider(provider: LLMProvider):raise HTTPException(status_code=400, detail=str(e))[email protected]("/{name}", response_model=LLMProvider)[email protected]("/{name}/", response_model=LLMProvider)async def update_provider(name: str, updates: Dict[str, Any]):"""Update an existing LLM provider."""provider = provider_manager.update_provider(name, updates)@@ -41,7 +40,16 @@ async def update_provider(name: str, updates: Dict[str, Any]):return provider[email protected]("/{name}")[email protected]("/")+async def delete_provider_by_query(name: str):+ """Delete an LLM provider (query param version)."""+ success = provider_manager.delete_provider(name)+ if not success:+ raise HTTPException(status_code=404, detail="Provider not found")+ return {"message": "Provider deleted"}++[email protected]("/{name}/")async def delete_provider(name: str):"""Delete an LLM provider."""success = provider_manager.delete_provider(name)@@ -50,7 +58,7 @@ async def delete_provider(name: str):return {"message": "Provider deleted"}[email protected]("/active", response_model=LLMProvider)[email protected]("/active/", response_model=LLMProvider)async def set_active_provider(name_payload: Dict[str, str]):"""Set the active LLM provider."""name = name_payload.get("name")@@ -63,7 +71,7 @@ async def set_active_provider(name_payload: Dict[str, str]):return provider[email protected]("/test", response_model=Dict[str, Any])[email protected]("/test/", response_model=Dict[str, Any])async def test_connection(request: TestConnectionRequest):"""Test connection to an LLM provider."""try:@@ -71,6 +79,14 @@ async def test_connection(request: TestConnectionRequest):# Users often paste full endpoints like http://.../v1/chat/completions# OpenAI client needs just the base (e.g., http://.../v1)base_url = request.base_url.rstrip("/")++ # Special handling for Ollama: if it ends in /api, it's likely wrong for completion but ok for tags+ # But here we want the completion base.+ if "/api" in base_url and not base_url.endswith("/v1"):+ # If user has http://localhost:11434/api -> change to http://localhost:11434/v1+ if ":11434" in base_url or "ollama" in base_url.lower():+ base_url = base_url.replace("/api", "/v1")+for suffix in ["/chat/completions", "/completions"]:if base_url.endswith(suffix):base_url = base_url[: -len(suffix)]@@ -83,14 +99,39 @@ async def test_connection(request: TestConnectionRequest):else:api_key_to_use = request.api_key- response = await openai_complete_if_cache(+ response = await llm_complete(model=request.model,prompt="Hello, are you working?",system_prompt="You are a helpful assistant. Reply with 'Yes'.",api_key=api_key_to_use,base_url=base_url,- max_tokens=10,+ binding=request.binding,+ max_tokens=200,)return {"success": True, "message": "Connection successful", "response": response}except Exception as e:return {"success": False, "message": f"Connection failed: {str(e)}"}++[email protected]("/models/", response_model=Dict[str, Any])+async def fetch_available_models(request: TestConnectionRequest):+ """Fetch available models from the provider."""+ try:+ # Sanitize Base URL (same as test_connection)+ base_url = request.base_url.rstrip("/")+ if "/api" in base_url and not base_url.endswith("/v1"):+ if ":11434" in base_url or "ollama" in base_url.lower():+ base_url = base_url.replace("/api", "/v1")++ for suffix in ["/chat/completions", "/completions"]:+ if base_url.endswith(suffix):+ base_url = base_url[: -len(suffix)]++ models = await llm_fetch_models(+ binding=request.binding,+ base_url=base_url,+ api_key=request.api_key if request.requires_key else None+ )+ return {"success": True, "models": models}+ except Exception as e:+ return {"success": False, "message": f"Failed to fetch models: {str(e)}"}diff --git a/src/core/llm_factory.py b/src/core/llm_factory.pynew file mode 100644index 000000000..c71960721--- /dev/null+++ b/src/core/llm_factory.py@@ -0,0 +1,300 @@+import os+import asyncio+from typing import Any, Callable, Dict, List, Optional+import aiohttp+from lightrag.llm.openai import openai_complete_if_cache++class LLMFactory:+ """+ Factory for creating LLM completion functions for various providers.+ Provides a unified interface for calling different LLM APIs.+ """++ @staticmethod+ async def anthropic_complete(+ model: str,+ prompt: str,+ system_prompt: str = "You are a helpful assistant.",+ api_key: Optional[str] = None,+ base_url: Optional[str] = None,+ **kwargs+ ) -> str:+ """+ Wrapper for Anthropic (Claude) API.+ """+ api_key = api_key or os.getenv("ANTHROPIC_API_KEY")+ if not api_key:+ raise ValueError("Anthropic API key is missing.")++ # Default Anthropic API URL if not provided+ if not base_url:+ url = "https://api.anthropic.com/v1/messages"+ else:+ url = base_url.rstrip("/")+ if not url.endswith("/messages"):+ url += "/messages"++ headers = {+ "x-api-key": api_key,+ "anthropic-version": "2023-06-01",+ "content-type": "application/json",+ }++ data = {+ "model": model,+ "system": system_prompt,+ "messages": [{"role": "user", "content": prompt}],+ "max_tokens": kwargs.get("max_tokens", 4096),+ "temperature": kwargs.get("temperature", 0.7),+ }++ async with aiohttp.ClientSession() as session:+ async with session.post(url, headers=headers, json=data) as response:+ if response.status != 200:+ error_text = await response.text()+ raise Exception(f"Anthropic API error: {response.status} - {error_text}")++ result = await response.json()+ # Anthropic returns a list of content blocks+ return result["content"][0]["text"]++ @staticmethod+ def get_completion_function(binding: str) -> Callable:+ """+ Returns the appropriate completion function based on the provider binding.+ """+ binding = binding.lower()++ if binding == "anthropic" or binding == "claude":+ return LLMFactory.anthropic_complete++ # For ollama-cloud, we usually treat it as OpenAI compatible or use a specific endpoint+ # If it's just a hosted Ollama, it's often OpenAI compatible (/v1)+ if binding == "ollama-cloud":+ # For now, treat it as OpenAI compatible+ return openai_complete_if_cache++ # Default to OpenAI-compatible for most (openai, azure_openai, ollama local, openrouter, deepseek, gemini)+ return openai_complete_if_cache++ @staticmethod+ async def fetch_models(+ binding: str,+ base_url: str,+ api_key: Optional[str] = None+ ) -> List[str]:+ """+ Fetch available models from the provider.+ """+ binding = binding.lower()+ base_url = base_url.rstrip("/")++ headers = {}+ if api_key:+ if binding in ["anthropic", "claude"]:+ headers["x-api-key"] = api_key+ else:+ headers["Authorization"] = f"Bearer {api_key}"++ async with aiohttp.ClientSession() as session:+ try:+ # Strategy 1: Local Ollama /api/tags+ # Try this for anything that looks like local Ollama or specifically has 'ollama' in binding/url+ # but NOT if it's explicitly 'ollama.com' (cloud) unless it's a fallback+ is_likely_local_ollama = (binding == "ollama" or "ollama" in base_url.lower()) and "ollama.com" not in base_url.lower() or ":11434" in base_url++ if is_likely_local_ollama:+ url = base_url.rstrip("/").replace("/v1", "") + "/api/tags"+ try:+ async with session.get(url, headers=headers) as resp:+ if resp.status == 200:+ data = await resp.json()+ if "models" in data:+ return [m["name"] for m in data.get("models", [])]+ except Exception:+ pass # Try Strategy 2 if this fails++ # Strategy 2: Standard OpenAI /models (also used by Ollama Cloud)+ url = f"{base_url.rstrip('/')}/models"+ async with session.get(url, headers=headers) as resp:+ if resp.status == 200:+ data = await resp.json()+ # Handle standard OpenAI format: {"data": [{"id": "model-id"}, ...]}+ if "data" in data and isinstance(data["data"], list):+ return [m.get("id") or m.get("name") for m in data["data"] if m.get("id") or m.get("name")]+ # Handle some providers that use {"models": ["model1", ...]}+ elif "models" in data and isinstance(data["models"], list):+ if data["models"] and isinstance(data["models"][0], dict):+ return [m.get("id") or m.get("name") for m in data["models"] if m.get("id") or m.get("name")]+ else:+ return [str(m) for m in data["models"]]+ # Handle raw list response+ elif isinstance(data, list):+ if data and isinstance(data[0], dict):+ return [m.get("id") or m.get("name") for m in data if m.get("id") or m.get("name")]+ else:+ return [str(m) for m in data]++ # Fallback Strategy: If it's ollama.com but /models failed, try /api/tags as well+ if "ollama.com" in base_url:+ url = base_url.rstrip("/").replace("/v1", "") + "/api/tags"+ try:+ async with session.get(url, headers=headers) as resp:+ if resp.status == 200:+ data = await resp.json()+ return [m["name"] for m in data.get("models", [])]+ except Exception:+ pass++ return []+ except Exception as e:+ print(f"Error fetching models from {base_url}: {e}")+ return []++# Unified interface for agents+def sanitize_url(base_url: str, model: str = "") -> str:+ """+ Sanitize base URL for OpenAI-compatible APIs, with special handling for Ollama.+ """+ if not base_url:+ return base_url++ url = base_url.rstrip("/")++ # Ensure URL has a protocol (default to http for local/ollama)+ if url and not url.startswith(("http://", "https://")):+ url = "http://" + url++ # Standard OpenAI client library is strict about URLs:+ # - No trailing slashes+ # - No /chat/completions or /completions/messages/embeddings suffixes (it adds these automatically)+ for suffix in ["/chat/completions", "/completions", "/messages", "/embeddings"]:+ if url.endswith(suffix):+ url = url[:-len(suffix)]+ url = url.rstrip("/")++ # Special handling for Ollama: local/self-hosted Ollama requires /v1+ # for OpenAI compatibility. If it's port 11434 or has 'ollama' in URL+ # and it's not the cloud version (ollama.com), ensure it ends in /v1.+ if ":11434" in url or ("ollama" in url.lower() and "ollama.com" not in url.lower()):+ if not url.endswith("/v1"):+ url = url.rstrip("/") + "/v1"++ return url+++async def llm_complete(+ model: str,+ prompt: str,+ system_prompt: str = "You are a helpful assistant.",+ binding: str = "openai",+ api_key: Optional[str] = None,+ base_url: Optional[str] = None,+ **kwargs+) -> str:+ """+ Call the appropriate LLM provider based on binding.+ Includes a direct aiohttp fallback for reasoning models and OpenAI-compatible services.+ """+ complete_fn = LLMFactory.get_completion_function(binding)++ # Sanitize base_url for OpenAI-compatible endpoints+ # The OpenAI client library is strict about URLs:+ # - No trailing slashes+ # - No /chat/completions or /completions suffixes (it adds these automatically)+ if base_url and (complete_fn == openai_complete_if_cache or binding in ["openai", "ollama"]):+ original_url = base_url+ base_url = sanitize_url(base_url, model)++ if original_url != base_url:+ print(f"π§ URL Sanitized: {original_url} -> {base_url}")+ print(f"π LLM Call: model={model}, binding={binding}, base_url={base_url}")++ try:+ # 1. Attempt using standard driver+ if complete_fn == openai_complete_if_cache:+ response = await complete_fn(+ model=model,+ prompt=prompt,+ system_prompt=system_prompt,+ api_key=api_key,+ base_url=base_url,+ **kwargs+ )+ else:+ response = await complete_fn(+ model=model,+ prompt=prompt,+ system_prompt=system_prompt,+ api_key=api_key,+ base_url=base_url,+ **kwargs+ )++ # If response is empty, it might be a reasoning model that lightrag can't parse+ if not response:+ raise ValueError("Empty response from standard driver")++ return response++ except Exception as e:+ # 2. Fallback to direct aiohttp call for OpenAI-compatible bindings+ # This is useful for reasoning models (DeepSeek R1, Kimi) that might+ # return empty content but have reasoning in other fields.+ is_openai_compatible = binding.lower() in ["openai", "ollama", "ollama-cloud", "deepseek", "openrouter", "gemini"]++ if is_openai_compatible and base_url:+ try:+ url = base_url.rstrip("/")+ if not url.endswith("/chat/completions"):+ url += "/chat/completions"++ print(f"π Fallback Direct Call: {url}")++ headers = {+ "Content-Type": "application/json",+ "Authorization": f"Bearer {api_key}" if api_key else ""+ }++ data = {+ "model": model,+ "messages": [+ {"role": "system", "content": system_prompt},+ {"role": "user", "content": prompt}+ ],+ "temperature": kwargs.get("temperature", 0.7),+ "max_tokens": kwargs.get("max_tokens", 4096)+ }++ async with aiohttp.ClientSession() as session:+ async with session.post(url, headers=headers, json=data) as resp:+ if resp.status == 200:+ result = await resp.json()+ if "choices" in result and result["choices"]:+ msg = result["choices"][0].get("message", {})+ content = msg.get("content", "")++ # If content is empty, look for reasoning/thinking fields+ if not content:+ content = msg.get("reasoning_content") or msg.get("reasoning") or msg.get("thought") or ""++ if content:+ return content+ else:+ error_text = await resp.text()+ print(f"β Fallback call failed with status={resp.status}: {error_text}")+ except Exception as e2:+ print(f"β Fallback direct call failed: {e2}")++ # If fallback also fails or isn't applicable, re-raise original+ raise e++async def llm_fetch_models(+ binding: str,+ base_url: str,+ api_key: Optional[str] = None+) -> List[str]:+ """+ Fetch available models from the provider.+ """+ return await LLMFactory.fetch_models(binding, base_url, api_key)