【LLM】 BaseModel的作用

在 Python 里,BaseModel 是 pydantic 库提供的一个基类,pydantic 是用于数据验证和设置管理的强大库。BaseModel 主要有以下作用:

1. 数据验证

BaseModel 能对输入的数据进行验证,保证数据符合定义的类型和约束。要是输入的数据不满足要求,pydantic 会抛出异常。

python 复制代码
from pydantic import BaseModel, Field

class User(BaseModel):
    name: str = Field(..., min_length=2, max_length=50)
    age: int = Field(..., gt=0, lt=120)

# 验证通过
user1 = User(name="Alice", age=25)
print(user1)

# 验证失败,抛出异常
try:
    user2 = User(name="A", age=150)
except Exception as e:
    print(e)

输出结果:

cmd 复制代码
name='Alice' age=25
2 validation errors for User
name
  String should have at least 2 characters [type=string_too_short, input_value='A', input_type=str]
    For further information visit https://errors.pydantic.dev/2.5/v/string_too_short
age
  Input should be less than 120 [type=number_too_big, input_value=150, input_type=int]
    For further information visit https://errors.pydantic.dev/2.5/v/number_too_big

2. 数据解析

BaseModel 可以把不同格式的数据(像字典、JSON 等)解析成 Python 对象,同时进行类型转换。

示例代码:

python 复制代码
from pydantic import BaseModel

class Book(BaseModel):
    title: str
    price: float

# 从字典解析数据
book_data = {"title": "Python Crash Course", "price": 29.99}
book = Book(**book_data)
print(book)

输出结果:

cmd 复制代码
title='Python Crash Course' price=29.99

3. 数据序列化

BaseModel 支持将 Python 对象序列化为字典或 JSON 字符串,方便数据的存储和传输。

py 复制代码
from pydantic import BaseModel

class Product(BaseModel):
    name: str
    quantity: int

product = Product(name="Laptop", quantity=10)

# 序列化为字典
product_dict = product.model_dump()
print(product_dict)

# 序列化为 JSON 字符串
product_json = product.model_dump_json()
print(product_json)

输出结果:

cmd 复制代码
{'name': 'Laptop', 'quantity': 10}
{"name": "Laptop", "quantity": 10}

4. 类型提示

借助 BaseModel 定义数据结构,能为代码提供清晰的类型提示,增强代码的可读性和可维护性。

在你当前编辑的代码里,Fetch 类继承自 BaseModel,目的是定义获取 URL 的参数,对输入参数进行验证和解析:

py 复制代码
class Fetch(BaseModel):
    """Parameters for fetching a URL."""

    url: Annotated[AnyUrl, Field(description="URL to fetch")]
    max_length: Annotated[
        int,
        Field(
            default=5000,
            description="Maximum number of characters to return.",
            gt=0,
            lt=1000000,
        ),
    ]
    start_index: Annotated[
        int,
        Field(
            default=0,
            description="On return output starting at this character index, useful if a previous fetch was truncated and more context is required.",
            ge=0,
        ),
    ]
    raw: Annotated[
        bool,
        Field(
            default=False,
            description="Get the actual HTML content of the requested page, without simplification.",
        ),
    ]
相关推荐
····懂···6 分钟前
抢占先机,PostgreSQL 中级专家认证的职业跃迁
数据库·postgresql
xiaobaibai1536 分钟前
智慧交通中目标检测 mAP↑28%:陌讯多模态融合算法实战解析
人工智能·算法·目标检测·计算机视觉·目标跟踪·视觉检测
GBASE16 分钟前
“G”术时刻:南大通用GBase 8c典型运维场景-扩缩容场景快速定位性能瓶颈
数据库
终将超越过去18 分钟前
分类-鸢尾花分类
人工智能·分类·数据挖掘
计算机科研圈19 分钟前
ICCV 2025 | EPD-Solver:西湖大学发布并行加速扩散采样算法
人工智能·算法·语言模型·自然语言处理·数据挖掘·iccv
涡能增压发动积34 分钟前
Browser-Use Agent使用初体验
人工智能·后端·python
zzywxc78734 分钟前
利用AI生成测试用例、优化测试执行、自我修复测试脚本,提升测试覆盖率和效率。
人工智能·测试用例·测试覆盖率
Elastic 中国社区官方博客34 分钟前
用于 UBI 的 Elasticsearch 插件:从搜索查询中分析用户行为
大数据·数据库·elasticsearch·搜索引擎·全文检索
汤姆yu37 分钟前
基于图像识别与分类的中国蛇类识别系统
人工智能·分类·数据挖掘·图像识别
小白不想白a39 分钟前
【MySQL安全】什么是SQL注入,怎么避免这种攻击:前端防护、后端orm框架、数据库白名单
数据库·sql·mysql·安全