Skip to content

Commit 0cf05a7

Browse files
authored
feat(provider): added regolo.ai (#3041)
1 parent 357edbf commit 0cf05a7

File tree

12 files changed

+596
-0
lines changed

12 files changed

+596
-0
lines changed

apps/setting/models_provider/constants/model_provider_constants.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
from setting.models_provider.impl.ollama_model_provider.ollama_model_provider import OllamaModelProvider
2020
from setting.models_provider.impl.openai_model_provider.openai_model_provider import OpenAIModelProvider
2121
from setting.models_provider.impl.qwen_model_provider.qwen_model_provider import QwenModelProvider
22+
from setting.models_provider.impl.regolo_model_provider.regolo_model_provider import \
23+
RegoloModelProvider
2224
from setting.models_provider.impl.siliconCloud_model_provider.siliconCloud_model_provider import \
2325
SiliconCloudModelProvider
2426
from setting.models_provider.impl.tencent_cloud_model_provider.tencent_cloud_model_provider import \
@@ -55,3 +57,4 @@ class ModelProvideConstants(Enum):
5557
aliyun_bai_lian_model_provider = AliyunBaiLianModelProvider()
5658
model_anthropic_provider = AnthropicModelProvider()
5759
model_siliconCloud_provider = SiliconCloudModelProvider()
60+
model_regolo_provider = RegoloModelProvider()
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# coding=utf-8
2+
"""
3+
@project: maxkb
4+
@Author:虎
5+
@file: __init__.py.py
6+
@date:2024/3/28 16:25
7+
@desc:
8+
"""
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# coding=utf-8
2+
"""
3+
@project: MaxKB
4+
@Author:虎
5+
@file: embedding.py
6+
@date:2024/7/12 16:45
7+
@desc:
8+
"""
9+
import traceback
10+
from typing import Dict
11+
12+
from django.utils.translation import gettext as _
13+
14+
from common import forms
15+
from common.exception.app_exception import AppApiException
16+
from common.forms import BaseForm
17+
from setting.models_provider.base_model_provider import BaseModelCredential, ValidCode
18+
19+
20+
class RegoloEmbeddingCredential(BaseForm, BaseModelCredential):
21+
def is_valid(self, model_type: str, model_name, model_credential: Dict[str, object], model_params, provider,
22+
raise_exception=True):
23+
model_type_list = provider.get_model_type_list()
24+
if not any(list(filter(lambda mt: mt.get('value') == model_type, model_type_list))):
25+
raise AppApiException(ValidCode.valid_error.value,
26+
_('{model_type} Model type is not supported').format(model_type=model_type))
27+
28+
for key in ['api_key']:
29+
if key not in model_credential:
30+
if raise_exception:
31+
raise AppApiException(ValidCode.valid_error.value, _('{key} is required').format(key=key))
32+
else:
33+
return False
34+
try:
35+
model = provider.get_model(model_type, model_name, model_credential)
36+
model.embed_query(_('Hello'))
37+
except Exception as e:
38+
traceback.print_exc()
39+
if isinstance(e, AppApiException):
40+
raise e
41+
if raise_exception:
42+
raise AppApiException(ValidCode.valid_error.value,
43+
_('Verification failed, please check whether the parameters are correct: {error}').format(
44+
error=str(e)))
45+
else:
46+
return False
47+
return True
48+
49+
def encryption_dict(self, model: Dict[str, object]):
50+
return {**model, 'api_key': super().encryption(model.get('api_key', ''))}
51+
52+
api_key = forms.PasswordInputField('API Key', required=True)
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# coding=utf-8
2+
import base64
3+
import os
4+
import traceback
5+
from typing import Dict
6+
7+
from langchain_core.messages import HumanMessage
8+
9+
from common import forms
10+
from common.exception.app_exception import AppApiException
11+
from common.forms import BaseForm, TooltipLabel
12+
from setting.models_provider.base_model_provider import BaseModelCredential, ValidCode
13+
from django.utils.translation import gettext_lazy as _, gettext
14+
15+
16+
class RegoloImageModelParams(BaseForm):
17+
temperature = forms.SliderField(TooltipLabel(_('Temperature'),
18+
_('Higher values make the output more random, while lower values make it more focused and deterministic')),
19+
required=True, default_value=0.7,
20+
_min=0.1,
21+
_max=1.0,
22+
_step=0.01,
23+
precision=2)
24+
25+
max_tokens = forms.SliderField(
26+
TooltipLabel(_('Output the maximum Tokens'),
27+
_('Specify the maximum number of tokens that the model can generate')),
28+
required=True, default_value=800,
29+
_min=1,
30+
_max=100000,
31+
_step=1,
32+
precision=0)
33+
34+
35+
class RegoloImageModelCredential(BaseForm, BaseModelCredential):
36+
api_base = forms.TextInputField('API URL', required=True)
37+
api_key = forms.PasswordInputField('API Key', required=True)
38+
39+
def is_valid(self, model_type: str, model_name, model_credential: Dict[str, object], model_params, provider,
40+
raise_exception=False):
41+
model_type_list = provider.get_model_type_list()
42+
if not any(list(filter(lambda mt: mt.get('value') == model_type, model_type_list))):
43+
raise AppApiException(ValidCode.valid_error.value,
44+
gettext('{model_type} Model type is not supported').format(model_type=model_type))
45+
46+
for key in ['api_key']:
47+
if key not in model_credential:
48+
if raise_exception:
49+
raise AppApiException(ValidCode.valid_error.value, gettext('{key} is required').format(key=key))
50+
else:
51+
return False
52+
try:
53+
model = provider.get_model(model_type, model_name, model_credential, **model_params)
54+
res = model.stream([HumanMessage(content=[{"type": "text", "text": gettext('Hello')}])])
55+
for chunk in res:
56+
print(chunk)
57+
except Exception as e:
58+
traceback.print_exc()
59+
if isinstance(e, AppApiException):
60+
raise e
61+
if raise_exception:
62+
raise AppApiException(ValidCode.valid_error.value,
63+
gettext(
64+
'Verification failed, please check whether the parameters are correct: {error}').format(
65+
error=str(e)))
66+
else:
67+
return False
68+
return True
69+
70+
def encryption_dict(self, model: Dict[str, object]):
71+
return {**model, 'api_key': super().encryption(model.get('api_key', ''))}
72+
73+
def get_model_params_setting_form(self, model_name):
74+
return RegoloImageModelParams()
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# coding=utf-8
2+
"""
3+
@project: MaxKB
4+
@Author:虎
5+
@file: llm.py
6+
@date:2024/7/11 18:32
7+
@desc:
8+
"""
9+
import traceback
10+
from typing import Dict
11+
12+
from django.utils.translation import gettext_lazy as _, gettext
13+
from langchain_core.messages import HumanMessage
14+
15+
from common import forms
16+
from common.exception.app_exception import AppApiException
17+
from common.forms import BaseForm, TooltipLabel
18+
from setting.models_provider.base_model_provider import BaseModelCredential, ValidCode
19+
20+
21+
class RegoloLLMModelParams(BaseForm):
22+
temperature = forms.SliderField(TooltipLabel(_('Temperature'),
23+
_('Higher values make the output more random, while lower values make it more focused and deterministic')),
24+
required=True, default_value=0.7,
25+
_min=0.1,
26+
_max=1.0,
27+
_step=0.01,
28+
precision=2)
29+
30+
max_tokens = forms.SliderField(
31+
TooltipLabel(_('Output the maximum Tokens'),
32+
_('Specify the maximum number of tokens that the model can generate')),
33+
required=True, default_value=800,
34+
_min=1,
35+
_max=100000,
36+
_step=1,
37+
precision=0)
38+
39+
40+
class RegoloLLMModelCredential(BaseForm, BaseModelCredential):
41+
42+
def is_valid(self, model_type: str, model_name, model_credential: Dict[str, object], model_params, provider,
43+
raise_exception=False):
44+
model_type_list = provider.get_model_type_list()
45+
if not any(list(filter(lambda mt: mt.get('value') == model_type, model_type_list))):
46+
raise AppApiException(ValidCode.valid_error.value,
47+
gettext('{model_type} Model type is not supported').format(model_type=model_type))
48+
49+
for key in ['api_key']:
50+
if key not in model_credential:
51+
if raise_exception:
52+
raise AppApiException(ValidCode.valid_error.value, gettext('{key} is required').format(key=key))
53+
else:
54+
return False
55+
try:
56+
57+
model = provider.get_model(model_type, model_name, model_credential, **model_params)
58+
model.invoke([HumanMessage(content=gettext('Hello'))])
59+
except Exception as e:
60+
traceback.print_exc()
61+
if isinstance(e, AppApiException):
62+
raise e
63+
if raise_exception:
64+
raise AppApiException(ValidCode.valid_error.value,
65+
gettext(
66+
'Verification failed, please check whether the parameters are correct: {error}').format(
67+
error=str(e)))
68+
else:
69+
return False
70+
return True
71+
72+
def encryption_dict(self, model: Dict[str, object]):
73+
return {**model, 'api_key': super().encryption(model.get('api_key', ''))}
74+
75+
api_key = forms.PasswordInputField('API Key', required=True)
76+
77+
def get_model_params_setting_form(self, model_name):
78+
return RegoloLLMModelParams()
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# coding=utf-8
2+
import traceback
3+
from typing import Dict
4+
5+
from django.utils.translation import gettext_lazy as _, gettext
6+
7+
from common import forms
8+
from common.exception.app_exception import AppApiException
9+
from common.forms import BaseForm, TooltipLabel
10+
from setting.models_provider.base_model_provider import BaseModelCredential, ValidCode
11+
12+
13+
class RegoloTTIModelParams(BaseForm):
14+
size = forms.SingleSelect(
15+
TooltipLabel(_('Image size'),
16+
_('The image generation endpoint allows you to create raw images based on text prompts. ')),
17+
required=True,
18+
default_value='1024x1024',
19+
option_list=[
20+
{'value': '1024x1024', 'label': '1024x1024'},
21+
{'value': '1024x1792', 'label': '1024x1792'},
22+
{'value': '1792x1024', 'label': '1792x1024'},
23+
],
24+
text_field='label',
25+
value_field='value'
26+
)
27+
28+
quality = forms.SingleSelect(
29+
TooltipLabel(_('Picture quality'), _('''
30+
By default, images are produced in standard quality.
31+
''')),
32+
required=True,
33+
default_value='standard',
34+
option_list=[
35+
{'value': 'standard', 'label': 'standard'},
36+
{'value': 'hd', 'label': 'hd'},
37+
],
38+
text_field='label',
39+
value_field='value'
40+
)
41+
42+
n = forms.SliderField(
43+
TooltipLabel(_('Number of pictures'),
44+
_('1 as default')),
45+
required=True, default_value=1,
46+
_min=1,
47+
_max=10,
48+
_step=1,
49+
precision=0)
50+
51+
52+
class RegoloTextToImageModelCredential(BaseForm, BaseModelCredential):
53+
api_key = forms.PasswordInputField('API Key', required=True)
54+
55+
def is_valid(self, model_type: str, model_name, model_credential: Dict[str, object], model_params, provider,
56+
raise_exception=False):
57+
model_type_list = provider.get_model_type_list()
58+
if not any(list(filter(lambda mt: mt.get('value') == model_type, model_type_list))):
59+
raise AppApiException(ValidCode.valid_error.value,
60+
gettext('{model_type} Model type is not supported').format(model_type=model_type))
61+
62+
for key in ['api_key']:
63+
if key not in model_credential:
64+
if raise_exception:
65+
raise AppApiException(ValidCode.valid_error.value, gettext('{key} is required').format(key=key))
66+
else:
67+
return False
68+
try:
69+
model = provider.get_model(model_type, model_name, model_credential, **model_params)
70+
res = model.check_auth()
71+
print(res)
72+
except Exception as e:
73+
traceback.print_exc()
74+
if isinstance(e, AppApiException):
75+
raise e
76+
if raise_exception:
77+
raise AppApiException(ValidCode.valid_error.value,
78+
gettext(
79+
'Verification failed, please check whether the parameters are correct: {error}').format(
80+
error=str(e)))
81+
else:
82+
return False
83+
return True
84+
85+
def encryption_dict(self, model: Dict[str, object]):
86+
return {**model, 'api_key': super().encryption(model.get('api_key', ''))}
87+
88+
def get_model_params_setting_form(self, model_name):
89+
return RegoloTTIModelParams()
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2+
<svg
3+
id="Livello_2"
4+
data-name="Livello 2"
5+
viewBox="0 0 104.4 104.38"
6+
version="1.1"
7+
sodipodi:docname="Regolo_logo_positive.svg"
8+
width="104.4"
9+
height="104.38"
10+
inkscape:version="1.4 (e7c3feb100, 2024-10-09)"
11+
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
12+
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
13+
xmlns="http://www.w3.org/2000/svg"
14+
xmlns:svg="http://www.w3.org/2000/svg">
15+
<sodipodi:namedview
16+
id="namedview13"
17+
pagecolor="#ffffff"
18+
bordercolor="#666666"
19+
borderopacity="1.0"
20+
inkscape:showpageshadow="2"
21+
inkscape:pageopacity="0.0"
22+
inkscape:pagecheckerboard="0"
23+
inkscape:deskcolor="#d1d1d1"
24+
inkscape:zoom="2.1335227"
25+
inkscape:cx="119.05193"
26+
inkscape:cy="48.511318"
27+
inkscape:window-width="1920"
28+
inkscape:window-height="1025"
29+
inkscape:window-x="0"
30+
inkscape:window-y="0"
31+
inkscape:window-maximized="1"
32+
inkscape:current-layer="g13" />
33+
<defs
34+
id="defs1">
35+
<style
36+
id="style1">
37+
.cls-1 {
38+
fill: #303030;
39+
}
40+
41+
.cls-2 {
42+
fill: #59e389;
43+
}
44+
</style>
45+
</defs>
46+
<g
47+
id="Grafica"
48+
transform="translate(0,-40.87)">
49+
<g
50+
id="g13">
51+
<path
52+
class="cls-1"
53+
d="m 104.39,105.96 v 36.18 c 0,0.32 -0.05,0.62 -0.14,0.91 -0.39,1.27 -1.58,2.2 -2.99,2.2 H 65.08 c -1.73,0 -3.13,-1.41 -3.13,-3.13 V 113.4 c 0,-0.15 0,-0.29 0,-0.44 v -7 c 0,-1.73 1.4,-3.13 3.13,-3.13 h 36.19 c 1.5,0 2.77,1.07 3.06,2.5 0.05,0.21 0.07,0.41 0.07,0.63 z"
54+
id="path1" />
55+
<path
56+
class="cls-1"
57+
d="m 104.39,105.96 v 36.18 c 0,0.32 -0.05,0.62 -0.14,0.91 -0.39,1.27 -1.58,2.2 -2.99,2.2 H 65.08 c -1.73,0 -3.13,-1.41 -3.13,-3.13 V 113.4 c 0,-0.15 0,-0.29 0,-0.44 v -7 c 0,-1.73 1.4,-3.13 3.13,-3.13 h 36.19 c 1.5,0 2.77,1.07 3.06,2.5 0.05,0.21 0.07,0.41 0.07,0.63 z"
58+
id="path2" />
59+
<path
60+
class="cls-2"
61+
d="M 101.27,40.88 H 65.09 c -1.73,0 -3.13,1.4 -3.13,3.13 v 28.71 c 0,4.71 -1.88,9.23 -5.2,12.56 L 44.42,97.61 c -3.32,3.33 -7.85,5.2 -12.55,5.2 H 18.98 c -2.21,0 -3.99,-1.79 -3.99,-3.99 V 87.29 c 0,-2.21 1.79,-3.99 3.99,-3.99 h 20.34 c 1.41,0 2.59,-0.93 2.99,-2.2 0.09,-0.29 0.14,-0.59 0.14,-0.91 V 44 c 0,-0.22 -0.02,-0.42 -0.07,-0.63 -0.29,-1.43 -1.56,-2.5 -3.06,-2.5 H 3.13 C 1.4,40.87 0,42.27 0,44 v 7 c 0,0.15 0,0.29 0,0.44 v 28.72 c 0,1.72 1.41,3.13 3.13,3.13 h 3.16 c 2.21,0 3.99,1.79 3.99,3.99 v 11.53 c 0,2.21 -1.79,3.99 -3.99,3.99 H 3.15 c -1.73,0 -3.13,1.4 -3.13,3.13 v 36.19 c 0,1.72 1.41,3.13 3.13,3.13 h 36.19 c 1.73,0 3.13,-1.41 3.13,-3.13 V 113.4 c 0,-4.7 1.87,-9.23 5.2,-12.55 L 60,88.51 c 3.33,-3.32 7.85,-5.2 12.56,-5.2 h 28.71 c 1.73,0 3.13,-1.4 3.13,-3.13 V 44 c 0,-1.73 -1.4,-3.13 -3.13,-3.13 z"
62+
id="path3" />
63+
</g>
64+
</g>
65+
</svg>

0 commit comments

Comments
 (0)