diff --git a/api/core/model_runtime/model_providers/openai/llm/_position.yaml b/api/core/model_runtime/model_providers/openai/llm/_position.yaml index 0d3143c2ae..688d4dbd09 100644 --- a/api/core/model_runtime/model_providers/openai/llm/_position.yaml +++ b/api/core/model_runtime/model_providers/openai/llm/_position.yaml @@ -1,3 +1,4 @@ +- gpt-4.1 - o1 - o1-2024-12-17 - o1-mini diff --git a/api/core/model_runtime/model_providers/openai/llm/gpt-4-1.yaml b/api/core/model_runtime/model_providers/openai/llm/gpt-4-1.yaml new file mode 100644 index 0000000000..f9f6edb4e6 --- /dev/null +++ b/api/core/model_runtime/model_providers/openai/llm/gpt-4-1.yaml @@ -0,0 +1,60 @@ +model: gpt-4.1 +label: + zh_Hans: gpt-4.1 + en_US: gpt-4.1 +model_type: llm +features: + - multi-tool-call + - agent-thought + - stream-tool-call + - vision +model_properties: + mode: chat + context_size: 1047576 +parameter_rules: + - name: temperature + use_template: temperature + - name: top_p + use_template: top_p + - name: presence_penalty + use_template: presence_penalty + - name: frequency_penalty + use_template: frequency_penalty + - name: max_tokens + use_template: max_tokens + default: 512 + min: 1 + max: 32768 + - name: reasoning_effort + label: + zh_Hans: 推理工作 + en_US: Reasoning Effort + type: string + help: + zh_Hans: 限制推理模型的推理工作 + en_US: Constrains effort on reasoning for reasoning models + required: false + options: + - low + - medium + - high + - name: response_format + label: + zh_Hans: 回复格式 + en_US: Response Format + type: string + help: + zh_Hans: 指定模型必须输出的格式 + en_US: specifying the format that the model must output + required: false + options: + - text + - json_object + - json_schema + - name: json_schema + use_template: json_schema +pricing: + input: '2.00' + output: '8.00' + unit: '0.000001' + currency: USD \ No newline at end of file diff --git a/api/core/model_runtime/model_providers/openai/llm/llm.py b/api/core/model_runtime/model_providers/openai/llm/llm.py index 37f7b33514..ab2580eb02 100644 --- a/api/core/model_runtime/model_providers/openai/llm/llm.py +++ b/api/core/model_runtime/model_providers/openai/llm/llm.py @@ -1049,6 +1049,9 @@ class OpenAILargeLanguageModel(_CommonOpenAI, LargeLanguageModel): """Calculate num tokens for gpt-3.5-turbo and gpt-4 with tiktoken package. Official documentation: https://github.com/openai/openai-cookbook/blob/main/examples/How_to_format_inputs_to_ChatGPT_models.ipynb""" + if not messages and not tools: + return 0 + if model.startswith("ft:"): model = model.split(":")[1] @@ -1058,17 +1061,17 @@ class OpenAILargeLanguageModel(_CommonOpenAI, LargeLanguageModel): try: encoding = tiktoken.get_encoding(model) - except KeyError: + except (KeyError, ValueError) as e: logger.warning("Warning: model not found. Using cl100k_base encoding.") - model = "cl100k_base" - encoding = tiktoken.get_encoding(model) + encoding_name = "cl100k_base" + encoding = tiktoken.get_encoding(encoding_name) if model.startswith("gpt-3.5-turbo-0301"): # every message follows {role/name}\n{content}\n tokens_per_message = 4 # if there's a name, the role is omitted tokens_per_name = -1 - elif model.startswith("gpt-3.5-turbo") or model.startswith("gpt-4") or model.startswith(("o1", "o3")): + elif model.startswith("gpt-3.5-turbo") or model.startswith("gpt-4") or model.startswith(("o1", "o3", "o4")): tokens_per_message = 3 tokens_per_name = 1 else: