113 lines
3.1 KiB
Markdown
113 lines
3.1 KiB
Markdown
# Structured Output – Pydantic
|
||
|
||
> Extract a validated Pydantic object from raw text without manual parsing.
|
||
|
||
This repository contains the solution for the exam task *“Structured output (Pydantic)”* from the LangChain course “Generating Structured Outputs”.
|
||
|
||
---
|
||
|
||
## 📦 Installation
|
||
|
||
```bash
|
||
# Create and activate a virtual environment (optional but recommended)
|
||
python -m venv .venv
|
||
source .venv/bin/activate # Windows: .\.venv\Scripts\activate
|
||
|
||
# Install dependencies
|
||
pip install langchain-core langchain-openai pydantic python-dotenv
|
||
```
|
||
|
||
> **Requirements**
|
||
> * Python 3.10+
|
||
> * LangChain ≥ 1.0.0
|
||
|
||
The project uses the OpenAI LLM via `langchain_openai`. If you prefer another provider (e.g., Ollama) replace the import and configuration accordingly.
|
||
|
||
---
|
||
|
||
## 📁 Project layout
|
||
|
||
```
|
||
├── solution.py # Main script – contains models, chain, CLI
|
||
└── .env # Optional: store your OPENAI_API_KEY here
|
||
```
|
||
|
||
> **.env**
|
||
> Add a line `OPENAI_API_KEY=sk-…` to use the OpenAI API without passing the key on the command line.
|
||
|
||
---
|
||
|
||
## 🚀 Running the script
|
||
|
||
```bash
|
||
python solution.py [--text TEXT] [--model MODEL]
|
||
```
|
||
|
||
* `--text TEXT` – raw text to parse.
|
||
If omitted, the script will prompt you for input or use one of the built‑in examples.
|
||
* `--model MODEL` – optional LLM model name (default: `gpt-4o-mini`).
|
||
|
||
### Example 1 – Person description
|
||
|
||
```bash
|
||
python solution.py --text "John Doe is a senior software engineer at Acme Corp. He loves Python, Docker and Kubernetes."
|
||
```
|
||
|
||
**Output**
|
||
|
||
```
|
||
Model: PersonInfo
|
||
{
|
||
"name": "John Doe",
|
||
"age": null,
|
||
"profession": "senior software engineer at Acme Corp.",
|
||
"skills": ["Python", "Docker", "Kubernetes"]
|
||
}
|
||
Summary: Parsed as a person profile.
|
||
```
|
||
|
||
### Example 2 – Meeting notes
|
||
|
||
```bash
|
||
python solution.py --text "Meeting on 2024-05-28 with Alice, Bob and Carol. Topics: project roadmap, budget allocation. Decisions: approve Q3 budget, assign tasks to team."
|
||
```
|
||
|
||
**Output**
|
||
|
||
```
|
||
Model: MeetingNotes
|
||
{
|
||
"date": "2024-05-28",
|
||
"participants": ["Alice", "Bob", "Carol"],
|
||
"topics": ["project roadmap", "budget allocation"],
|
||
"decisions": ["approve Q3 budget", "assign tasks to team"]
|
||
}
|
||
Summary: Parsed as meeting notes.
|
||
```
|
||
|
||
---
|
||
|
||
## 📄 How it works
|
||
|
||
1. **Pydantic models** – `PersonInfo` and `MeetingNotes`, each field annotated with a description for the LLM.
|
||
2. **Prompt template** – instructs the model to output JSON that matches one of the schemas.
|
||
3. **Output parser** – `PydanticOutputParser` validates the returned JSON against the chosen schema.
|
||
4. **Schema selection** – a simple heuristic (keyword search) decides whether the input describes a person or a meeting, then runs the appropriate chain.
|
||
|
||
The script prints both the full parsed object (`model_dump()`) and a short human‑readable summary.
|
||
|
||
---
|
||
|
||
## 🛠️ Customisation
|
||
|
||
* Change the LLM model by editing `DEFAULT_MODEL` in `solution.py`.
|
||
* Add more heuristics for schema selection.
|
||
* Extend the models with additional fields or validation rules.
|
||
|
||
---
|
||
|
||
## 📄 License
|
||
|
||
MIT © 2026 – see [LICENSE](LICENSE) (if present).
|
||
|
||
--- |