-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Expand file tree
/
Copy pathollama_model_provider.py
More file actions
279 lines (260 loc) · 12.8 KB
/
ollama_model_provider.py
File metadata and controls
279 lines (260 loc) · 12.8 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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# coding=utf-8
"""
@project: maxkb
@Author:虎
@file: ollama_model_provider.py
@date:2024/3/5 17:23
@desc:
"""
import json
import os
from typing import Dict, Iterator
from urllib.parse import urlparse, ParseResult
import requests
from common.utils.common import get_file_content
from models_provider.base_model_provider import IModelProvider, ModelProvideInfo, ModelInfo, ModelTypeConst, \
BaseModelCredential, DownModelChunk, DownModelChunkStatus, ValidCode, ModelInfoManage
from models_provider.impl.ollama_model_provider.credential.embedding import OllamaEmbeddingModelCredential
from models_provider.impl.ollama_model_provider.credential.image import OllamaImageModelCredential
from models_provider.impl.ollama_model_provider.credential.llm import OllamaLLMModelCredential
from models_provider.impl.ollama_model_provider.credential.reranker import OllamaReRankModelCredential
from models_provider.impl.ollama_model_provider.model.embedding import OllamaEmbedding
from models_provider.impl.ollama_model_provider.model.image import OllamaImage
from models_provider.impl.ollama_model_provider.model.llm import OllamaChatModel
from models_provider.impl.ollama_model_provider.model.reranker import OllamaReranker
from maxkb.conf import PROJECT_DIR
from django.utils.translation import gettext as _
""
ollama_llm_model_credential = OllamaLLMModelCredential()
model_info_list = [
ModelInfo(
'deepseek-r1:1.5b',
'',
ModelTypeConst.LLM, ollama_llm_model_credential, OllamaChatModel),
ModelInfo(
'deepseek-r1:7b',
'',
ModelTypeConst.LLM, ollama_llm_model_credential, OllamaChatModel),
ModelInfo(
'deepseek-r1:8b',
'',
ModelTypeConst.LLM, ollama_llm_model_credential, OllamaChatModel),
ModelInfo(
'deepseek-r1:14b',
'',
ModelTypeConst.LLM, ollama_llm_model_credential, OllamaChatModel),
ModelInfo(
'deepseek-r1:32b',
'',
ModelTypeConst.LLM, ollama_llm_model_credential, OllamaChatModel),
ModelInfo(
'llama2',
_('Llama 2 is a set of pretrained and fine-tuned generative text models ranging in size from 7 billion to 70 billion. This is a repository of 7B pretrained models. Links to other models can be found in the index at the bottom.'),
ModelTypeConst.LLM, ollama_llm_model_credential, OllamaChatModel),
ModelInfo(
'llama2:13b',
_('Llama 2 is a set of pretrained and fine-tuned generative text models ranging in size from 7 billion to 70 billion. This is a repository of 13B pretrained models. Links to other models can be found in the index at the bottom.'),
ModelTypeConst.LLM, ollama_llm_model_credential, OllamaChatModel),
ModelInfo(
'llama2:70b',
_('Llama 2 is a set of pretrained and fine-tuned generative text models ranging in size from 7 billion to 70 billion. This is a repository of 70B pretrained models. Links to other models can be found in the index at the bottom.'),
ModelTypeConst.LLM, ollama_llm_model_credential, OllamaChatModel),
ModelInfo(
'llama2-chinese:13b',
_('Since the Chinese alignment of Llama2 itself is weak, we use the Chinese instruction set to fine-tune meta-llama/Llama-2-13b-chat-hf with LoRA so that it has strong Chinese conversation capabilities.'),
ModelTypeConst.LLM, ollama_llm_model_credential, OllamaChatModel),
ModelInfo(
'llama3:8b',
_('Meta Llama 3: The most capable public product LLM to date. 8 billion parameters.'),
ModelTypeConst.LLM, ollama_llm_model_credential, OllamaChatModel),
ModelInfo(
'llama3:70b',
_('Meta Llama 3: The most capable public product LLM to date. 70 billion parameters.'),
ModelTypeConst.LLM, ollama_llm_model_credential, OllamaChatModel),
ModelInfo(
'qwen:0.5b',
_("Compared with previous versions, qwen 1.5 0.5b has significantly enhanced the model's alignment with human preferences and its multi-language processing capabilities. Models of all sizes support a context length of 32768 tokens. 500 million parameters."),
ModelTypeConst.LLM, ollama_llm_model_credential, OllamaChatModel),
ModelInfo(
'qwen:1.8b',
_("Compared with previous versions, qwen 1.5 1.8b has significantly enhanced the model's alignment with human preferences and its multi-language processing capabilities. Models of all sizes support a context length of 32768 tokens. 1.8 billion parameters."),
ModelTypeConst.LLM, ollama_llm_model_credential, OllamaChatModel),
ModelInfo(
'qwen:4b',
_("Compared with previous versions, qwen 1.5 4b has significantly enhanced the model's alignment with human preferences and its multi-language processing capabilities. Models of all sizes support a context length of 32768 tokens. 4 billion parameters."),
ModelTypeConst.LLM, ollama_llm_model_credential, OllamaChatModel),
ModelInfo(
'qwen:7b',
_("Compared with previous versions, qwen 1.5 7b has significantly enhanced the model's alignment with human preferences and its multi-language processing capabilities. Models of all sizes support a context length of 32768 tokens. 7 billion parameters."),
ModelTypeConst.LLM, ollama_llm_model_credential, OllamaChatModel),
ModelInfo(
'qwen:14b',
_("Compared with previous versions, qwen 1.5 14b has significantly enhanced the model's alignment with human preferences and its multi-language processing capabilities. Models of all sizes support a context length of 32768 tokens. 14 billion parameters."),
ModelTypeConst.LLM, ollama_llm_model_credential, OllamaChatModel),
ModelInfo(
'qwen:32b',
_("Compared with previous versions, qwen 1.5 32b has significantly enhanced the model's alignment with human preferences and its multi-language processing capabilities. Models of all sizes support a context length of 32768 tokens. 32 billion parameters."),
ModelTypeConst.LLM, ollama_llm_model_credential, OllamaChatModel),
ModelInfo(
'qwen:72b',
_("Compared with previous versions, qwen 1.5 72b has significantly enhanced the model's alignment with human preferences and its multi-language processing capabilities. Models of all sizes support a context length of 32768 tokens. 72 billion parameters."),
ModelTypeConst.LLM, ollama_llm_model_credential, OllamaChatModel),
ModelInfo(
'qwen:110b',
_("Compared with previous versions, qwen 1.5 110b has significantly enhanced the model's alignment with human preferences and its multi-language processing capabilities. Models of all sizes support a context length of 32768 tokens. 110 billion parameters."),
ModelTypeConst.LLM, ollama_llm_model_credential, OllamaChatModel),
ModelInfo(
'qwen2:72b-instruct',
'',
ModelTypeConst.LLM, ollama_llm_model_credential, OllamaChatModel),
ModelInfo(
'qwen2:57b-a14b-instruct',
'',
ModelTypeConst.LLM, ollama_llm_model_credential, OllamaChatModel),
ModelInfo(
'qwen2:7b-instruct',
'',
ModelTypeConst.LLM, ollama_llm_model_credential, OllamaChatModel),
ModelInfo(
'qwen2.5:72b-instruct',
'',
ModelTypeConst.LLM, ollama_llm_model_credential, OllamaChatModel),
ModelInfo(
'qwen2.5:32b-instruct',
'',
ModelTypeConst.LLM, ollama_llm_model_credential, OllamaChatModel),
ModelInfo(
'qwen2.5:14b-instruct',
'',
ModelTypeConst.LLM, ollama_llm_model_credential, OllamaChatModel),
ModelInfo(
'qwen2.5:7b-instruct',
'',
ModelTypeConst.LLM, ollama_llm_model_credential, OllamaChatModel),
ModelInfo(
'qwen2.5:1.5b-instruct',
'',
ModelTypeConst.LLM, ollama_llm_model_credential, OllamaChatModel),
ModelInfo(
'qwen2.5:0.5b-instruct',
'',
ModelTypeConst.LLM, ollama_llm_model_credential, OllamaChatModel),
ModelInfo(
'qwen2.5:3b-instruct',
'',
ModelTypeConst.LLM, ollama_llm_model_credential, OllamaChatModel),
ModelInfo(
'phi3',
_("Phi-3 Mini is Microsoft's 3.8B parameter, lightweight, state-of-the-art open model."),
ModelTypeConst.LLM, ollama_llm_model_credential, OllamaChatModel),
]
ollama_embedding_model_credential = OllamaEmbeddingModelCredential()
ollama_image_model_credential = OllamaImageModelCredential()
ollama_reranker_model_credential = OllamaReRankModelCredential()
embedding_model_info = [
ModelInfo(
'nomic-embed-text',
_('A high-performance open embedding model with a large token context window.'),
ModelTypeConst.EMBEDDING, ollama_embedding_model_credential, OllamaEmbedding),
]
reranker_model_info = [
ModelInfo(
'linux6200/bge-reranker-v2-m3',
'',
ModelTypeConst.RERANKER, ollama_reranker_model_credential, OllamaReranker),
]
image_model_info = [
ModelInfo(
'llava:7b',
'',
ModelTypeConst.IMAGE, ollama_image_model_credential, OllamaImage),
ModelInfo(
'llava:13b',
'',
ModelTypeConst.IMAGE, ollama_image_model_credential, OllamaImage),
ModelInfo(
'llava:34b',
'',
ModelTypeConst.IMAGE, ollama_image_model_credential, OllamaImage),
]
model_info_manage = (
ModelInfoManage.builder()
.append_model_info_list(model_info_list)
.append_model_info_list(embedding_model_info)
.append_default_model_info(ModelInfo(
'phi3',
_('Phi-3 Mini is Microsoft\'s 3.8B parameter, lightweight, state-of-the-art open model.'),
ModelTypeConst.LLM, ollama_llm_model_credential, OllamaChatModel))
.append_default_model_info(ModelInfo(
'nomic-embed-text',
_('A high-performance open embedding model with a large token context window.'),
ModelTypeConst.EMBEDDING, ollama_embedding_model_credential, OllamaEmbedding), )
.append_model_info_list(image_model_info)
.append_default_model_info(image_model_info[0])
.append_model_info_list(reranker_model_info)
.append_default_model_info(reranker_model_info[0])
.build()
)
def get_base_url(url: str):
parse = urlparse(url)
result_url = ParseResult(scheme=parse.scheme, netloc=parse.netloc, path=parse.path, params='',
query='',
fragment='').geturl()
return result_url[:-1] if result_url.endswith("/") else result_url
def convert_to_down_model_chunk(row_str: str, chunk_index: int):
row = json.loads(row_str)
status = DownModelChunkStatus.unknown
digest = ""
progress = 100
if 'status' in row:
digest = row.get('status')
if row.get('status') == 'success':
status = DownModelChunkStatus.success
if row.get('status').__contains__("pulling"):
progress = 0
status = DownModelChunkStatus.pulling
if 'total' in row and 'completed' in row and row.get('total'):
progress = (row.get('completed') / row.get('total') * 100)
elif 'error' in row:
status = DownModelChunkStatus.error
digest = row.get('error')
return DownModelChunk(status=status, digest=digest, progress=progress, details=row_str, index=chunk_index)
def convert(response_stream) -> Iterator[DownModelChunk]:
temp = ""
index = 0
for c in response_stream:
index += 1
row_content = c.decode()
temp += row_content
if row_content.endswith('}') or row_content.endswith('\n'):
rows = [t for t in temp.split("\n") if len(t) > 0]
for row in rows:
yield convert_to_down_model_chunk(row, index)
temp = ""
if len(temp) > 0:
rows = [t for t in temp.split("\n") if len(t) > 0]
for row in rows:
yield convert_to_down_model_chunk(row, index)
class OllamaModelProvider(IModelProvider):
def get_model_info_manage(self):
return model_info_manage
def get_model_provide_info(self):
return ModelProvideInfo(provider='model_ollama_provider', name='Ollama', icon=get_file_content(
os.path.join(PROJECT_DIR, "apps", 'models_provider', 'impl', 'ollama_model_provider', 'icon',
'ollama_icon_svg')))
@staticmethod
def get_base_model_list(api_base):
base_url = get_base_url(api_base)
r = requests.request(method="GET", url=f"{base_url}/api/tags", timeout=5)
r.raise_for_status()
return r.json()
def down_model(self, model_type: str, model_name, model_credential: Dict[str, object]) -> Iterator[DownModelChunk]:
api_base = model_credential.get('api_base', '')
base_url = get_base_url(api_base)
r = requests.request(
method="POST",
url=f"{base_url}/api/pull",
data=json.dumps({"name": model_name}).encode(),
stream=True,
)
return convert(r)