forked from apurvsinghgautam/robin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathllm_utils.py
More file actions
216 lines (192 loc) · 7.13 KB
/
llm_utils.py
File metadata and controls
216 lines (192 loc) · 7.13 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
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
import requests
from urllib.parse import urljoin
from langchain_openai import ChatOpenAI
from langchain_ollama import ChatOllama
from typing import Callable, Optional, List
from langchain_anthropic import ChatAnthropic
from langchain_google_genai import ChatGoogleGenerativeAI
from langchain_core.callbacks.base import BaseCallbackHandler
from config import OLLAMA_BASE_URL, OPENROUTER_BASE_URL, OPENROUTER_API_KEY, GOOGLE_API_KEY
class BufferedStreamingHandler(BaseCallbackHandler):
def __init__(self, buffer_limit: int = 60, ui_callback: Optional[Callable[[str], None]] = None):
self.buffer = ""
self.buffer_limit = buffer_limit
self.ui_callback = ui_callback
def on_llm_new_token(self, token: str, **kwargs) -> None:
self.buffer += token
if "\n" in token or len(self.buffer) >= self.buffer_limit:
print(self.buffer, end="", flush=True)
if self.ui_callback:
self.ui_callback(self.buffer)
self.buffer = ""
def on_llm_end(self, response, **kwargs) -> None:
if self.buffer:
print(self.buffer, end="", flush=True)
if self.ui_callback:
self.ui_callback(self.buffer)
self.buffer = ""
# --- Configuration Data ---
# Instantiate common dependencies once
_common_callbacks = [BufferedStreamingHandler(buffer_limit=60)]
# Define common parameters for most LLMs
_common_llm_params = {
"temperature": 0,
"streaming": True,
"callbacks": _common_callbacks,
}
# Map input model choices (lowercased) to their configuration
# Each config includes the class and any model-specific constructor parameters
_llm_config_map = {
'gpt-4.1': {
'class': ChatOpenAI,
'constructor_params': {'model_name': 'gpt-4.1'}
},
'gpt-5.1': {
'class': ChatOpenAI,
'constructor_params': {'model_name': 'gpt-5.1'}
},
'gpt-5-mini': {
'class': ChatOpenAI,
'constructor_params': {'model_name': 'gpt-5-mini'}
},
'gpt-5-nano': {
'class': ChatOpenAI,
'constructor_params': {'model_name': 'gpt-5-nano'}
},
'claude-sonnet-4-5': {
'class': ChatAnthropic,
'constructor_params': {'model': 'claude-sonnet-4-5'}
},
'claude-sonnet-4-0': {
'class': ChatAnthropic,
'constructor_params': {'model': 'claude-sonnet-4-0'}
},
'gemini-2.5-flash': {
'class': ChatGoogleGenerativeAI,
'constructor_params': {'model': 'gemini-2.5-flash', 'google_api_key': GOOGLE_API_KEY }
},
'gemini-2.5-flash-lite': {
'class': ChatGoogleGenerativeAI,
'constructor_params': {'model': 'gemini-2.5-flash-lite', 'google_api_key': GOOGLE_API_KEY}
},
'gemini-2.5-pro': {
'class': ChatGoogleGenerativeAI,
'constructor_params': {'model': 'gemini-2.5-pro', 'google_api_key': GOOGLE_API_KEY}
},
'gpt-5.1-openrouter': {
'class': ChatOpenAI,
'constructor_params': {
'model_name': 'openai/gpt-5.1',
'base_url': OPENROUTER_BASE_URL,
'api_key': OPENROUTER_API_KEY # Use OpenRouter API key
}
},
'gpt-5-mini-openrouter': {
'class': ChatOpenAI,
'constructor_params': {
'model_name': 'openai/gpt-5-mini',
'base_url': OPENROUTER_BASE_URL,
'api_key': OPENROUTER_API_KEY # Use OpenRouter API key
}
},
'claude-sonnet-4.5-openrouter': {
'class': ChatOpenAI,
'constructor_params': {
'model_name': 'anthropic/claude-sonnet-4.5',
'base_url': OPENROUTER_BASE_URL,
'api_key': OPENROUTER_API_KEY # Use OpenRouter API key
}
},
'grok-4.1-fast-openrouter': {
'class': ChatOpenAI,
'constructor_params': {
'model_name': 'x-ai/grok-4.1-fast',
'base_url': OPENROUTER_BASE_URL,
'api_key': OPENROUTER_API_KEY # Use OpenRouter API key
}
},
# 'llama3.2': {
# 'class': ChatOllama,
# 'constructor_params': {'model': 'llama3.2:latest', 'base_url': OLLAMA_BASE_URL}
# },
# 'llama3.1': {
# 'class': ChatOllama,
# 'constructor_params': {'model': 'llama3.1:latest', 'base_url': OLLAMA_BASE_URL}
# },
# 'gemma3': {
# 'class': ChatOllama,
# 'constructor_params': {'model': 'gemma3:latest', 'base_url': OLLAMA_BASE_URL}
# },
# 'deepseek-r1': {
# 'class': ChatOllama,
# 'constructor_params': {'model': 'deepseek-r1:latest', 'base_url': OLLAMA_BASE_URL}
# },
# Add more models here easily:
# 'mistral7b': {
# 'class': ChatOllama,
# 'constructor_params': {'model': 'mistral:7b', 'base_url': OLLAMA_BASE_URL}
# },
# 'gpt3.5': {
# 'class': ChatOpenAI,
# 'constructor_params': {'model_name': 'gpt-3.5-turbo', 'base_url': OLLAMA_BASE_URL}
# }
}
def _normalize_model_name(name: str) -> str:
return name.strip().lower()
def _get_ollama_base_url() -> Optional[str]:
if not OLLAMA_BASE_URL:
return None
return OLLAMA_BASE_URL.rstrip("/") + "/"
def fetch_ollama_models() -> List[str]:
"""
Retrieve the list of locally available Ollama models by querying the Ollama HTTP API.
Returns an empty list if the API isn't reachable or the base URL is not defined.
"""
base_url = _get_ollama_base_url()
if not base_url:
return []
try:
resp = requests.get(urljoin(base_url, "api/tags"), timeout=3)
resp.raise_for_status()
models = resp.json().get("models", [])
available = []
for m in models:
name = m.get("name") or m.get("model")
if name:
available.append(name)
return available
except (requests.RequestException, ValueError):
return []
def get_model_choices() -> List[str]:
"""
Combine the statically configured cloud models with the locally available Ollama models.
"""
base_models = list(_llm_config_map.keys())
dynamic_models = fetch_ollama_models()
normalized = {_normalize_model_name(m): m for m in base_models}
for dm in dynamic_models:
key = _normalize_model_name(dm)
if key not in normalized:
normalized[key] = dm
# Preserve the order: original base models first, then the dynamic ones in alphabetical order
ordered_dynamic = sorted(
[name for key, name in normalized.items() if name not in base_models],
key=_normalize_model_name,
)
return base_models + ordered_dynamic
def resolve_model_config(model_choice: str):
"""
Resolve a model choice (case-insensitive) to the corresponding configuration.
Supports both the predefined remote models and any locally installed Ollama models.
"""
model_choice_lower = _normalize_model_name(model_choice)
config = _llm_config_map.get(model_choice_lower)
if config:
return config
for ollama_model in fetch_ollama_models():
if _normalize_model_name(ollama_model) == model_choice_lower:
return {
"class": ChatOllama,
"constructor_params": {"model": ollama_model, "base_url": OLLAMA_BASE_URL},
}
return None