大模型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) # 直接拿到对象,舒服!
相关推荐
神秘的猪头11 小时前
🔌 把 MCP 装进大脑!手把手带你构建能“热插拔”工具的 AI Agent
langchain·llm·mcp
智泊AI16 小时前
一文讲清:Agent、Workflow、MCP的区别是啥?
llm
哥不是小萝莉1 天前
OpenClaw 架构设计全解析
ai
warm3snow1 天前
Claude Code 黑客马拉松:5 个获奖项目,没有一个是"纯码农"做的
ai·大模型·llm·agent·skill·mcp
Ray Liang1 天前
被低估的量化版模型,小身材也能干大事
人工智能·ai·ai助手·mindx
代码匠心2 天前
AI 自动编程:一句话设计高颜值博客
前端·ai·ai编程·claude
马腾化云东2 天前
Agent开发应知应会(langfuse):Langfuse Score概念详解和实战应用
人工智能·llm·ai编程
Baihai_IDP2 天前
HackerNews 热榜第一名:AGI 的 A,原来代表的是 Ads(广告)
人工智能·程序员·llm
吴佳浩2 天前
OpenClaw Windows 完整安装与本地模型配置教程(实战版)
llm·openai