-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathllm_model_cache.py
60 lines (50 loc) · 2.39 KB
/
llm_model_cache.py
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
from config import Config
from helper_ollama_http import get_ollama_model_names
from llm_tools_manager import ToolManager
from llm_tools_manager import SettingsCache
from langchain_community.llms import Ollama
class ModelCache:
def __init__(self, config: Config):
self.model_cache = {}
self.config = config
self.settings_cache = SettingsCache()
self.model_server_url = config.get_value_by_key('chat', 'model_server_url',
'http://localhost:11434')
self.current_model_name = config.get_value_by_key('chat', 'model_name_default',
'mistral:instruct')
self.current_model_temperature = config.get_value_by_key('chat', 'model_temperature',
0.9)
self.current_model_num_predict = config.get_value_by_key('chat', 'model_num_predict',
128)
self.current_model = None
self.current_tools = None
self.available_models = get_ollama_model_names(self.model_server_url)
def load_model(self, model_name: str):
"""
Returns:
tuple: A tuple containing the model and its tools.
"""
if model_name in self.model_cache:
print(f"Found in cache {model_name}")
self.current_model_name = model_name
self.current_model, self.current_tools = self.model_cache[model_name]
else:
print(f"Model NOT found {model_name}")
model = Ollama(model=model_name, format='json',
temperature=self.current_model_temperature,
num_predict=self.current_model_num_predict)
tools = ToolManager(self.config, self.settings_cache)
self.set_model(model_name, model, tools)
return self.current_model, self.current_tools
def set_model(self, model_name: str, model, tools):
self.current_model_name = model_name
self.model_cache[model_name] = (model, tools)
self.current_model = model
self.current_tools = tools
def get_model(self):
"""
Get current model and tools, if any.
Returns:
tuple: A tuple containing the model and its tools.
"""
return self.current_model, self.current_tools