82 lines
3.4 KiB
Python
82 lines
3.4 KiB
Python
"""
|
||
Main entry point for the "сырой текст задания → плоская карточка" task.
|
||
|
||
The script demonstrates how to:
|
||
1. Define a Pydantic model that represents a parsed task card.
|
||
2. Build a LangChain chain that takes an informal description and returns a validated
|
||
:class:`TaskCard` instance.
|
||
3. Print the raw JSON, the ``model_dump`` representation and a human‑readable summary.
|
||
|
||
The example is intentionally self‑contained – it does not rely on any external files
|
||
and can be executed with:
|
||
|
||
```bash
|
||
pip install -r requirements.txt
|
||
python main.py
|
||
```
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
import os
|
||
from typing import List
|
||
|
||
from langchain_core.prompts import PromptTemplate
|
||
from langchain_openai import ChatOpenAI
|
||
from langchain_core.output_parsers import PydanticOutputParser
|
||
|
||
# Import the model defined in models.py
|
||
from models import TaskCard
|
||
|
||
# ---------------------------------------------------------------------------
|
||
# 1. Configure LLM – use BroJS endpoint via environment variable
|
||
# ---------------------------------------------------------------------------
|
||
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.0,
|
||
)
|
||
|
||
# ---------------------------------------------------------------------------
|
||
# 2. Prepare parser and prompt template
|
||
# ---------------------------------------------------------------------------
|
||
parser = PydanticOutputParser(pydantic_object=TaskCard)
|
||
|
||
prompt_template = PromptTemplate(
|
||
template="""
|
||
You are an assistant that extracts structured information from a short informal task description.
|
||
Return the data in the format specified by the following instructions.
|
||
|
||
Input: {input_text}
|
||
{format_instructions}
|
||
""",
|
||
input_variables=["input_text"],
|
||
partial_variables={"format_instructions": parser.get_format_instructions()},
|
||
)
|
||
|
||
chain = prompt_template | llm | parser
|
||
|
||
# ---------------------------------------------------------------------------
|
||
# 3. Example usage – three different informal descriptions
|
||
# ---------------------------------------------------------------------------
|
||
examples: List[str] = [
|
||
"Сдайте к пятнице мини-отчёт по LangChain: 2 страницы, упор на агентов. Оценка: за полноту и за пример кода.",
|
||
"На следующей неделе подготовьте презентацию о применении RAG в чат‑ботах. Требуется 10 слайдов, оценка – содержание и дизайн.",
|
||
"Разработайте скрипт на Python, который парсит CSV и выводит статистику. Срок: до конца месяца. Оценка: корректность кода и комментарии.",
|
||
]
|
||
|
||
for idx, text in enumerate(examples, 1):
|
||
print(f"\nExample {idx}:\n{text}\n")
|
||
result = chain.invoke({"input_text": text})
|
||
# ``result`` is already a TaskCard instance because of the parser.
|
||
print("Parsed object (model_dump):", result.model_dump())
|
||
print("Human‑readable summary:\n", str(result))
|
||
|
||
# ---------------------------------------------------------------------------
|
||
# 4. If run as script, execute examples
|
||
# ---------------------------------------------------------------------------
|
||
if __name__ == "__main__":
|
||
# The loop above already demonstrates the functionality.
|
||
pass
|
||
" |