Files
cucumbers-solutions/solutions/unknown/solution.py
T

57 lines
1.8 KiB
Python

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
# 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,
)
# State definition
class GraphState(TypedDict):
human_value: str | None
# 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"],
}
)
# 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)
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("Неверный выбор. Повторите.")
else:
break
print("\nИтоговое состояние:")
print(state)