-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathllm_utils.py
More file actions
79 lines (71 loc) · 2.64 KB
/
llm_utils.py
File metadata and controls
79 lines (71 loc) · 2.64 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
from config import OLLAMA_BASE_URL
from typing import Callable, Optional
from langchain_openai import ChatOpenAI
from langchain_ollama import ChatOllama
from langchain_anthropic import ChatAnthropic
from langchain_google_genai import ChatGoogleGenerativeAI
from langchain_core.callbacks.base import BaseCallbackHandler
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 = {
'gpt4o': {
'class': ChatOpenAI,
'constructor_params': {'model_name': 'gpt-4o'}
},
'gpt-4.1': {
'class': ChatOpenAI,
'constructor_params': {'model_name': 'gpt-4.1'}
},
'claude-3-5-sonnet-latest': {
'class': ChatAnthropic,
'constructor_params': {'model': 'claude-3-5-sonnet-latest'}
},
'llama3.1': {
'class': ChatOllama,
'constructor_params': {'model': 'llama3.1:latest', 'base_url': OLLAMA_BASE_URL}
},
'gemini-2.5-flash': {
'class': ChatGoogleGenerativeAI,
'constructor_params': {'model': 'models/gemini-2.5-flash'}
},
'gemini-flash-latest': {
'class': ChatGoogleGenerativeAI,
'constructor_params': {'model': 'models/gemini-flash-latest'}
}
# 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}
# }
}