-
Notifications
You must be signed in to change notification settings - Fork 20
/
defaults.py
193 lines (144 loc) · 5.71 KB
/
defaults.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
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
from pathlib import Path
from typing import Any
from langchain_core.language_models import BaseChatModel
from ..backend.settings import FuncchainSettings
from .patches.llamacpp import ChatLlamaCpp
def get_gguf_model(
name: str,
label: str,
settings: FuncchainSettings,
) -> Path:
"""
Gather GGUF model from huggingface/TheBloke
possible input:
- DiscoLM-mixtral-8x7b-v2-GGUF
- TheBloke/DiscoLM-mixtral-8x7b-v2
- discolm-mixtral-8x7b-v2
...
Raises ModelNotFound(name) error in case of no result.
"""
from huggingface_hub import hf_hub_download
name = name.removeprefix("TheBloke/")
name = name.removesuffix("-GGUF")
label = "Q5_K_M" if label == "latest" else label
model_path = Path(settings.local_models_path)
if (p := model_path / f"{name.lower()}.{label}.gguf").exists():
return p
repo_id = f"TheBloke/{name}-GGUF"
filename = f"{name.lower()}.{label}.gguf"
try:
# todo make setting to turn prints off
print("\033c")
print("Downloading model from huggingface... (Ctrl+C to cancel)")
p = hf_hub_download(
repo_id,
filename,
local_dir=model_path,
local_dir_use_symlinks=True,
)
print("\033c")
return Path(p)
except Exception:
raise ValueError(f"ModelNotFound: {name}.{label}")
def default_model_fallback(
settings: FuncchainSettings,
**model_kwargs: Any,
) -> ChatLlamaCpp:
"""
Give user multiple options for local models to download.
"""
if input("ModelNotFound: Do you want to download a local model instead?").lower().startswith("y"):
model_kwargs.update(settings.llamacpp_kwargs())
return ChatLlamaCpp(
model_path=get_gguf_model("neural-chat-7b-v3-1", "Q4_K_M", settings).as_posix(),
**model_kwargs,
)
print("Please select a model to use funcchain!")
exit(0)
def univeral_model_selector(
settings: FuncchainSettings,
**model_kwargs: Any,
) -> BaseChatModel:
"""
Automatically selects the best possible model for a given ModelName.
You can use this schema:
"provider/model_name:"
and automatically select the right model for you.
You can add optional model kwargs like temperature.
Examples:
- "openai/gpt-3.5-turbo"
- "anthropic/claude-2"
- "llamacpp/openchat-3.5-0106" (theblock gguf models)
- "ollama/deepseek-llm-7b-chat"
Supported:
[ openai, anthropic, google, ollama ]
Raises:
- ModelNotFoundError, when the model is not found.
"""
if not isinstance(settings.llm, str) and settings.llm is not None:
return settings.llm
model_name = settings.llm if isinstance(settings.llm, str) else ""
model_kwargs.update(settings.model_kwargs())
if model_name:
mtype, name = model_name.split("/") if "/" in model_name else ("", model_name)
mtype = mtype.lower()
model_kwargs["model_name"] = name
try:
match mtype:
case "openai":
from langchain_openai import ChatOpenAI
model_kwargs.update(settings.openai_kwargs())
return ChatOpenAI(**model_kwargs)
case "anthropic":
from langchain_anthropic import ChatAnthropic
model_kwargs.pop("streaming", None)
return ChatAnthropic(**model_kwargs)
case "google":
from langchain_google_genai import ChatGoogleGenerativeAI
return ChatGoogleGenerativeAI(**model_kwargs)
case "ollama":
from .patches.ollama import ChatOllama
model = model_kwargs.pop("model_name")
model_kwargs.update(settings.ollama_kwargs())
return ChatOllama(model=model, **model_kwargs)
case "llamacpp" | "thebloke" | "gguf":
from .patches.llamacpp import ChatLlamaCpp
model_kwargs.pop("model_name")
name, label = name.split(":") if ":" in name else (name, "latest")
model_path = get_gguf_model(name, label, settings).as_posix()
print("\033[90m" f"using {model_path}" "\033[0m")
model_kwargs.update(settings.llamacpp_kwargs())
return ChatLlamaCpp(
model_path=model_path,
**model_kwargs,
)
except Exception as e:
print("ERROR:", e)
raise e
try:
if "gpt-4" in name or "gpt-3.5" in name:
from langchain_openai.chat_models import ChatOpenAI
model_kwargs.update(settings.openai_kwargs())
return ChatOpenAI(**model_kwargs)
except Exception as e:
print(e)
model_kwargs.pop("model_name", None)
if settings.openai_api_key:
from langchain_openai import ChatOpenAI
model_kwargs.update(settings.openai_kwargs())
return ChatOpenAI(**model_kwargs)
if settings.azure_api_key:
from langchain_openai import AzureChatOpenAI
return AzureChatOpenAI(**model_kwargs)
if settings.anthropic_api_key:
from langchain_anthropic import ChatAnthropic
model_kwargs.pop("streaming", None)
return ChatAnthropic(**model_kwargs)
if settings.google_api_key:
from langchain_google_genai import ChatGoogleGenerativeAI
return ChatGoogleGenerativeAI(**model_kwargs)
raise ValueError(
"Could not read llm selector string. Please check "
"[here](https://github.com/shroominic/funcchain/blob/main/MODELS.md) "
"for more info."
)