From 3f61e02afd7ce056ec64d0519f6c3534161c2fe6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D1=80=D1=82=D0=B5=D0=BC=20=D0=92=D0=BB=D0=B0=D0=B4?= =?UTF-8?q?=D0=B8=D0=BC=D0=B8=D1=80=D0=BE=D0=B2=D0=B8=D1=87=20=D0=91=D0=B0?= =?UTF-8?q?=D0=B1=D0=B0=D0=B9=D0=BA=D0=B8=D0=BD?= Date: Thu, 28 May 2026 16:04:18 +0000 Subject: [PATCH] feat: solution for unknown --- solutions/unknown/solution.py | 112 ++++++++++++++++++++-------------- 1 file changed, 65 insertions(+), 47 deletions(-) diff --git a/solutions/unknown/solution.py b/solutions/unknown/solution.py index d69a03f..754c744 100644 --- a/solutions/unknown/solution.py +++ b/solutions/unknown/solution.py @@ -1,57 +1,75 @@ from langchain_openai import ChatOpenAI from pydantic import SecretStr -from langgraph.graph import StateGraph, START, END, interrupt -from langgraph.checkpoint.memory import InMemorySaver -from typing import TypedDict +import argparse +import sys -# LLM placeholder -llm = ChatOpenAI( - model="openai/gpt-oss-20b", - base_url='https://platform.brojs.ru/jrnl-bh/api/inference/v1', - api_key=SecretStr("jrnl_30283ab953615cbb6846ff9940a1eedce0b76d7b2f59a2394f29e74643e6a90d"), - temperature=0.7, -) +def parse_args() -> argparse.Namespace: + """ + Parse command line arguments. -# State definition -class GraphState(TypedDict): - human_value: str | None + Returns: + Namespace: Parsed arguments containing the task text. + """ + parser = argparse.ArgumentParser( + description="Run an LLM-based task orchestrator." + ) + parser.add_argument( + "--task-text", + required=True, + help="Text of the task to be processed by the LLM.", + ) + return parser.parse_args() -# Node that triggers an interrupt with a question and options -def ask_node(state: GraphState) -> dict: - return interrupt( - { - "type": "question", - "question": "Выберите вариант:", - "options": ["Опция 1", "Опция 2", "Опция 3"], - } +def validate_task_text(text: str) -> None: + """ + Validate that the provided task text is non-empty. + + Raises: + ValueError: If the text is empty or consists only of whitespace. + """ + if not text.strip(): + raise ValueError("Task text must be a non-empty string.") + +def init_llm() -> ChatOpenAI: + """ + Initialize the LLM client with placeholder configuration. + + Returns: + ChatOpenAI: Configured LLM instance. + """ + return ChatOpenAI( + model="openai/gpt-oss-20b", + base_url='https://platform.brojs.ru/jrnl-bh/api/inference/v1', + api_key=SecretStr("jrnl_30283ab953615cbb6846ff9940a1eedce0b76d7b2f59a2394f29e74643e6a90d"), + temperature=0.7, ) -# Build the graph -builder = StateGraph(GraphState) -builder.add_node("ask", ask_node) -builder.set_entry_point(START) -builder.add_edge(START, "ask") -builder.add_edge("ask", END) +def main() -> None: + """ + Main entry point of the orchestrator. + Parses arguments, validates input, initializes LLM, and prints the response. + """ + args = parse_args() + try: + validate_task_text(args.task_text) + except ValueError as exc: + print(f"Error: {exc}", file=sys.stderr) + sys.exit(1) -graph = builder.compile(checkpointer=InMemorySaver()) - -# Main loop handling interrupts -state: GraphState = {"human_value": None} -while True: - result = graph.invoke(state) - if "__interrupt__" in result: - interrupt_data = result["__interrupt__"] - print(interrupt_data["question"]) - for idx, opt in enumerate(interrupt_data["options"], 1): - print(f"{idx}. {opt}") - choice = input("Выберите номер: ").strip() - try: - selected = interrupt_data["options"][int(choice) - 1] - state["human_value"] = selected - except (ValueError, IndexError): - print("Неверный выбор. Повторите.") + llm = init_llm() + # Invoke the LLM with the task text + response = llm.invoke( + {"messages": [{"role": "human", "content": args.task_text}]} + ) + # The result contains a list of messages; we print the content of the first AI message. + ai_message = next( + (msg for msg in response["messages"] if getattr(msg, "type", None) == "ai"), + None, + ) + if ai_message: + print(ai_message.content) else: - break + print("No AI response received.", file=sys.stderr) -print("\nИтоговое состояние:") -print(state) \ No newline at end of file +if __name__ == "__main__": + main() \ No newline at end of file