Skip to content

Commit 0510a5c

Browse files
author
Arena AI Agent
committed
feat(core): 🌐 implement omni-provider engine (OpenAI, Anthropic, Gemini, Ollama) with max flexibility
1 parent 71ca139 commit 0510a5c

2 files changed

Lines changed: 45 additions & 25 deletions

File tree

ā€Žepistemic_forge/llm.pyā€Ž

Lines changed: 44 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,63 @@
1-
"""SOTA LLM Engine using Instructor and Pydantic for Strict Structured Outputs.
2-
ENFORCES: Reproducibility (Seed 42, Temp 0.0) for scientific benchmarks.
1+
"""Omni-Provider LLM Engine for Absolute Flexibility.
2+
Supports: OpenAI, Anthropic, Gemini, Ollama, vLLM, Azure, etc.
3+
Enforces: Strict Pydantic JSON Schemas.
34
"""
4-
import instructor
5-
from openai import OpenAI
65
from pydantic import BaseModel
76
from loguru import logger
87
from tenacity import retry, stop_after_attempt, wait_exponential
8+
import instructor
9+
from litellm import completion
910

10-
try:
11-
client = instructor.from_openai(OpenAI())
12-
except Exception as e:
13-
logger.warning(f"OpenAI client init failed (missing key?): {e}")
14-
client = None
11+
def get_instructor_client(model: str):
12+
"""Dynamically route the instructor client based on the model provider."""
13+
import openai
14+
import anthropic
15+
import google.generativeai as genai
16+
17+
if model.startswith("claude"):
18+
return instructor.from_anthropic(anthropic.Anthropic())
19+
elif model.startswith("gemini"):
20+
return instructor.from_gemini(genai.GenerativeModel(model))
21+
else:
22+
# Default to OpenAI / LiteLLM proxy / Ollama Local
23+
return instructor.from_openai(openai.OpenAI())
1524

1625
@retry(stop=stop_after_attempt(3), wait=wait_exponential(multiplier=1, min=2, max=10))
1726
def generate_structured(
1827
messages: list,
1928
response_model: type[BaseModel],
2029
model: str = "gpt-4o-mini",
2130
temperature: float = 0.0,
22-
seed: int = 42
31+
seed: int = 42,
32+
api_base: str = None
2333
) -> BaseModel:
2434
"""
25-
Research-Grade Extraction.
26-
Enforces Temperature=0.0 and Seed=42 to guarantee deterministic, reproducible scientific output.
35+
Omni-Provider Structured Extraction.
36+
Allows passing ANY model (local Ollama, Claude, GPT, Groq).
2737
"""
28-
if not client:
29-
raise ValueError("LLM Client is not initialized. Please set OPENAI_API_KEY.")
30-
3138
try:
32-
logger.debug(f"Initiating scientifically rigorous call to {model} [temp={temperature}, seed={seed}] for schema [{response_model.__name__}]...")
33-
response = client.chat.completions.create(
34-
model=model,
35-
messages=messages,
36-
response_model=response_model,
37-
temperature=temperature,
38-
seed=seed
39-
)
39+
logger.debug(f"Routing neural call to [{model}] for schema [{response_model.__name__}]...")
40+
41+
# We use instructor's dynamic client routing
42+
client = get_instructor_client(model)
43+
44+
kwargs = {
45+
"model": model,
46+
"messages": messages,
47+
"response_model": response_model,
48+
"temperature": temperature,
49+
}
50+
51+
# Only inject seed if the provider supports it (like OpenAI)
52+
if "gpt" in model or "llama" in model:
53+
kwargs["seed"] = seed
54+
55+
if api_base: # For Local Ollama or vLLM routing
56+
kwargs["base_url"] = api_base
57+
58+
response = client.chat.completions.create(**kwargs)
4059
return response
60+
4161
except Exception as e:
42-
logger.error(f"SOTA LLM API Critical Failure: {str(e)}")
62+
logger.error(f"Omni-Provider API Failure for {model}: {str(e)}")
4363
raise

ā€Žpyproject.tomlā€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ classifiers = [
3131
"Topic :: Scientific/Engineering :: Artificial Intelligence",
3232
"Topic :: Text Processing :: Linguistic",
3333
]
34-
dependencies = ["instructor>=1.3.0", "openai>=1.30.0", "pydantic>=2.0.0", "loguru>=0.7.0", "tenacity>=8.0.0"]
34+
dependencies = ["litellm>=1.0.0", "pydantic>=2.0.0", "loguru>=0.7.0", "tenacity>=8.0.0", "instructor>=1.3.0"]
3535

3636
[project.optional-dependencies]
3737
dev = ["pytest>=7.0"]

0 commit comments

Comments
Ā (0)