-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
11 changed files
with
631 additions
and
5 deletions.
There are no files selected for viewing
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
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,8 @@ | ||
# coding=utf-8 | ||
from abc import abstractmethod | ||
|
||
|
||
class BaseSpeechToText: | ||
@abstractmethod | ||
def speech_to_text(self, audio_file): | ||
pass |
38 changes: 38 additions & 0 deletions
38
apps/setting/models_provider/impl/openai_model_provider/model/stt.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,38 @@ | ||
from typing import Dict | ||
|
||
from langchain_openai.chat_models import ChatOpenAI | ||
|
||
from common.config.tokenizer_manage_config import TokenizerManage | ||
from setting.models_provider.base_model_provider import MaxKBBaseModel | ||
from setting.models_provider.impl.base_stt import BaseSpeechToText | ||
|
||
|
||
def custom_get_token_ids(text: str): | ||
tokenizer = TokenizerManage.get_tokenizer() | ||
return tokenizer.encode(text) | ||
|
||
|
||
class OpenAISpeechToText(MaxKBBaseModel, ChatOpenAI, BaseSpeechToText): | ||
@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 OpenAISpeechToText( | ||
model=model_name, | ||
openai_api_base=model_credential.get('api_base'), | ||
openai_api_key=model_credential.get('api_key'), | ||
**optional_params, | ||
streaming=True, | ||
stream_usage=True, | ||
custom_get_token_ids=custom_get_token_ids | ||
) | ||
|
||
def speech_to_text(self, audio_file): | ||
return self.client.audio.transcriptions.create( | ||
model=self.model, | ||
language="zh", | ||
file=audio_file | ||
) |
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
45 changes: 45 additions & 0 deletions
45
apps/setting/models_provider/impl/volcanic_engine_model_provider/credential/stt.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,45 @@ | ||
# 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 VolcanicEngineSTTModelCredential(BaseForm, BaseModelCredential): | ||
volcanic_api_url = forms.TextInputField('API 域名', required=True) | ||
volcanic_app_id = forms.TextInputField('App ID', required=True) | ||
volcanic_token = forms.PasswordInputField('Token', required=True) | ||
volcanic_cluster = forms.TextInputField('Cluster', 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 ['volcanic_api_url', 'volcanic_app_id', 'volcanic_token', 'volcanic_cluster']: | ||
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) | ||
print(model) | ||
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, 'volcanic_token': super().encryption(model.get('volcanic_token', ''))} | ||
|
||
def get_model_params_setting_form(self, model_name): | ||
pass |
Oops, something went wrong.