Skip to content

Commit ff41b1f

Browse files
committed
refactor: tokens
1 parent 3d1c43c commit ff41b1f

File tree

10 files changed

+27
-12
lines changed
  • apps/models_provider/impl
    • aliyun_bai_lian_model_provider/model
    • aws_bedrock_model_provider/model
    • deepseek_model_provider/model
    • kimi_model_provider/model
    • ollama_model_provider/credential
    • openai_model_provider/model
    • siliconCloud_model_provider/model
    • vllm_model_provider/model
    • volcanic_engine_model_provider/model
    • xinference_model_provider/model

10 files changed

+27
-12
lines changed

apps/models_provider/impl/aliyun_bai_lian_model_provider/model/llm.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,5 @@ def new_instance(model_type, model_name, model_credential: Dict[str, object], **
2020
model=model_name,
2121
openai_api_base=model_credential.get('api_base'),
2222
openai_api_key=model_credential.get('api_key'),
23-
**optional_params
23+
extra_body=optional_params
2424
)

apps/models_provider/impl/aws_bedrock_model_provider/model/llm.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import os
22
import re
3-
from typing import Dict
3+
from typing import Dict, List
44

55
from botocore.config import Config
66
from langchain_community.chat_models import BedrockChat
7+
from langchain_core.messages import BaseMessage, get_buffer_string
78

9+
from common.config.tokenizer_manage_config import TokenizerManage
810
from models_provider.base_model_provider import MaxKBBaseModel
911

1012

@@ -72,6 +74,19 @@ def new_instance(cls, model_type: str, model_name: str, model_credential: Dict[s
7274
config=config
7375
)
7476

77+
def get_num_tokens_from_messages(self, messages: List[BaseMessage]) -> int:
78+
try:
79+
return super().get_num_tokens_from_messages(messages)
80+
except Exception as e:
81+
tokenizer = TokenizerManage.get_tokenizer()
82+
return sum([len(tokenizer.encode(get_buffer_string([m]))) for m in messages])
83+
84+
def get_num_tokens(self, text: str) -> int:
85+
try:
86+
return super().get_num_tokens(text)
87+
except Exception as e:
88+
tokenizer = TokenizerManage.get_tokenizer()
89+
return len(tokenizer.encode(text))
7590

7691
def _update_aws_credentials(profile_name, access_key_id, secret_access_key):
7792
credentials_path = os.path.join(os.path.expanduser("~"), ".aws", "credentials")

apps/models_provider/impl/deepseek_model_provider/model/llm.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,6 @@ def new_instance(model_type, model_name, model_credential: Dict[str, object], **
2626
model=model_name,
2727
openai_api_base='https://api.deepseek.com',
2828
openai_api_key=model_credential.get('api_key'),
29-
**optional_params
29+
extra_body=optional_params
3030
)
3131
return deepseek_chat_open_ai

apps/models_provider/impl/kimi_model_provider/model/llm.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,6 @@ def new_instance(model_type, model_name, model_credential: Dict[str, object], **
2626
openai_api_base=model_credential['api_base'],
2727
openai_api_key=model_credential['api_key'],
2828
model_name=model_name,
29-
**optional_params
29+
extra_body=optional_params,
3030
)
3131
return kimi_chat_open_ai

apps/models_provider/impl/ollama_model_provider/credential/llm.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class OllamaLLMModelParams(BaseForm):
2525
_step=0.01,
2626
precision=2)
2727

28-
max_tokens = forms.SliderField(
28+
num_predict = forms.SliderField(
2929
TooltipLabel(_('Output the maximum Tokens'),
3030
_('Specify the maximum number of tokens that the model can generate')),
3131
required=True, default_value=1024,

apps/models_provider/impl/openai_model_provider/model/llm.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,15 @@ def new_instance(model_type, model_name, model_credential: Dict[str, object], **
3333
streaming = model_kwargs.get('streaming', True)
3434
if 'o1' in model_name:
3535
streaming = False
36-
azure_chat_open_ai = OpenAIChatModel(
36+
chat_open_ai = OpenAIChatModel(
3737
model=model_name,
3838
openai_api_base=model_credential.get('api_base'),
3939
openai_api_key=model_credential.get('api_key'),
40-
**optional_params,
40+
extra_body=optional_params,
4141
streaming=streaming,
4242
custom_get_token_ids=custom_get_token_ids
4343
)
44-
return azure_chat_open_ai
44+
return chat_open_ai
4545

4646
def get_num_tokens_from_messages(self, messages: List[BaseMessage]) -> int:
4747
try:

apps/models_provider/impl/siliconCloud_model_provider/model/llm.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,5 @@ def new_instance(model_type, model_name, model_credential: Dict[str, object], **
3434
model=model_name,
3535
openai_api_base=model_credential.get('api_base'),
3636
openai_api_key=model_credential.get('api_key'),
37-
**optional_params
37+
extra_body=optional_params
3838
)

apps/models_provider/impl/vllm_model_provider/model/llm.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def new_instance(model_type, model_name, model_credential: Dict[str, object], **
3131
model=model_name,
3232
openai_api_base=model_credential.get('api_base'),
3333
openai_api_key=model_credential.get('api_key'),
34-
**optional_params,
34+
extra_body=optional_params,
3535
streaming=True,
3636
stream_usage=True,
3737
)

apps/models_provider/impl/volcanic_engine_model_provider/model/llm.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,5 @@ def new_instance(model_type, model_name, model_credential: Dict[str, object], **
1717
model=model_name,
1818
openai_api_base=model_credential.get('api_base'),
1919
openai_api_key=model_credential.get('api_key'),
20-
**optional_params
20+
extra_body=optional_params
2121
)

apps/models_provider/impl/xinference_model_provider/model/llm.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def new_instance(model_type, model_name, model_credential: Dict[str, object], **
3434
model=model_name,
3535
openai_api_base=base_url,
3636
openai_api_key=model_credential.get('api_key'),
37-
**optional_params
37+
extra_body=optional_params
3838
)
3939

4040
def get_num_tokens_from_messages(self, messages: List[BaseMessage]) -> int:

0 commit comments

Comments
 (0)