From ced0d93bbda2c7d5b778fc1e9eef472a22e5e0af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B0=D1=80=D0=B0=D1=82=20=D0=A4=D0=B0=D0=B7=D1=8B?= =?UTF-8?q?=D0=BB=D0=BE=D0=B2?= Date: Thu, 14 May 2026 08:43:27 +0000 Subject: [PATCH] add src/main.py --- src/main.py | 104 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 src/main.py diff --git a/src/main.py b/src/main.py new file mode 100644 index 0000000..6b4a61d --- /dev/null +++ b/src/main.py @@ -0,0 +1,104 @@ +"""Main module for task 69dd4221f309a98be0006b2e. + +This module defines a Pydantic model representing a task card and a function +`parse_task_description` that takes a natural‑language description of a +task and returns a validated instance of the model. + +The implementation uses LangChain's structured output parser to guarantee +that the LLM returns data in the expected format. +""" + +from __future__ import annotations + +from dataclasses import dataclass +from typing import List, Optional + +from langchain_core.prompts import PromptTemplate +from langchain_openai import ChatOpenAI +from langchain_core.output_parsers import PydanticOutputParser +from pydantic import BaseModel, Field + +# --------------------------------------------------------------------------- +# Pydantic model +# --------------------------------------------------------------------------- +class TaskCard(BaseModel): + """Structured representation of a task description. + + The field names are chosen to be concise yet expressive. All fields are + optional because the LLM may not mention every piece of information. + """ + + title: Optional[str] = Field(None, description="Short title of the task") + subject: Optional[str] = Field(None, description="Subject or domain of the task") + deadline_hint: Optional[str] = Field( + None, description="Human‑readable hint about the deadline" + ) + deliverable_type: Optional[str] = Field( + None, description="What is expected to be submitted (report, code, etc.)" + ) + grading_hints: Optional[List[str]] = Field( + None, description="List of hints about grading criteria" + ) + +# --------------------------------------------------------------------------- +# LangChain chain +# --------------------------------------------------------------------------- +# LLM – you can change the model name or temperature via environment +# variables or by passing arguments to ChatOpenAI. +llm = ChatOpenAI(temperature=0.0) + +parser = PydanticOutputParser(pydantic_object=TaskCard) + +prompt = PromptTemplate( + template=( + "You are an assistant that extracts structured information from a short " + "task description. Return the data in the following JSON format: " + "{format_instructions}\n\nInput: {input}\nOutput:" + ), + input_variables=["input"], + partial_variables={"format_instructions": parser.get_format_instructions()}, +) + +chain = prompt | llm | parser + +# --------------------------------------------------------------------------- +# Public API +# --------------------------------------------------------------------------- + +def parse_task_description(description: str) -> TaskCard: + """Parse a natural‑language task description. + + Parameters + ---------- + description: str + One‑sentence or short paragraph describing the task. + + Returns + ------- + TaskCard + Validated Pydantic model with extracted fields. + """ + return chain.invoke({"input": description}) + +# --------------------------------------------------------------------------- +# Demo / CLI +# --------------------------------------------------------------------------- +if __name__ == "__main__": + import argparse + import json + + parser_cli = argparse.ArgumentParser(description="Parse a task description.") + parser_cli.add_argument("description", type=str, help="Task description to parse") + args = parser_cli.parse_args() + + card = parse_task_description(args.description) + print("Parsed card:") + print(json.dumps(card.model_dump(), indent=2, ensure_ascii=False)) + print("\nHuman‑readable summary:") + print( + f"Title: {card.title or 'N/A'}\n" + f"Subject: {card.subject or 'N/A'}\n" + f"Deadline hint: {card.deadline_hint or 'N/A'}\n" + f"Deliverable: {card.deliverable_type or 'N/A'}\n" + f"Grading hints: {', '.join(card.grading_hints) if card.grading_hints else 'N/A'}" + )