52 lines
1.8 KiB
Python
52 lines
1.8 KiB
Python
"""
|
||
Task: Convert raw task text to flat card using LangChain and Pydantic.
|
||
"""
|
||
|
||
from pydantic import BaseModel, Field
|
||
from langchain_core.prompts import PromptTemplate
|
||
from langchain_openai import ChatOpenAI
|
||
from langchain_core.output_parsers import PydanticOutputParser
|
||
|
||
class TaskCard(BaseModel):
|
||
title: str = Field(..., description="Task title")
|
||
subject: str = Field(..., description="Subject or topic of the task")
|
||
deadline_hint: str = Field(..., description="Free‑form deadline hint")
|
||
deliverable_type: str = Field(..., description="What to submit: report, code, presentation, etc.")
|
||
grading_hints: list[str] = Field(..., description="List of grading hints mentioned in the text")
|
||
|
||
# Prompt template
|
||
prompt_template = """
|
||
You are an assistant that extracts structured information from a natural language task description.
|
||
Return the data in the following JSON format:
|
||
{format_instructions}
|
||
|
||
Task description: {task_text}
|
||
"""
|
||
|
||
parser = PydanticOutputParser(pydantic_object=TaskCard)
|
||
prompt = PromptTemplate(
|
||
template=prompt_template,
|
||
input_variables=["task_text"],
|
||
partial_variables={"format_instructions": parser.get_format_instructions()},
|
||
)
|
||
|
||
llm = ChatOpenAI(temperature=0)
|
||
chain = prompt | llm | parser
|
||
|
||
if __name__ == "__main__":
|
||
import sys
|
||
if len(sys.argv) < 2:
|
||
print("Usage: python main.py '<task description>'")
|
||
sys.exit(1)
|
||
task_text = sys.argv[1]
|
||
result = chain.invoke({"task_text": task_text})
|
||
print("Parsed card:\n", result.model_dump(indent=2))
|
||
# Human readable summary
|
||
print("\nSummary:\n")
|
||
print(f"Title: {result.title}")
|
||
print(f"Subject: {result.subject}")
|
||
print(f"Deadline hint: {result.deadline_hint}")
|
||
print(f"Deliverable type: {result.deliverable_type}")
|
||
print(f"Grading hints: {', '.join(result.grading_hints)}")
|
||
"""
|