-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathllm_manager.py
More file actions
157 lines (134 loc) · 6.99 KB
/
Copy pathllm_manager.py
File metadata and controls
157 lines (134 loc) · 6.99 KB
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
import os
import json
import re
import traceback
from typing import Any, Dict, List, Type, Union
from pydantic import BaseModel
from dotenv import load_dotenv
# LangChain imports
from langchain_google_genai import ChatGoogleGenerativeAI
from langchain_openai import ChatOpenAI
from langchain_core.messages import BaseMessage, SystemMessage, HumanMessage, AIMessage
from langchain_core.runnables import Runnable
load_dotenv()
# List of models to try in order
FALLBACK_MODELS = [
{"provider": "google", "model": "gemini-2.5-flash"},
{"provider": "groq", "model": "llama-3.3-70b-versatile"},
{"provider": "groq", "model": "llama-3.1-8b-instant"},
{"provider": "openrouter", "model": "inclusionai/ling-3.0-flash:free"},
{"provider": "openrouter", "model": "poolside/laguna-s-2.1:free"},
{"provider": "openrouter", "model": "poolside/laguna-xs-2.1:free"}
]
class FallbackLLMManager:
def __init__(self):
self.current_model_idx = 0
self.google_api_key = os.getenv("GOOGLE_API_KEY")
self.groq_api_key = os.getenv("GROQ_API_KEY")
self.openrouter_api_key = os.getenv("OPENROUTER_API_KEY")
def _get_llm(self, provider: str, model_name: str) -> Union[ChatGoogleGenerativeAI, ChatOpenAI]:
if provider == "google":
if not self.google_api_key:
raise ValueError("GOOGLE_API_KEY is not set")
return ChatGoogleGenerativeAI(
model=model_name,
google_api_key=self.google_api_key,
temperature=0.7
)
elif provider == "groq":
if not self.groq_api_key:
raise ValueError("GROQ_API_KEY is not set")
return ChatOpenAI(
model=model_name,
openai_api_key=self.groq_api_key,
openai_api_base="https://api.groq.com/openai/v1",
temperature=0.7
)
elif provider == "openrouter":
if not self.openrouter_api_key:
raise ValueError("OPENROUTER_API_KEY is not set")
return ChatOpenAI(
model=model_name,
openai_api_key=self.openrouter_api_key,
openai_api_base="https://openrouter.ai/api/v1",
temperature=0.7
)
else:
raise ValueError(f"Unknown provider: {provider}")
def invoke(self, messages: List[BaseMessage], **kwargs) -> AIMessage:
attempts = 0
max_attempts = len(FALLBACK_MODELS)
while attempts < max_attempts:
model_info = FALLBACK_MODELS[self.current_model_idx]
provider = model_info["provider"]
model_name = model_info["model"]
try:
print(f"[LLM] Attempting invoke with model: {provider}/{model_name}")
llm = self._get_llm(provider, model_name)
response = llm.invoke(messages, **kwargs)
return response
except Exception as e:
print(f"[LLM] Error using {provider}/{model_name}: {e}")
# Move to next model
self.current_model_idx = (self.current_model_idx + 1) % len(FALLBACK_MODELS)
attempts += 1
raise RuntimeError("All fallback models failed to respond to invoke.")
def with_structured_output(self, schema: Type[BaseModel]) -> 'StructuredFallbackLLM':
return StructuredFallbackLLM(self, schema)
class StructuredFallbackLLM:
def __init__(self, manager: FallbackLLMManager, schema: Type[BaseModel]):
self.manager = manager
self.schema = schema
def _clean_json_string(self, text: str) -> str:
# Find first '{' and last '}'
start_idx = text.find('{')
end_idx = text.rfind('}')
if start_idx != -1 and end_idx != -1:
return text[start_idx:end_idx + 1]
return text
def invoke(self, messages: List[BaseMessage], **kwargs) -> BaseModel:
attempts = 0
max_attempts = len(FALLBACK_MODELS)
while attempts < max_attempts:
model_info = FALLBACK_MODELS[self.manager.current_model_idx]
provider = model_info["provider"]
model_name = model_info["model"]
try:
print(f"[Structured LLM] Attempting structured output with: {provider}/{model_name}")
llm = self.manager._get_llm(provider, model_name)
# We try native structured output first
try:
structured_llm = llm.with_structured_output(self.schema)
res = structured_llm.invoke(messages, **kwargs)
return res
except Exception as structured_err:
print(f"[Structured LLM] Native structured output failed for {provider}/{model_name}: {structured_err}")
print("[Structured LLM] Falling back to manual JSON prompt parsing...")
# Fallback to manual JSON formatting and parsing
schema_json = json.dumps(self.schema.model_json_schema(), indent=2)
json_instruction = (
f"\n\nYou MUST return a JSON object that strictly conforms to the following schema:\n"
f"{schema_json}\n\n"
f"CRITICAL: Return ONLY valid JSON. Do not include any explanations, markdown code blocks, "
f"introductory text, or trailing content. Your entire response must be parseable as a single JSON object."
)
# Clone messages and modify system message or add instruction
fallback_messages = list(messages)
if len(fallback_messages) > 0 and isinstance(fallback_messages[0], SystemMessage):
fallback_messages[0] = SystemMessage(content=fallback_messages[0].content + json_instruction)
else:
fallback_messages.append(HumanMessage(content=json_instruction))
res_raw = llm.invoke(fallback_messages, **kwargs)
raw_text = res_raw.content
if isinstance(raw_text, list):
raw_text = "".join(item if isinstance(item, str) else item.get("text", "") for item in raw_text)
cleaned_json = self._clean_json_string(raw_text)
parsed_obj = json.loads(cleaned_json)
validated_obj = self.schema.model_validate(parsed_obj)
return validated_obj
except Exception as e:
print(f"[Structured LLM] Error using {provider}/{model_name}: {e}")
# Move to next model
self.manager.current_model_idx = (self.manager.current_model_idx + 1) % len(FALLBACK_MODELS)
attempts += 1
raise RuntimeError("All fallback models failed to respond to structured invoke.")