add main.py
This commit is contained in:
@@ -1,104 +1,25 @@
|
|||||||
"""Task 69dd4221f309a98be0006b2e – Structured task card parser.
|
import os
|
||||||
|
import json
|
||||||
|
from parser import TaskParser
|
||||||
|
|
||||||
The script accepts a single natural‑language description of a course task and
|
# Example raw texts
|
||||||
returns a validated Pydantic model instance. It demonstrates a one‑shot
|
RAW_TEXTS = [
|
||||||
prompting pattern with a structured output parser.
|
"Напишите мини-отчёт по LangChain до пятницы, 3-5 страниц, критерии: полнота и примеры кода",
|
||||||
|
"Сделать агента на LangGraph который умеет искать в интернете, сдать ссылку на github",
|
||||||
Usage:
|
"Реализовать REST API на FastAPI с авторизацией JWT, покрыть тестами, дедлайн 1 июня",
|
||||||
python main.py "<task description>"
|
]
|
||||||
|
|
||||||
The script prints the raw model dump and a short human‑readable summary.
|
|
||||||
"""
|
|
||||||
|
|
||||||
import sys
|
|
||||||
from typing import List
|
|
||||||
|
|
||||||
from pydantic import BaseModel, Field
|
|
||||||
from langchain_core.prompts import PromptTemplate
|
|
||||||
from langchain_openai import ChatOpenAI
|
|
||||||
from langchain_core.output_parsers import PydanticOutputParser
|
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------
|
|
||||||
# 1. Pydantic model
|
|
||||||
# ---------------------------------------------------------------------------
|
|
||||||
class TaskCard(BaseModel):
|
|
||||||
"""Compact representation of a course task.
|
|
||||||
|
|
||||||
All fields are optional because the model may not be able to infer every
|
|
||||||
piece of information from a very short description. The parser will
|
|
||||||
still return a valid instance – missing values will be ``None``.
|
|
||||||
"""
|
|
||||||
|
|
||||||
title: str | None = Field(None, description="Short title of the task")
|
|
||||||
subject: str | None = Field(None, description="Subject or topic of the task")
|
|
||||||
deadline_hint: str | None = Field(
|
|
||||||
None, description="Free‑form hint about the deadline (e.g. 'к пятнице')"
|
|
||||||
)
|
|
||||||
deliverable_type: str | None = Field(
|
|
||||||
None, description="What is expected to be submitted (report, code, etc.)"
|
|
||||||
)
|
|
||||||
grading_hints: List[str] | None = Field(
|
|
||||||
None, description="List of hints mentioned about grading"
|
|
||||||
)
|
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------
|
|
||||||
# 2. Prompt + chain
|
|
||||||
# ---------------------------------------------------------------------------
|
|
||||||
parser = PydanticOutputParser(pydantic_object=TaskCard)
|
|
||||||
|
|
||||||
prompt_template = """You are a helper that extracts structured information from a
|
|
||||||
course task description. Return a JSON object that matches the following
|
|
||||||
schema:
|
|
||||||
|
|
||||||
{schema}
|
|
||||||
|
|
||||||
The input description is:
|
|
||||||
|
|
||||||
{description}
|
|
||||||
|
|
||||||
Respond ONLY with the JSON object. Do not add any extra text.
|
|
||||||
"""
|
|
||||||
|
|
||||||
prompt = PromptTemplate(
|
|
||||||
template=prompt_template,
|
|
||||||
input_variables=["description"],
|
|
||||||
partial_variables={"schema": parser.get_format_instructions()},
|
|
||||||
)
|
|
||||||
|
|
||||||
# LLM – use the default OpenAI OSS endpoint via environment variables
|
|
||||||
llm = ChatOpenAI(model="openai/gpt-oss-20b:free", temperature=0.2)
|
|
||||||
|
|
||||||
chain = prompt | llm | parser
|
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------
|
|
||||||
# 3. Main entry point
|
|
||||||
# ---------------------------------------------------------------------------
|
|
||||||
|
|
||||||
def main() -> None:
|
def main() -> None:
|
||||||
if len(sys.argv) < 2:
|
parser = TaskParser()
|
||||||
print("Usage: python main.py '<task description>'")
|
cards = parser.batch_parse(RAW_TEXTS)
|
||||||
sys.exit(1)
|
for idx, card in enumerate(cards, start=1):
|
||||||
|
print(f"\n=== Task {idx} ===")
|
||||||
description = sys.argv[1]
|
print(card.to_markdown())
|
||||||
result: TaskCard = chain.invoke({"description": description})
|
filename = f"task_{idx}.json"
|
||||||
|
parser.save_to_file(card, filename)
|
||||||
# Print raw model dump
|
print(f"Saved to {filename}")
|
||||||
print("\n--- Parsed model ---")
|
|
||||||
print(result.model_dump(indent=2))
|
|
||||||
|
|
||||||
# Human‑readable summary
|
|
||||||
print("\n--- Summary ---")
|
|
||||||
print(f"Title: {result.title or 'N/A'}")
|
|
||||||
print(f"Subject: {result.subject or 'N/A'}")
|
|
||||||
print(f"Deadline hint: {result.deadline_hint or 'N/A'}")
|
|
||||||
print(f"Deliverable: {result.deliverable_type or 'N/A'}")
|
|
||||||
if result.grading_hints:
|
|
||||||
print("Grading hints:")
|
|
||||||
for hint in result.grading_hints:
|
|
||||||
print(f"- {hint}")
|
|
||||||
else:
|
|
||||||
print("Grading hints: N/A")
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
|
||||||
|
# End of main.py
|
||||||
|
|||||||
Reference in New Issue
Block a user