add src/main.py

This commit is contained in:
2026-05-14 08:43:27 +00:00
parent 9313009fcf
commit ced0d93bbd
+104
View File
@@ -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 naturallanguage 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="Humanreadable 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 naturallanguage task description.
Parameters
----------
description: str
Onesentence 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("\nHumanreadable 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'}"
)