add main.py
This commit is contained in:
@@ -1,15 +1,24 @@
|
|||||||
import os
|
import os
|
||||||
from langchain_openai import ChatOpenAI
|
|
||||||
from langchain_core.messages import HumanMessage
|
|
||||||
from langchain_output_parsers import PydanticOutputParser
|
|
||||||
from pydantic import BaseModel, Field
|
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):
|
class TaskCard(BaseModel):
|
||||||
title: str = Field(..., description="Title of the task")
|
title: str = Field(..., description="Title of the task")
|
||||||
subject: str = Field(..., description="Subject area")
|
subject: str = Field(..., description="Subject or domain of the task")
|
||||||
deadline_hint: str | None = Field(None, description="Hint about deadline")
|
deadline_hint: str | None = Field(None, description="Short hint about deadline")
|
||||||
deliverable_type: str = Field(..., description="What to submit (report, code, etc.)")
|
deliverable_type: str = Field(..., description="What to submit: report, code, presentation etc.")
|
||||||
grading_hints: list[str] = Field(default_factory=list, description="Hints for grading")
|
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(
|
llm = ChatOpenAI(
|
||||||
model="openai/gpt-oss-20b:free",
|
model="openai/gpt-oss-20b:free",
|
||||||
@@ -17,21 +26,16 @@ llm = ChatOpenAI(
|
|||||||
api_key=os.getenv("OPENAI_API_KEY"),
|
api_key=os.getenv("OPENAI_API_KEY"),
|
||||||
temperature=0.0,
|
temperature=0.0,
|
||||||
)
|
)
|
||||||
parser = PydanticOutputParser(pydantic_object=TaskCard)
|
|
||||||
prompt_template = """
|
|
||||||
You are a task summarizer.
|
|
||||||
Given the following informal description of an assignment, produce a JSON object matching TaskCard model.
|
|
||||||
|
|
||||||
Description: {description}
|
|
||||||
|
|
||||||
{format_instructions}
|
|
||||||
"""
|
|
||||||
prompt = prompt_template | llm | parser
|
|
||||||
async def main():
|
async def main():
|
||||||
description = "Сдайте к пятнице мини-отчёт по LangChain: 2 страницы, упор на агентов. Оценка: за полноту и за пример кода."
|
task_desc = "Сдайте к пятнице мини-отчёт по LangChain: 2 страницы, упор на агентов. Оценка: за полноту и за пример кода."
|
||||||
result = await prompt.ainvoke({"description": description})
|
chain = prompt_template | llm | parser
|
||||||
print(result["messages"][-1].content)
|
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__":
|
if __name__ == "__main__":
|
||||||
import asyncio
|
import asyncio
|
||||||
asyncio.run(main())
|
asyncio.run(main())
|
||||||
Reference in New Issue
Block a user