add main.py
This commit is contained in:
@@ -1,5 +1,4 @@
|
|||||||
from pydantic import BaseModel, Field
|
from pydantic import BaseModel, Field
|
||||||
from typing import List
|
|
||||||
from langchain_core.prompts import PromptTemplate
|
from langchain_core.prompts import PromptTemplate
|
||||||
from langchain_openai import ChatOpenAI
|
from langchain_openai import ChatOpenAI
|
||||||
from langchain_core.output_parsers import PydanticOutputParser
|
from langchain_core.output_parsers import PydanticOutputParser
|
||||||
@@ -7,40 +6,31 @@ from langchain_core.output_parsers import PydanticOutputParser
|
|||||||
class TaskCard(BaseModel):
|
class TaskCard(BaseModel):
|
||||||
title: str = Field(..., description="Title of the assignment")
|
title: str = Field(..., description="Title of the assignment")
|
||||||
subject: str = Field(..., description="Subject or topic of the assignment")
|
subject: str = Field(..., description="Subject or topic of the assignment")
|
||||||
deadline_hint: str = Field(..., description="Free‑form deadline hint")
|
deadline_hint: str = Field(..., description="Free‑form hint about deadline")
|
||||||
deliverable_type: str = Field(..., description="What to submit: report, code, presentation, etc.")
|
deliverable_type: str = Field(..., description="What is to be submitted (report, code, presentation, etc.)")
|
||||||
grading_hints: List[str] = Field(..., description="List of grading hints mentioned in the text")
|
grading_hints: list[str] = Field(..., description="List of hints mentioned about grading")
|
||||||
|
|
||||||
parser = PydanticOutputParser(pydantic_object=TaskCard)
|
parser = PydanticOutputParser(pydantic_object=TaskCard)
|
||||||
|
|
||||||
prompt_template = (
|
|
||||||
"You are an assistant that extracts structured information from a short assignment description. "
|
|
||||||
"Return the data in the following JSON format exactly as required by the parser. "
|
|
||||||
"Do not add any extra keys or text.\n"
|
|
||||||
"{format_instructions}\n"
|
|
||||||
"Input: {input_text}\n"
|
|
||||||
"Output:"
|
|
||||||
)
|
|
||||||
|
|
||||||
prompt = PromptTemplate(
|
prompt = PromptTemplate(
|
||||||
template=prompt_template,
|
template="""
|
||||||
|
You are an assistant that extracts structured information from a short assignment description.
|
||||||
|
Return the data in the following JSON format exactly:
|
||||||
|
{format_instructions}
|
||||||
|
|
||||||
|
Input: {input_text}
|
||||||
|
""",
|
||||||
input_variables=["input_text"],
|
input_variables=["input_text"],
|
||||||
partial_variables={"format_instructions": parser.get_format_instructions()},
|
partial_variables={"format_instructions": parser.get_format_instructions()},
|
||||||
)
|
)
|
||||||
|
|
||||||
llm = ChatOpenAI(temperature=0)
|
llm = ChatOpenAI(temperature=0)
|
||||||
|
|
||||||
chain = prompt | llm | parser
|
chain = prompt | llm | parser
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
# Example input
|
sample = "Сдайте к пятнице мини-отчёт по LangChain: 2 страницы, упор на агентов. Оценка: за полноту и за пример кода."
|
||||||
input_text = "Сдайте к пятнице мини-отчёт по LangChain: 2 страницы, упор на агентов. Оценка: за полноту и за пример кода."
|
result = chain.invoke({"input_text": sample})
|
||||||
result = chain.invoke({"input_text": input_text})
|
|
||||||
print("Parsed object:")
|
print("Parsed object:")
|
||||||
print(result.model_dump())
|
print(result.model_dump())
|
||||||
print("\nHuman readable summary:\n")
|
print("\nHuman readable summary:")
|
||||||
print(f"Title: {result.title}")
|
print(f"Title: {result.title}\nSubject: {result.subject}\nDeadline: {result.deadline_hint}\nDeliverable: {result.deliverable_type}\nGrading hints: {', '.join(result.grading_hints)}")
|
||||||
print(f"Subject: {result.subject}")
|
|
||||||
print(f"Deadline: {result.deadline_hint}")
|
|
||||||
print(f"Deliverable: {result.deliverable_type}")
|
|
||||||
print(f"Grading hints: {', '.join(result.grading_hints)}")
|
|
||||||
|
|||||||
Reference in New Issue
Block a user