Удалить solutions/69dd4221f309a98be0006b2e_сырой_текст_задания___плоская_карточка/task_parser.py
This commit is contained in:
-79
@@ -1,79 +0,0 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import List
|
||||
|
||||
from dotenv import load_dotenv
|
||||
from langchain_core.output_parsers import PydanticOutputParser
|
||||
from langchain_core.prompts import PromptTemplate
|
||||
from langchain_openai import ChatOpenAI
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
load_dotenv()
|
||||
|
||||
|
||||
class TaskCard(BaseModel):
|
||||
title: str = Field(
|
||||
...,
|
||||
description="Краткое название задачи",
|
||||
)
|
||||
subject: str | None = Field(
|
||||
None,
|
||||
description="Тема или предмет задачи",
|
||||
)
|
||||
deadline_hint: str | None = Field(
|
||||
None,
|
||||
description="Срок сдачи в свободной форме",
|
||||
)
|
||||
deliverable_type: str | None = Field(
|
||||
None,
|
||||
description="Тип сдаваемого материала: отчёт, код, презентация и т.п.",
|
||||
)
|
||||
grading_hints: List[str] | None = Field(
|
||||
None,
|
||||
description="Ключевые критерии оценки",
|
||||
)
|
||||
|
||||
|
||||
def main() -> None:
|
||||
parser = PydanticOutputParser(pydantic_object=TaskCard)
|
||||
|
||||
template = PromptTemplate(
|
||||
template=(
|
||||
"Прочитай описание учебного задания и извлеки информацию в JSON.\n\n"
|
||||
"Описание:\n{input_text}\n\n"
|
||||
"{format_instructions}"
|
||||
),
|
||||
input_variables=["input_text"],
|
||||
partial_variables={
|
||||
"format_instructions": parser.get_format_instructions()
|
||||
},
|
||||
)
|
||||
|
||||
model = ChatOpenAI(temperature=0)
|
||||
|
||||
chain = template | model | parser
|
||||
|
||||
sample = (
|
||||
"Сдайте к пятнице мини-отчёт по LangChain: 2 страницы, упор на агентов. "
|
||||
"Оценка: за полноту и за пример кода."
|
||||
)
|
||||
|
||||
result: TaskCard = chain.invoke({"input_text": sample})
|
||||
|
||||
print("Валидированный объект:")
|
||||
print(result.model_dump())
|
||||
|
||||
print("\nСводка:")
|
||||
print(f"Задание: {result.title}.")
|
||||
if result.subject:
|
||||
print(f"Тема: {result.subject}.")
|
||||
if result.deadline_hint:
|
||||
print(f"Срок сдачи: {result.deadline_hint}.")
|
||||
if result.deliverable_type:
|
||||
print(f"Тип материала: {result.deliverable_type}.")
|
||||
if result.grading_hints:
|
||||
print(f"Критерии оценки: {', '.join(result.grading_hints)}.")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user