Skip to content

Commit 8ed235f

Browse files
hjlarryiamjoel
authored andcommitted
feat: support json_schema for ollama models (#11449)
1 parent 7bbb745 commit 8ed235f

File tree

1 file changed

+9
-7
lines changed
  • api/core/model_runtime/model_providers/ollama/llm

1 file changed

+9
-7
lines changed

api/core/model_runtime/model_providers/ollama/llm/llm.py

+9-7
Original file line numberDiff line numberDiff line change
@@ -181,9 +181,11 @@ def _generate(
181181
# prepare the payload for a simple ping to the model
182182
data = {"model": model, "stream": stream}
183183

184-
if "format" in model_parameters:
185-
data["format"] = model_parameters["format"]
186-
del model_parameters["format"]
184+
if format_schema := model_parameters.pop("format", None):
185+
try:
186+
data["format"] = format_schema if format_schema == "json" else json.loads(format_schema)
187+
except json.JSONDecodeError as e:
188+
raise InvokeBadRequestError(f"Invalid format schema: {str(e)}")
187189

188190
if "keep_alive" in model_parameters:
189191
data["keep_alive"] = model_parameters["keep_alive"]
@@ -733,12 +735,12 @@ def get_customizable_model_schema(self, model: str, credentials: dict) -> AIMode
733735
ParameterRule(
734736
name="format",
735737
label=I18nObject(en_US="Format", zh_Hans="返回格式"),
736-
type=ParameterType.STRING,
738+
type=ParameterType.TEXT,
739+
default="json",
737740
help=I18nObject(
738-
en_US="the format to return a response in. Currently the only accepted value is json.",
739-
zh_Hans="返回响应的格式。目前唯一接受的值是json。",
741+
en_US="the format to return a response in. Format can be `json` or a JSON schema.",
742+
zh_Hans="返回响应的格式。目前接受的值是字符串`json`或JSON schema.",
740743
),
741-
options=["json"],
742744
),
743745
],
744746
pricing=PriceConfig(

0 commit comments

Comments
 (0)