Files
task-69dd4221f309a98be0006b2e/main.py
T
2026-05-26 12:23:31 +00:00

45 lines
2.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import os
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="Краткое название задачи")
subject: str = Field(..., description="Предмет или область")
deadline_hint: str = Field(..., description="Краткая подсказка о сроке")
deliverable_type: str = Field(..., description="Тип сдачи: отчёт, код, презентация и т.д.")
grading_hints: list[str] = Field(..., description="Список критериев оценки")
# LLM configuration BroJS
llm = ChatOpenAI(
model="openai/gpt-oss-20b:free",
base_url="https://platform.brojs.ru/jrnl-bh/api/inference/v1",
api_key=os.getenv("JOURNAL_MCP_PAT"),
temperature=0.5,
)
parser = PydanticOutputParser(pydantic_object=TaskCard)
prompt_template = """\nНиже приведена формулировка задания от преподавателя.\nВаша задача – вернуть данные в формате JSON, соответствующем модели TaskCard.\n{format_instructions}\n\nФормулировка: {input_text}\n"""
prompt = PromptTemplate(
template=prompt_template,
input_variables=["input_text"],
partial_variables={"format_instructions": parser.get_format_instructions()},
)
chain = prompt | llm | parser
if __name__ == "__main__":
example = "Сдайте к пятнице мини-отчёт по LangChain: 2 страницы, упор на агентов. Оценка: за полноту и за пример кода."
result = chain.invoke({"input_text": example})
print("\n--- Parsed Result ---")
print(result.model_dump())
print("\n--- Summary ---")
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)}")