41 lines
1.8 KiB
Python
41 lines
1.8 KiB
Python
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="Title of the task")
|
|
subject: str = Field(..., description="Subject or domain of the task")
|
|
deadline_hint: str | None = Field(None, description="Short hint about deadline")
|
|
deliverable_type: str = Field(..., description="What to submit: report, code, presentation etc.")
|
|
grading_hints: list[str] = Field(default_factory=list, description="Hints for grading such as completeness, code example")
|
|
|
|
parser = PydanticOutputParser(pydantic_object=TaskCard)
|
|
prompt_template = PromptTemplate(
|
|
template="""
|
|
Given the following informal task description:\n{task_description}\n\nReturn a JSON object with fields: title, subject, deadline_hint, deliverable_type, grading_hints.\n{format_instructions}
|
|
""",
|
|
input_variables=["task_description"],
|
|
partial_variables={"format_instructions": parser.get_format_instructions()},
|
|
)
|
|
|
|
llm = ChatOpenAI(
|
|
model="openai/gpt-oss-20b:free",
|
|
base_url="https://openrouter.ai/api/v1",
|
|
api_key=os.getenv("OPENAI_API_KEY"),
|
|
temperature=0.0,
|
|
)
|
|
|
|
async def main():
|
|
task_desc = "Сдайте к пятнице мини-отчёт по LangChain: 2 страницы, упор на агентов. Оценка: за полноту и за пример кода."
|
|
chain = prompt_template | llm | parser
|
|
result = await chain.ainvoke({"task_description": task_desc})
|
|
print("Parsed object:\n", result)
|
|
print("\nSummary:")
|
|
for key, value in result.items():
|
|
print(f"{key}: {value}")
|
|
|
|
if __name__ == "__main__":
|
|
import asyncio
|
|
asyncio.run(main()) |