Files

113 lines
3.1 KiB
Markdown
Raw Permalink Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 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**
> * Python3.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 builtin 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 humanreadable 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).
---