Skip to content

Commit f878a2f

Browse files
committed
feat: Support vllm image model
1 parent 82f2d7e commit f878a2f

File tree

3 files changed

+106
-6
lines changed

3 files changed

+106
-6
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# coding=utf-8
2+
import base64
3+
import os
4+
from typing import Dict
5+
6+
from langchain_core.messages import HumanMessage
7+
8+
from common import forms
9+
from common.exception.app_exception import AppApiException
10+
from common.forms import BaseForm, TooltipLabel
11+
from setting.models_provider.base_model_provider import BaseModelCredential, ValidCode
12+
from django.utils.translation import gettext_lazy as _
13+
14+
class VllmImageModelParams(BaseForm):
15+
temperature = forms.SliderField(TooltipLabel(_('Temperature'),
16+
_('Higher values make the output more random, while lower values make it more focused and deterministic')),
17+
required=True, default_value=0.7,
18+
_min=0.1,
19+
_max=1.0,
20+
_step=0.01,
21+
precision=2)
22+
23+
max_tokens = forms.SliderField(
24+
TooltipLabel(_('Output the maximum Tokens'),
25+
_('Specify the maximum number of tokens that the model can generate')),
26+
required=True, default_value=800,
27+
_min=1,
28+
_max=100000,
29+
_step=1,
30+
precision=0)
31+
32+
33+
34+
class VllmImageModelCredential(BaseForm, BaseModelCredential):
35+
api_base = forms.TextInputField('API Url', required=True)
36+
api_key = forms.PasswordInputField('API Key', required=True)
37+
38+
def is_valid(self, model_type: str, model_name, model_credential: Dict[str, object], model_params, provider,
39+
raise_exception=False):
40+
model_type_list = provider.get_model_type_list()
41+
if not any(list(filter(lambda mt: mt.get('value') == model_type, model_type_list))):
42+
raise AppApiException(ValidCode.valid_error.value, _('{model_type} Model type is not supported').format(model_type=model_type))
43+
44+
for key in ['api_base', 'api_key']:
45+
if key not in model_credential:
46+
if raise_exception:
47+
raise AppApiException(ValidCode.valid_error.value, _('{key} is required').format(key=key))
48+
else:
49+
return False
50+
try:
51+
model = provider.get_model(model_type, model_name, model_credential, **model_params)
52+
res = model.stream([HumanMessage(content=[{"type": "text", "text": "你好"}])])
53+
for chunk in res:
54+
print(chunk)
55+
except Exception as e:
56+
if isinstance(e, AppApiException):
57+
raise e
58+
if raise_exception:
59+
raise AppApiException(ValidCode.valid_error.value, _('Verification failed, please check whether the parameters are correct: {error}').format(error=str(e)))
60+
else:
61+
return False
62+
return True
63+
64+
def encryption_dict(self, model: Dict[str, object]):
65+
return {**model, 'api_key': super().encryption(model.get('api_key', ''))}
66+
67+
def get_model_params_setting_form(self, model_name):
68+
return VllmImageModelParams()
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from typing import Dict
2+
3+
from setting.models_provider.base_model_provider import MaxKBBaseModel
4+
from setting.models_provider.impl.base_chat_open_ai import BaseChatOpenAI
5+
6+
7+
class VllmImage(MaxKBBaseModel, BaseChatOpenAI):
8+
9+
@staticmethod
10+
def new_instance(model_type, model_name, model_credential: Dict[str, object], **model_kwargs):
11+
optional_params = MaxKBBaseModel.filter_optional_params(model_kwargs)
12+
return VllmImage(
13+
model_name=model_name,
14+
openai_api_base=model_credential.get('api_base'),
15+
openai_api_key=model_credential.get('api_key'),
16+
# stream_options={"include_usage": True},
17+
streaming=True,
18+
stream_usage=True,
19+
**optional_params,
20+
)

apps/setting/models_provider/impl/vllm_model_provider/vllm_model_provider.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,37 @@
77
from common.util.file_util import get_file_content
88
from setting.models_provider.base_model_provider import IModelProvider, ModelProvideInfo, ModelInfo, ModelTypeConst, \
99
ModelInfoManage
10+
from setting.models_provider.impl.vllm_model_provider.credential.image import VllmImageModelCredential
1011
from setting.models_provider.impl.vllm_model_provider.credential.llm import VLLMModelCredential
12+
from setting.models_provider.impl.vllm_model_provider.model.image import VllmImage
1113
from setting.models_provider.impl.vllm_model_provider.model.llm import VllmChatModel
1214
from smartdoc.conf import PROJECT_DIR
1315
from django.utils.translation import gettext_lazy as _
1416

1517
v_llm_model_credential = VLLMModelCredential()
18+
image_model_credential = VllmImageModelCredential()
19+
1620
model_info_list = [
1721
ModelInfo('facebook/opt-125m', _('Facebook’s 125M parameter model'), ModelTypeConst.LLM, v_llm_model_credential, VllmChatModel),
1822
ModelInfo('BAAI/Aquila-7B', _('BAAI’s 7B parameter model'), ModelTypeConst.LLM, v_llm_model_credential, VllmChatModel),
1923
ModelInfo('BAAI/AquilaChat-7B', _('BAAI’s 13B parameter mode'), ModelTypeConst.LLM, v_llm_model_credential, VllmChatModel),
2024

2125
]
2226

23-
model_info_manage = (ModelInfoManage.builder().append_model_info_list(model_info_list).append_default_model_info(
24-
ModelInfo(
25-
'facebook/opt-125m',
26-
_('Facebook’s 125M parameter model'),
27-
ModelTypeConst.LLM, v_llm_model_credential, VllmChatModel))
28-
.build())
27+
image_model_info_list = [
28+
ModelInfo('Qwen/Qwen2-VL-2B-Instruct', '', ModelTypeConst.IMAGE, image_model_credential, VllmImage),
29+
]
30+
31+
model_info_manage = (
32+
ModelInfoManage.builder()
33+
.append_model_info_list(model_info_list)
34+
.append_default_model_info(ModelInfo('facebook/opt-125m',
35+
_('Facebook’s 125M parameter model'),
36+
ModelTypeConst.LLM, v_llm_model_credential, VllmChatModel))
37+
.append_model_info_list(image_model_info_list)
38+
.append_default_model_info(image_model_info_list[0])
39+
.build()
40+
)
2941

3042

3143
def get_base_url(url: str):

0 commit comments

Comments
 (0)