mirror of
https://git.mirrors.martin98.com/https://github.com/langgenius/dify.git
synced 2025-08-14 03:25:57 +08:00
feat: support OPENAI json_schema (#7258)
This commit is contained in:
parent
5aa373dc04
commit
6ff7fd80a1
@ -1,4 +1,3 @@
|
|||||||
|
|
||||||
from core.model_runtime.entities.model_entities import DefaultParameterName
|
from core.model_runtime.entities.model_entities import DefaultParameterName
|
||||||
|
|
||||||
PARAMETER_RULE_TEMPLATE: dict[DefaultParameterName, dict] = {
|
PARAMETER_RULE_TEMPLATE: dict[DefaultParameterName, dict] = {
|
||||||
@ -94,5 +93,16 @@ PARAMETER_RULE_TEMPLATE: dict[DefaultParameterName, dict] = {
|
|||||||
},
|
},
|
||||||
'required': False,
|
'required': False,
|
||||||
'options': ['JSON', 'XML'],
|
'options': ['JSON', 'XML'],
|
||||||
}
|
},
|
||||||
}
|
DefaultParameterName.JSON_SCHEMA: {
|
||||||
|
'label': {
|
||||||
|
'en_US': 'JSON Schema',
|
||||||
|
},
|
||||||
|
'type': 'text',
|
||||||
|
'help': {
|
||||||
|
'en_US': 'Set a response json schema will ensure LLM to adhere it.',
|
||||||
|
'zh_Hans': '设置返回的json schema,llm将按照它返回',
|
||||||
|
},
|
||||||
|
'required': False,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
@ -95,6 +95,7 @@ class DefaultParameterName(Enum):
|
|||||||
FREQUENCY_PENALTY = "frequency_penalty"
|
FREQUENCY_PENALTY = "frequency_penalty"
|
||||||
MAX_TOKENS = "max_tokens"
|
MAX_TOKENS = "max_tokens"
|
||||||
RESPONSE_FORMAT = "response_format"
|
RESPONSE_FORMAT = "response_format"
|
||||||
|
JSON_SCHEMA = "json_schema"
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def value_of(cls, value: Any) -> 'DefaultParameterName':
|
def value_of(cls, value: Any) -> 'DefaultParameterName':
|
||||||
@ -118,6 +119,7 @@ class ParameterType(Enum):
|
|||||||
INT = "int"
|
INT = "int"
|
||||||
STRING = "string"
|
STRING = "string"
|
||||||
BOOLEAN = "boolean"
|
BOOLEAN = "boolean"
|
||||||
|
TEXT = "text"
|
||||||
|
|
||||||
|
|
||||||
class ModelPropertyKey(Enum):
|
class ModelPropertyKey(Enum):
|
||||||
|
@ -37,6 +37,9 @@ parameter_rules:
|
|||||||
options:
|
options:
|
||||||
- text
|
- text
|
||||||
- json_object
|
- json_object
|
||||||
|
- json_schema
|
||||||
|
- name: json_schema
|
||||||
|
use_template: json_schema
|
||||||
pricing:
|
pricing:
|
||||||
input: '2.50'
|
input: '2.50'
|
||||||
output: '10.00'
|
output: '10.00'
|
||||||
|
@ -37,6 +37,9 @@ parameter_rules:
|
|||||||
options:
|
options:
|
||||||
- text
|
- text
|
||||||
- json_object
|
- json_object
|
||||||
|
- json_schema
|
||||||
|
- name: json_schema
|
||||||
|
use_template: json_schema
|
||||||
pricing:
|
pricing:
|
||||||
input: '0.15'
|
input: '0.15'
|
||||||
output: '0.60'
|
output: '0.60'
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
import json
|
||||||
import logging
|
import logging
|
||||||
from collections.abc import Generator
|
from collections.abc import Generator
|
||||||
from typing import Optional, Union, cast
|
from typing import Optional, Union, cast
|
||||||
@ -544,13 +545,18 @@ class OpenAILargeLanguageModel(_CommonOpenAI, LargeLanguageModel):
|
|||||||
|
|
||||||
response_format = model_parameters.get("response_format")
|
response_format = model_parameters.get("response_format")
|
||||||
if response_format:
|
if response_format:
|
||||||
if response_format == "json_object":
|
if response_format == "json_schema":
|
||||||
response_format = {"type": "json_object"}
|
json_schema = model_parameters.get("json_schema")
|
||||||
|
if not json_schema:
|
||||||
|
raise ValueError("Must define JSON Schema when the response format is json_schema")
|
||||||
|
try:
|
||||||
|
schema = json.loads(json_schema)
|
||||||
|
except:
|
||||||
|
raise ValueError(f"not currect json_schema format: {json_schema}")
|
||||||
|
model_parameters.pop("json_schema")
|
||||||
|
model_parameters["response_format"] = {"type": "json_schema", "json_schema": schema}
|
||||||
else:
|
else:
|
||||||
response_format = {"type": "text"}
|
model_parameters["response_format"] = {"type": response_format}
|
||||||
|
|
||||||
model_parameters["response_format"] = response_format
|
|
||||||
|
|
||||||
|
|
||||||
extra_model_kwargs = {}
|
extra_model_kwargs = {}
|
||||||
|
|
||||||
|
@ -100,7 +100,7 @@ const ParameterItem: FC<ParameterItemProps> = ({
|
|||||||
handleInputChange(v === 1)
|
handleInputChange(v === 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
const handleStringInputChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
const handleStringInputChange = (e: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>) => {
|
||||||
handleInputChange(e.target.value)
|
handleInputChange(e.target.value)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -190,6 +190,16 @@ const ParameterItem: FC<ParameterItemProps> = ({
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (parameterRule.type === 'text') {
|
||||||
|
return (
|
||||||
|
<textarea
|
||||||
|
className='w-full h-20 ml-4 px-1 rounded-lg bg-gray-100 outline-none text-[12px] text-gray-900'
|
||||||
|
value={renderValue as string}
|
||||||
|
onChange={handleStringInputChange}
|
||||||
|
/>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
if (parameterRule.type === 'string' && !!parameterRule?.options?.length) {
|
if (parameterRule.type === 'string' && !!parameterRule?.options?.length) {
|
||||||
return (
|
return (
|
||||||
<SimpleSelect
|
<SimpleSelect
|
||||||
|
Loading…
x
Reference in New Issue
Block a user