Ollama 支持结构化输出,可用 JSON schema 约束模型返回格式
Structured outputs · Ollama Blog
Ollama 新增结构化输出支持,可通过 JSON schema 把模型输出约束为指定格式,Python 和 JavaScript 库已同步更新。该功能可用于从文档解析数据、从图像提取数据以及统一语言模型响应格式,官方称其比 JSON mode 更可靠一致。使用时可在 cURL 请求或 Python、JavaScript 库中通过 format 参数传入 schema,官方建议用 Pydantic 或 Zod 定义 schema、在提示词中加上 return as JSON,并把 temperature 设为 0。
正文
当前显示原文,中文全文译文尚未就绪。
December 6, 2024
Ollama now supports structured outputs making it possible to constrain a model’s output to a specific format defined by a JSON schema. The Ollama Python and JavaScript libraries have been updated to support structured outputs.
Use cases for structured outputs include:
Parsing data from documents; Extracting data from images; Structuring all language model responses; More reliability and consistency than JSON mode
Download the latest version of Ollama
Upgrade to the latest version of the Ollama Python or JavaScript library:
Python
JavaScript
To pass structured outputs to the model, the format parameter can be used in the cURL request or the format parameter in the Python or JavaScript libraries.
cURL
Output
The response is returned in the format defined by the JSON schema in the request.
Python
Using the Ollama Python library, pass in the schema as a JSON object to the format parameter as either dict or use Pydantic (recommended) to serialize the schema using model_json_schema().
Output
JavaScript
Using the Ollama JavaScript library, pass in the schema as a JSON object to the format parameter as either object or use Zod (recommended) to serialize the schema using zodToJsonSchema().
To extract structured data from text, define a schema to represent information. The model then extracts the information and returns the data in the defined schema as JSON:
Example output
Image description
Structured outputs can also be used with vision models. For example, the following code uses llama3.2-vision to describe the following image and returns a structured output:
For reliable use of structured outputs, consider to:
Use Pydantic (Python) or Zod (JavaScript) to define the schema for the response; Add “return as JSON” to the prompt to help the model understand the request; Set the temperature to 0 for more deterministic output
What’s next?
Exposing logits for controlled generation; Performance and accuracy improvements for structured outputs; GPU acceleration for sampling; Additional format support beyond JSON schema