feat: solution for 'сырой текст задания → плоская карточка'
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
node_modules/
|
||||
.env
|
||||
dist/
|
||||
build/
|
||||
*.log
|
||||
Submodule
+1
Submodule 3 added at ddd2bdb8a9
Submodule
+1
Submodule 8-deep-agents-from-scratch added at 380e236ecf
@@ -0,0 +1,84 @@
|
||||
# Assignment Card Extraction
|
||||
|
||||
This project demonstrates how to convert a natural language assignment description into a structured data object using **LangChain** and **Pydantic**.
|
||||
|
||||
## Features
|
||||
|
||||
- Parses assignment details such as title, subject, deadline, deliverable type, and grading hints.
|
||||
- Uses OpenAI's GPT model to interpret free‑form text.
|
||||
- Validates the output with a Pydantic model to ensure type safety.
|
||||
|
||||
## Prerequisites
|
||||
|
||||
- Python 3.10 or newer
|
||||
- An OpenAI API key
|
||||
|
||||
## Setup
|
||||
|
||||
1. **Clone the repository** (or copy the files into a directory):
|
||||
|
||||
```bash
|
||||
git clone https://github.com/your-username/assignment-card-extractor.git
|
||||
cd assignment-card-extractor
|
||||
```
|
||||
|
||||
2. **Create a virtual environment** (recommended):
|
||||
|
||||
```bash
|
||||
python -m venv .venv
|
||||
source .venv/bin/activate # On Windows: .venv\Scripts\activate
|
||||
```
|
||||
|
||||
3. **Install dependencies**:
|
||||
|
||||
```bash
|
||||
pip install -r requirements.txt
|
||||
```
|
||||
|
||||
4. **Configure the OpenAI API key**:
|
||||
|
||||
- Open the `.env` file.
|
||||
- Replace `your_openai_api_key_here` with your actual key.
|
||||
|
||||
```dotenv
|
||||
OPENAI_API_KEY=sk-XXXXXXXXXXXXXXXXXXXX
|
||||
```
|
||||
|
||||
## Running the Example
|
||||
|
||||
```bash
|
||||
python src/main.py
|
||||
```
|
||||
|
||||
You should see output similar to:
|
||||
|
||||
```
|
||||
=== Parsed Assignment Card ===
|
||||
{
|
||||
"title": "Мини-отчёт по LangChain",
|
||||
"subject": "LangChain",
|
||||
"deadline_hint": "к пятнице",
|
||||
"deliverable_type": "отчёт",
|
||||
"grading_hints": [
|
||||
"полнота",
|
||||
"пример кода"
|
||||
]
|
||||
}
|
||||
|
||||
=== Human-readable Summary ===
|
||||
Title: Мини-отчёт по LangChain
|
||||
Subject: LangChain
|
||||
Deadline: к пятнице
|
||||
Deliverable: отчёт
|
||||
Grading Hints: полнота, пример кода
|
||||
```
|
||||
|
||||
## Customization
|
||||
|
||||
- **Change the LLM**: Edit `src/main.py` to use a different model or adjust temperature.
|
||||
- **Add more fields**: Update the `AssignmentCard` model and the prompt template accordingly.
|
||||
- **Use in a pipeline**: Import the `chain` object from `src/main.py` into your own application.
|
||||
|
||||
## License
|
||||
|
||||
MIT License
|
||||
Submodule
+1
Submodule human-in-the-loop-interrupt-resume added at 3c81f16ab4
Submodule
+1
Submodule human-in-the-loop-middleware added at 082d5fb669
Submodule
+1
Submodule llm-interrupt added at 67ab81df8f
Submodule
+1
Submodule mcp added at 1fbb6def58
Submodule
+1
Submodule rag added at a8a8111eca
@@ -0,0 +1,4 @@
|
||||
langchain-core>=0.2.0
|
||||
langchain-openai>=0.2.0
|
||||
pydantic>=2.0
|
||||
python-dotenv>=1.0
|
||||
+116
@@ -0,0 +1,116 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
"""
|
||||
Assignment Card Extraction
|
||||
|
||||
This script demonstrates how to extract structured assignment details from a
|
||||
natural language description using LangChain and Pydantic.
|
||||
"""
|
||||
|
||||
import os
|
||||
from typing import List
|
||||
|
||||
from dotenv import load_dotenv
|
||||
from langchain_core.output_parsers import PydanticOutputParser
|
||||
from langchain_core.prompts import PromptTemplate
|
||||
from langchain_openai import ChatOpenAI
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
# Load environment variables (expects OPENAI_API_KEY)
|
||||
load_dotenv()
|
||||
|
||||
# --------------------------------------------------------------------------- #
|
||||
# Pydantic model definition
|
||||
# --------------------------------------------------------------------------- #
|
||||
class AssignmentCard(BaseModel):
|
||||
"""
|
||||
Structured representation of an assignment description.
|
||||
"""
|
||||
|
||||
title: str = Field(
|
||||
...,
|
||||
description="Short title of the assignment (e.g., 'Mini-report on LangChain').",
|
||||
)
|
||||
subject: str = Field(
|
||||
...,
|
||||
description="Subject or topic of the assignment (e.g., 'LangChain').",
|
||||
)
|
||||
deadline_hint: str = Field(
|
||||
...,
|
||||
description="A short phrase indicating the deadline (e.g., 'by Friday').",
|
||||
)
|
||||
deliverable_type: str = Field(
|
||||
...,
|
||||
description="What to submit: report, code, presentation, etc.",
|
||||
)
|
||||
grading_hints: List[str] = Field(
|
||||
...,
|
||||
description="List of key grading criteria mentioned in the description.",
|
||||
)
|
||||
|
||||
# --------------------------------------------------------------------------- #
|
||||
# LangChain components
|
||||
# --------------------------------------------------------------------------- #
|
||||
# Parser that will convert the LLM output into an AssignmentCard instance
|
||||
parser = PydanticOutputParser(pydantic_object=AssignmentCard)
|
||||
|
||||
# Prompt template that instructs the LLM to output JSON matching the model
|
||||
prompt = PromptTemplate(
|
||||
template=(
|
||||
"You are an assignment extraction assistant. "
|
||||
"Given the following assignment description, extract the following fields:\n\n"
|
||||
"- title: short title of the assignment\n"
|
||||
"- subject: subject or topic\n"
|
||||
"- deadline_hint: a short phrase indicating the deadline\n"
|
||||
"- deliverable_type: what to submit (e.g., report, code, presentation)\n"
|
||||
"- grading_hints: list of key grading criteria mentioned\n\n"
|
||||
"Return a JSON object with exactly these keys. Do not include any additional keys or text.\n\n"
|
||||
"Description: {description}\n\n"
|
||||
"{format_instructions}"
|
||||
),
|
||||
input_variables=["description"],
|
||||
partial_variables={"format_instructions": parser.get_format_instructions()},
|
||||
)
|
||||
|
||||
# LLM configuration
|
||||
llm = ChatOpenAI(
|
||||
temperature=0,
|
||||
model="gpt-3.5-turbo",
|
||||
)
|
||||
|
||||
# Chain: prompt -> LLM -> parser
|
||||
chain = prompt | llm | parser
|
||||
|
||||
# --------------------------------------------------------------------------- #
|
||||
# Main execution
|
||||
# --------------------------------------------------------------------------- #
|
||||
def main() -> None:
|
||||
# Sample assignment description
|
||||
sample_description = (
|
||||
"Сдайте к пятнице мини-отчёт по LangChain: 2 страницы, упор на агентов. "
|
||||
"Оценка: за полноту и за пример кода."
|
||||
)
|
||||
|
||||
# Run the chain
|
||||
try:
|
||||
result = chain.invoke({"description": sample_description})
|
||||
except Exception as e:
|
||||
print(f"Error during chain execution: {e}")
|
||||
return
|
||||
|
||||
# The result is already a validated AssignmentCard instance
|
||||
print("\n=== Parsed Assignment Card ===")
|
||||
print(result.model_dump(indent=2))
|
||||
|
||||
# Human-readable summary
|
||||
print("\n=== Human-readable Summary ===")
|
||||
print(f"Title: {result.title}")
|
||||
print(f"Subject: {result.subject}")
|
||||
print(f"Deadline: {result.deadline_hint}")
|
||||
print(f"Deliverable: {result.deliverable_type}")
|
||||
print(f"Grading Hints: {', '.join(result.grading_hints)}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,66 @@
|
||||
# Login App
|
||||
|
||||
A simple React application demonstrating a login form with email and password fields, along with "Forgot password?" and "Register" links that navigate to their respective routes.
|
||||
|
||||
## Features
|
||||
|
||||
- **Login Form**: Email and password inputs with basic validation.
|
||||
- **Routing**: Uses `react-router-dom` for navigation between login, forgot password, and register pages.
|
||||
- **Minimal Styling**: Basic CSS to make the UI clean and functional.
|
||||
|
||||
## Getting Started
|
||||
|
||||
### Prerequisites
|
||||
|
||||
- Node.js (v14 or newer)
|
||||
- npm (v6 or newer)
|
||||
|
||||
### Installation
|
||||
|
||||
```bash
|
||||
# Clone the repository
|
||||
git clone https://github.com/your-username/login-app.git
|
||||
cd login-app
|
||||
|
||||
# Install dependencies
|
||||
npm install
|
||||
```
|
||||
|
||||
### Running the App
|
||||
|
||||
```bash
|
||||
npm start
|
||||
```
|
||||
|
||||
Open your browser and navigate to `http://localhost:3000`. You should see the login page.
|
||||
|
||||
### Building for Production
|
||||
|
||||
```bash
|
||||
npm run build
|
||||
```
|
||||
|
||||
The production-ready files will be in the `build/` directory.
|
||||
|
||||
## Project Structure
|
||||
|
||||
```
|
||||
login-app/
|
||||
├── node_modules/
|
||||
├── public/
|
||||
├── src/
|
||||
│ ├── components/
|
||||
│ │ ├── ForgotPassword.js
|
||||
│ │ ├── Login.js
|
||||
│ │ ├── Login.css
|
||||
│ │ └── Register.js
|
||||
│ ├── App.js
|
||||
│ ├── index.css
|
||||
│ └── index.js
|
||||
├── package.json
|
||||
└── README.md
|
||||
```
|
||||
|
||||
## License
|
||||
|
||||
This project is open source and available under the MIT License.
|
||||
@@ -0,0 +1,17 @@
|
||||
{
|
||||
"name": "login-app",
|
||||
"version": "0.1.0",
|
||||
"private": true,
|
||||
"dependencies": {
|
||||
"react": "^18.2.0",
|
||||
"react-dom": "^18.2.0",
|
||||
"react-router-dom": "^6.14.1",
|
||||
"react-scripts": "5.0.1"
|
||||
},
|
||||
"scripts": {
|
||||
"start": "react-scripts start",
|
||||
"build": "react-scripts build",
|
||||
"test": "react-scripts test",
|
||||
"eject": "react-scripts eject"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
import { BrowserRouter as Router, Routes, Route, Navigate } from 'react-router-dom';
|
||||
import Login from './components/Login';
|
||||
import ForgotPassword from './components/ForgotPassword';
|
||||
import Register from './components/Register';
|
||||
|
||||
function App() {
|
||||
return (
|
||||
<Router>
|
||||
<Routes>
|
||||
<Route path="/" element={<Navigate replace to="/login" />} />
|
||||
<Route path="/login" element={<Login />} />
|
||||
<Route path="/forgot-password" element={<ForgotPassword />} />
|
||||
<Route path="/register" element={<Register />} />
|
||||
</Routes>
|
||||
</Router>
|
||||
);
|
||||
}
|
||||
|
||||
export default App;
|
||||
@@ -0,0 +1,39 @@
|
||||
import React from 'react';
|
||||
import { Routes, Route, Navigate } from 'react-router-dom';
|
||||
import LoginForm from './components/LoginForm';
|
||||
import { Box, Typography } from '@mui/material';
|
||||
|
||||
const RegisterPage: React.FC = () => (
|
||||
<Box sx={{ p: 4 }}>
|
||||
<Typography variant="h4">Register Page</Typography>
|
||||
<Typography>Registration form will go here.</Typography>
|
||||
</Box>
|
||||
);
|
||||
|
||||
const ForgotPasswordPage: React.FC = () => (
|
||||
<Box sx={{ p: 4 }}>
|
||||
<Typography variant="h4">Forgot Password</Typography>
|
||||
<Typography>Forgot password form will go here.</Typography>
|
||||
</Box>
|
||||
);
|
||||
|
||||
const HomePage: React.FC = () => (
|
||||
<Box sx={{ p: 4 }}>
|
||||
<Typography variant="h4">Welcome to the App</Typography>
|
||||
<Typography>Use the navigation to login, register, or reset password.</Typography>
|
||||
</Box>
|
||||
);
|
||||
|
||||
const App: React.FC = () => {
|
||||
return (
|
||||
<Routes>
|
||||
<Route path="/" element={<Navigate replace to="/login" />} />
|
||||
<Route path="/login" element={<LoginForm />} />
|
||||
<Route path="/register" element={<RegisterPage />} />
|
||||
<Route path="/forgot-password" element={<ForgotPasswordPage />} />
|
||||
<Route path="*" element={<HomePage />} />
|
||||
</Routes>
|
||||
);
|
||||
};
|
||||
|
||||
export default App;
|
||||
@@ -0,0 +1,13 @@
|
||||
import { Link } from 'react-router-dom';
|
||||
|
||||
function ForgotPassword() {
|
||||
return (
|
||||
<div style={{ padding: '20px' }}>
|
||||
<h2>Forgot Password</h2>
|
||||
<p>This is a placeholder page for password recovery.</p>
|
||||
<Link to="/login">Back to Login</Link>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export default ForgotPassword;
|
||||
@@ -0,0 +1,38 @@
|
||||
.login-container {
|
||||
max-width: 400px;
|
||||
margin: 80px auto;
|
||||
padding: 20px;
|
||||
border: 1px solid #ddd;
|
||||
border-radius: 8px;
|
||||
background-color: #fafafa;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.login-form {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 15px;
|
||||
}
|
||||
|
||||
.login-form label {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
font-weight: 500;
|
||||
text-align: left;
|
||||
}
|
||||
|
||||
.login-form input {
|
||||
padding: 8px;
|
||||
font-size: 1rem;
|
||||
margin-top: 5px;
|
||||
}
|
||||
|
||||
.login-form button {
|
||||
padding: 10px;
|
||||
font-size: 1rem;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.login-links {
|
||||
margin-top: 15px;
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
import { useState } from 'react';
|
||||
import { Link, useNavigate } from 'react-router-dom';
|
||||
import './Login.css';
|
||||
|
||||
function Login() {
|
||||
const [email, setEmail] = useState('');
|
||||
const [password, setPassword] = useState('');
|
||||
const navigate = useNavigate();
|
||||
|
||||
const handleSubmit = (e) => {
|
||||
e.preventDefault();
|
||||
// Placeholder for authentication logic
|
||||
console.log('Email:', email);
|
||||
console.log('Password:', password);
|
||||
// After successful login, navigate to a protected route or dashboard
|
||||
// navigate('/dashboard');
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="login-container">
|
||||
<h2>Login</h2>
|
||||
<form onSubmit={handleSubmit} className="login-form">
|
||||
<label>
|
||||
Email:
|
||||
<input
|
||||
type="email"
|
||||
value={email}
|
||||
onChange={(e) => setEmail(e.target.value)}
|
||||
required
|
||||
/>
|
||||
</label>
|
||||
|
||||
<label>
|
||||
Password:
|
||||
<input
|
||||
type="password"
|
||||
value={password}
|
||||
onChange={(e) => setPassword(e.target.value)}
|
||||
required
|
||||
/>
|
||||
</label>
|
||||
|
||||
<button type="submit">Login</button>
|
||||
</form>
|
||||
|
||||
<div className="login-links">
|
||||
<Link to="/forgot-password">Forgot password?</Link>
|
||||
<span> | </span>
|
||||
<Link to="/register">Register</Link>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export default Login;
|
||||
@@ -0,0 +1,121 @@
|
||||
import React, { useState, FormEvent } from 'react';
|
||||
import {
|
||||
Box,
|
||||
Button,
|
||||
TextField,
|
||||
Link,
|
||||
Typography,
|
||||
Stack,
|
||||
Divider,
|
||||
} from '@mui/material';
|
||||
import { Link as RouterLink } from 'react-router-dom';
|
||||
import GoogleIcon from '@mui/icons-material/Google';
|
||||
import FacebookIcon from '@mui/icons-material/Facebook';
|
||||
|
||||
const LoginForm: React.FC = () => {
|
||||
const [email, setEmail] = useState<string>('');
|
||||
const [password, setPassword] = useState<string>('');
|
||||
const [errors, setErrors] = useState<{ email?: string; password?: string }>({});
|
||||
|
||||
const validate = () => {
|
||||
const newErrors: { email?: string; password?: string } = {};
|
||||
if (!email) {
|
||||
newErrors.email = 'Email is required';
|
||||
} else if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email)) {
|
||||
newErrors.email = 'Invalid email address';
|
||||
}
|
||||
if (!password) {
|
||||
newErrors.password = 'Password is required';
|
||||
} else if (password.length < 6) {
|
||||
newErrors.password = 'Password must be at least 6 characters';
|
||||
}
|
||||
setErrors(newErrors);
|
||||
return Object.keys(newErrors).length === 0;
|
||||
};
|
||||
|
||||
const handleSubmit = (e: FormEvent) => {
|
||||
e.preventDefault();
|
||||
if (!validate()) return;
|
||||
console.log('Submitting', { email, password });
|
||||
// Placeholder for actual authentication logic
|
||||
};
|
||||
|
||||
const handleThirdPartyLogin = (provider: string) => {
|
||||
console.log(`Logging in with ${provider}`);
|
||||
// Placeholder for third‑party auth
|
||||
};
|
||||
|
||||
return (
|
||||
<Box
|
||||
sx={{
|
||||
maxWidth: 400,
|
||||
mx: 'auto',
|
||||
mt: 8,
|
||||
p: 4,
|
||||
border: '1px solid #e0e0e0',
|
||||
borderRadius: 2,
|
||||
boxShadow: 3,
|
||||
}}
|
||||
>
|
||||
<Typography variant="h5" component="h1" gutterBottom>
|
||||
Sign In
|
||||
</Typography>
|
||||
<Box component="form" onSubmit={handleSubmit} noValidate>
|
||||
<TextField
|
||||
label="Email"
|
||||
type="email"
|
||||
fullWidth
|
||||
margin="normal"
|
||||
value={email}
|
||||
onChange={(e) => setEmail(e.target.value)}
|
||||
error={!!errors.email}
|
||||
helperText={errors.email}
|
||||
/>
|
||||
<TextField
|
||||
label="Password"
|
||||
type="password"
|
||||
fullWidth
|
||||
margin="normal"
|
||||
value={password}
|
||||
onChange={(e) => setPassword(e.target.value)}
|
||||
error={!!errors.password}
|
||||
helperText={errors.password}
|
||||
/>
|
||||
<Box sx={{ display: 'flex', justifyContent: 'space-between', mt: 1 }}>
|
||||
<Link component={RouterLink} to="/forgot-password" variant="body2">
|
||||
Forgot password?
|
||||
</Link>
|
||||
<Link component={RouterLink} to="/register" variant="body2">
|
||||
Register
|
||||
</Link>
|
||||
</Box>
|
||||
<Button type="submit" variant="contained" color="primary" fullWidth sx={{ mt: 2 }}>
|
||||
Sign In
|
||||
</Button>
|
||||
</Box>
|
||||
|
||||
<Divider sx={{ my: 3 }}>or</Divider>
|
||||
|
||||
<Stack spacing={2}>
|
||||
<Button
|
||||
variant="outlined"
|
||||
fullWidth
|
||||
startIcon={<GoogleIcon />}
|
||||
onClick={() => handleThirdPartyLogin('Google')}
|
||||
>
|
||||
Sign in with Google
|
||||
</Button>
|
||||
<Button
|
||||
variant="outlined"
|
||||
fullWidth
|
||||
startIcon={<FacebookIcon />}
|
||||
onClick={() => handleThirdPartyLogin('Facebook')}
|
||||
>
|
||||
Sign in with Facebook
|
||||
</Button>
|
||||
</Stack>
|
||||
</Box>
|
||||
);
|
||||
};
|
||||
|
||||
export default LoginForm;
|
||||
@@ -0,0 +1,13 @@
|
||||
import { Link } from 'react-router-dom';
|
||||
|
||||
function Register() {
|
||||
return (
|
||||
<div style={{ padding: '20px' }}>
|
||||
<h2>Register</h2>
|
||||
<p>This is a placeholder page for user registration.</p>
|
||||
<Link to="/login">Back to Login</Link>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export default Register;
|
||||
@@ -0,0 +1,5 @@
|
||||
body {
|
||||
margin: 0;
|
||||
font-family: Arial, Helvetica, sans-serif;
|
||||
background-color: #f0f2f5;
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
import React from 'react';
|
||||
import ReactDOM from 'react-dom/client';
|
||||
import App from './App';
|
||||
import './index.css';
|
||||
|
||||
const root = ReactDOM.createRoot(document.getElementById('root'));
|
||||
root.render(
|
||||
<React.StrictMode>
|
||||
<App />
|
||||
</React.StrictMode>
|
||||
);
|
||||
@@ -0,0 +1,17 @@
|
||||
import React from 'react';
|
||||
import ReactDOM from 'react-dom/client';
|
||||
import { BrowserRouter } from 'react-router-dom';
|
||||
import App from './App';
|
||||
import CssBaseline from '@mui/material/CssBaseline';
|
||||
|
||||
const root = ReactDOM.createRoot(
|
||||
document.getElementById('root') as HTMLElement
|
||||
);
|
||||
root.render(
|
||||
<React.StrictMode>
|
||||
<CssBaseline />
|
||||
<BrowserRouter>
|
||||
<App />
|
||||
</BrowserRouter>
|
||||
</React.StrictMode>
|
||||
);
|
||||
Reference in New Issue
Block a user