-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
134 additions
and
1 deletion.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
apps/setting/models_provider/impl/openai_model_provider/credential/image.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# coding=utf-8 | ||
from typing import Dict | ||
|
||
from common import forms | ||
from common.exception.app_exception import AppApiException | ||
from common.forms import BaseForm | ||
from setting.models_provider.base_model_provider import BaseModelCredential, ValidCode | ||
|
||
|
||
class OpenAIImageModelCredential(BaseForm, BaseModelCredential): | ||
api_base = forms.TextInputField('API 域名', required=True) | ||
api_key = forms.PasswordInputField('API Key', required=True) | ||
|
||
def is_valid(self, model_type: str, model_name, model_credential: Dict[str, object], provider, | ||
raise_exception=False): | ||
model_type_list = provider.get_model_type_list() | ||
if not any(list(filter(lambda mt: mt.get('value') == model_type, model_type_list))): | ||
raise AppApiException(ValidCode.valid_error.value, f'{model_type} 模型类型不支持') | ||
|
||
for key in ['api_base', 'api_key']: | ||
if key not in model_credential: | ||
if raise_exception: | ||
raise AppApiException(ValidCode.valid_error.value, f'{key} 字段为必填字段') | ||
else: | ||
return False | ||
try: | ||
model = provider.get_model(model_type, model_name, model_credential) | ||
model.check_auth() | ||
except Exception as e: | ||
if isinstance(e, AppApiException): | ||
raise e | ||
if raise_exception: | ||
raise AppApiException(ValidCode.valid_error.value, f'校验失败,请检查参数是否正确: {str(e)}') | ||
else: | ||
return False | ||
return True | ||
|
||
def encryption_dict(self, model: Dict[str, object]): | ||
return {**model, 'api_key': super().encryption(model.get('api_key', ''))} | ||
|
||
def get_model_params_setting_form(self, model_name): | ||
pass |
79 changes: 79 additions & 0 deletions
79
apps/setting/models_provider/impl/openai_model_provider/model/image.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import base64 | ||
import os | ||
from typing import Dict | ||
|
||
from openai import OpenAI | ||
|
||
from common.config.tokenizer_manage_config import TokenizerManage | ||
from setting.models_provider.base_model_provider import MaxKBBaseModel | ||
from setting.models_provider.impl.base_image import BaseImage | ||
|
||
|
||
def custom_get_token_ids(text: str): | ||
tokenizer = TokenizerManage.get_tokenizer() | ||
return tokenizer.encode(text) | ||
|
||
|
||
class OpenAIImage(MaxKBBaseModel, BaseImage): | ||
api_base: str | ||
api_key: str | ||
model: str | ||
|
||
def __init__(self, **kwargs): | ||
super().__init__(**kwargs) | ||
self.api_key = kwargs.get('api_key') | ||
self.api_base = kwargs.get('api_base') | ||
|
||
@staticmethod | ||
def new_instance(model_type, model_name, model_credential: Dict[str, object], **model_kwargs): | ||
optional_params = {} | ||
if 'max_tokens' in model_kwargs and model_kwargs['max_tokens'] is not None: | ||
optional_params['max_tokens'] = model_kwargs['max_tokens'] | ||
if 'temperature' in model_kwargs and model_kwargs['temperature'] is not None: | ||
optional_params['temperature'] = model_kwargs['temperature'] | ||
return OpenAIImage( | ||
model=model_name, | ||
api_base=model_credential.get('api_base'), | ||
api_key=model_credential.get('api_key'), | ||
**optional_params, | ||
) | ||
|
||
def check_auth(self): | ||
client = OpenAI( | ||
base_url=self.api_base, | ||
api_key=self.api_key | ||
) | ||
response_list = client.models.with_raw_response.list() | ||
# print(response_list) | ||
# cwd = os.path.dirname(os.path.abspath(__file__)) | ||
# with open(f'{cwd}/img_1.png', 'rb') as f: | ||
# self.image_understand(f, "一句话概述这个图片") | ||
|
||
def image_understand(self, image_file, text): | ||
client = OpenAI( | ||
base_url=self.api_base, | ||
api_key=self.api_key | ||
) | ||
base64_image = base64.b64encode(image_file.read()).decode('utf-8') | ||
|
||
response = client.chat.completions.create( | ||
model=self.model, | ||
messages=[ | ||
{ | ||
"role": "user", | ||
"content": [ | ||
{ | ||
"type": "text", | ||
"text": text, | ||
}, | ||
{ | ||
"type": "image_url", | ||
"image_url": { | ||
"url": f"data:image/jpeg;base64,{base64_image}" | ||
}, | ||
}, | ||
], | ||
} | ||
], | ||
) | ||
return response.choices[0].message.content |
Binary file added
BIN
+355 KB
apps/setting/models_provider/impl/openai_model_provider/model/img_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters