Ollama 0.5 支持结构化输出

升级 Ollama 到 0.5

在 AIGC 应用开发领域,实现结构化输出一直是一个挑战,不过行业内已经探索诸多方法来应对。OpenAI 在其模型的迭代中特别关注了这一问题,并做了相应的优化。

最近,Ollama 在其 0.5+版本中推出了关键更新,引入了与 OpenAI 相类似的结构化输出(Structured Outputs)特性。这项新功能允许开发者利用 JSON schema 来明确指定和限制模型的输出格式,确保了输出内容的格式一致性与预期性。目前,此功能已全面集成到 Ollama 的 Python 和 JavaScript 客户端库中,为开发者提供了更强大的工具来控制模型的响应格式。

我们之前使用的 ollama 0.4 版本,现对它进行升级。

ollama --version
# ollama version is 0.4.1

cd /root/RAGAll/ollama
mkdir release-0.5.1
cd release-0.5.1/ && wget https://github.com/ollama/ollama/releases/download/v0.5.1/ollama-linux-amd64.tgz

# 解压
tar zxvf ollama-linux-amd64.tgz
# 重新软链
rm /usr/bin/ollama
## rm: remove symbolic link ‘/usr/bin/ollama’? y
ln -s /root/RAGAll/ollama/release-0.5.1/bin/ollama /usr/bin/ollama

# 先启动ollama,查看版本
ollama --version
## ollama version is 0.5.1 

结构化输出测试

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

下面执行一些用例看效果。首先,更新 python 库:

pip install -U ollama

## Successfully installed annotated-types-0.7.0 ollama-0.4.4 pydantic-2.10.3 pydantic-core-2.27.1

问答输出格式化

编写测试代码, 使用 qwen 2.5 模型,回答关于中国的信息:

from ollama import chat
from pydantic import BaseModel

class Country(BaseModel):
  name: str
  capital: str
  languages: list[str]

response = chat(
  messages=[
    {
      'role': 'user',
      'content': 'Tell me about China.',
    }
  ],
  model='qwen2.5:7b',
  format=Country.model_json_schema(),
)

#country = Country.model_validate_json(response.message.content)
#print(country)
print(response.message.content)

输出:

{
  "capital": "Beijing",
  "languages": ["Mandarin", "Cantonese"],
  "name": "People's Republic of China"
}

知识抽取格式化

测试语料(注意!这些是大模型生成的内容,真实性未知,仅用于测试目的):

### 语料一:苹果 iPhone 14 Pro Max

苹果的iPhone 14 Pro Max配备了6.7英寸Super Retina XDR显示屏和A16 Bionic芯片,支持5G。它拥有4800万像素主摄及两个1200万像素辅助镜头。起售价为1099美元,提供128GB至1TB存储选项,适合追求高性能与摄影的用户。

### 语料二:三星 Galaxy S23 Ultra

三星Galaxy S23 Ultra搭载6.8英寸Dynamic AMOLED 2X显示屏,分辨率为3088x1440像素,刷新率120Hz。采用高通骁龙8 Gen 2处理器,最高12GB RAM和1TB存储。后置四摄系统,包括108MP主摄、两个10MP长焦镜头和12MP超广角镜头。起售价1199美元,适合需要高性能和优秀影像能力的用户。

### 语料三:小米 13 Pro

小米13 Pro拥有6.73英寸AMOLED显示屏,分辨率为3200x1440,支持1-120Hz动态刷新率。内置高通骁龙8 Gen 2处理器,12GB RAM和512GB UFS 4.0存储。摄影方面,配有50MP主摄和两颗50MP副摄。在中国市场的起售价为4999元人民币,是性价比高的高端选择。

测试代码:

from ollama import chat
from pydantic import BaseModel

class Phone(BaseModel):
  name: str
  price: str
  display: int
  camera: str | None
  cpu: str | None
  ram: str | None

class Phones(BaseModel):
  phones: list[Phone]

response = chat(
  messages=[
    {
      'role': 'user',
      'content': '''
        ### 语料一:苹果 iPhone 14 Pro Max
        
        苹果的iPhone 14 Pro Max配备了6.7英寸Super Retina XDR显示屏和A16 Bionic芯片,支持5G。它拥有4800万像素主摄及两个1200万像素辅助镜头。起售价为1099美元,提供128GB至1TB存储选项,适合追求高性能与摄影的用户。
        
        ### 语料二:三星 Galaxy S23 Ultra
        
        三星Galaxy S23 Ultra搭载6.8英寸Dynamic AMOLED 2X显示屏,分辨率为3088x1440像素,刷新率120Hz。采用高通骁龙8 Gen 2处理器,最高12GB RAM和1TB存储。后置四摄系统,包括108MP主摄、两个10MP长焦镜头和12MP超广角镜头。起售价1199美元,适合需要高性能和优秀影像能力的用户。
        
        ### 语料三:小米 13 Pro
        
        小米13 Pro拥有6.73英寸AMOLED显示屏,分辨率为3200x1440,支持1-120Hz动态刷新率。内置高通骁龙8 Gen 2处理器,12GB RAM和512GB UFS 4.0存储。摄影方面,配有50MP主摄和两颗50MP副摄。在中国市场的起售价为4999元人民币,是性价比高的高端选择。
      ''',
    }
  ],
  model='qwen2.5:7b',
  format=Phones.model_json_schema(),
)

# ps = Phones.model_validate_json(response.message.content)
print(response.message.content)

输出(json):

图片结构化输出测试

测试图片:

由于没有好的视觉模型,进行以下简单测试。

from ollama import chat
from typing import Literal, List, Optional
from pydantic import BaseModel

class Object(BaseModel):
  name: str
  confidence: float
  attributes: str

class ImageDescription(BaseModel):
  summary: str
  objects: List[Object]
  scene: str
  colors: List[str]
  time_of_day: Literal['Morning', 'Afternoon', 'Evening', 'Night']
  setting: Literal['Indoor', 'Outdoor', 'Unknown']
  text_content: Optional[str] = None

path = '../photos/beach.jpg'

response = chat(
  model='llama3.2-vision:11b',
  format=ImageDescription.model_json_schema(),  # Pass in the schema for the response
  messages=[
    {
      'role': 'user',
      'content': 'Analyze this image and describe what you see, including any objects, the scene, colors and any text you can detect.',
      'images': [path],
    },
  ],
  options={'temperature': 0},  # Set temperature to 0 for more deterministic output
)

image_description = ImageDescription.model_validate_json(response.message.content)
print(image_description)

输出:

summary='A palm tree stands on a sandy beach under a blue sky with white clouds.' 
objects=[Object(name='tree', confidence=0.99, attributes='palm tree'), Object(name='beach', confidence=1.0, attributes='sand')] 
scene='beach' colors=['blue', 'green', 'white'] 
time_of_day='Afternoon' 
setting='Outdoor' 
text_content='The image shows a palm tree standing on a sandy beach. The sky is blue and there are white clouds in the background. The overall atmosphere suggests a warm and sunny day, possibly during the afternoon or early evening.'

小结

结果还不错,如果能稳定输出的话,对于AIGC工程化是莫大的鼓励。天下苦格式幻觉久矣!

参考文献

Ollama 引入与OpenAI一样的结构化输出功能,开发者头疼的问题将成过去式

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

1024点线面

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值