大模型JSON格式输出:instructor

参考:

https://github.com/567-labs/instructor

安装:

复制代码
pip install instructor

使用:

复制代码
from pydantic import BaseModel 
import instructor
from openai import OpenAI 

 # 定义你想要的数据结构 
class UserInfo(BaseModel):     
      name: str     
      age: int    
      skills: list[str]  

# 1. 準備工作:初始化用戶端
client = OpenAI(
    # 建議通過環境變數配置API Key,避免寫入程式碼。
    api_key="&&&393",
    # API Key與地區強綁定,請確保base_url與API Key的地區一致。
    base_url="https://&&olces.com/api/v3",
)

# 自动给 LLM 加上结构化约束
client = instructor.patch(client) 
 
user_info = client.chat.completions.create(    
 model="deepseek-v3-1-terminus",    
 response_model=UserInfo, #   直接传类,甚至不需要自己写 JSON Prompt    
 messages=[{"role": "user", "content": "Melon 今年 25 岁,擅长 Python 和 AI"}] ) 

print(user_info.name) # 直接拿到对象,舒服!
相关推荐
knqiufan21 小时前
PingCraft:从需求文档到可追踪工作项的 Agent 实践之路
ai·llm·agent·pingcode
qqxhb1 天前
11|结构化输出:为什么 JSON 能让系统更稳定
json·ai编程·结构化·规范模板
星辰_mya1 天前
一封AI的介绍信
大模型·ai编程
twc8291 天前
大模型基础概念(简要版)
大模型·llm
chaors1 天前
从零学RAG0x0f:RAG 评估指标提升实战
langchain·llm·ai编程
xixixi777771 天前
2026 年 03 月 20 日 AI+通信+安全行业日报(来更新啦)
大数据·人工智能·安全·ai·大模型·通信
汤姆yu1 天前
OpenClaw企业级落地方案
大模型·openclaw·企业落地方案
智算菩萨1 天前
【Generative AI For Autonomous Driving】1 生成式AI重塑自动驾驶的技术浪潮与体系化挑战
论文阅读·人工智能·深度学习·机器学习·ai·自动驾驶
智算菩萨1 天前
【Generative AI For Autonomous Driving】7 生成式AI驱动自动驾驶的未来图景:开放挑战、社会机遇与技术展望
论文阅读·人工智能·深度学习·机器学习·ai·自动驾驶
王菜鸟1 天前
什么是Agent及相关框架介绍
ai·ai编程