Introduction to the basic steps
Extracting structured data from natural language using Instructor can be divided into three main steps:
- Defining the Pydantic Data Model
- Initializing the OpenAI client and integrating with Instructor
- Call the chat.completions.create method
Detailed operating instructions
Here are the key points of the concrete implementation example:
- Model Definition: You need to create a class that inherits from BaseModel and explicitly specify the field types.
- Client Integration: Wrapping the standard OpenAI client using the constructor.from_openai() method
- API call: The response_model parameter must be specified to associate an output structure.
Best Practice Recommendations
- Add detailed descriptions to key fields to improve model understanding
- Consider adding optional fields to deal with uncertainties
- For complex structures, nested modeling is possible
This answer comes from the articleInstructor: a Python library to simplify structured output workflows for large language modelsThe































