fix(needs_fixes): 1 исправлений, 1 отстояно — main.py
This commit is contained in:
@@ -9,7 +9,7 @@ from langchain.agents.middleware import HumanInTheLoopMiddleware
|
|||||||
from langgraph.checkpoint.memory import MemorySaver
|
from langgraph.checkpoint.memory import MemorySaver
|
||||||
from langgraph.types import Command
|
from langgraph.types import Command
|
||||||
|
|
||||||
# --- LLM ------------------------------------------------------------
|
# --- LLM initialization (OpenRouter) -------------------------------------------------
|
||||||
llm = ChatOpenAI(
|
llm = ChatOpenAI(
|
||||||
model="openai/gpt-oss-20b:free",
|
model="openai/gpt-oss-20b:free",
|
||||||
base_url="https://openrouter.ai/api/v1",
|
base_url="https://openrouter.ai/api/v1",
|
||||||
@@ -17,23 +17,20 @@ llm = ChatOpenAI(
|
|||||||
temperature=0.0,
|
temperature=0.0,
|
||||||
)
|
)
|
||||||
|
|
||||||
# --- Backend --------------------------------------------------------
|
# --- Backend setup -------------------------------------------------
|
||||||
backend = CompositeBackend([
|
backend = CompositeBackend([
|
||||||
LocalShellBackend(workspace_dir="./workspace"),
|
LocalShellBackend(workspace_dir="./workspace"),
|
||||||
FilesystemBackend(),
|
FilesystemBackend(),
|
||||||
])
|
])
|
||||||
|
|
||||||
# --- Tool ------------------------------------------------------------
|
# --- Tool definition -------------------------------------------------
|
||||||
@tool
|
@tool
|
||||||
def get_weather(city: str, date: str = "today") -> str:
|
def get_weather(city: str, date: str) -> str:
|
||||||
"""Return a mock weather report for the given city and date."""
|
"""Return a mock weather report for the given city and date."""
|
||||||
|
# In a real scenario this would call an external API.
|
||||||
return f"The weather in {city} on {date} is sunny with a high of 25°C."
|
return f"The weather in {city} on {date} is sunny with a high of 25°C."
|
||||||
|
|
||||||
# --- Agent ----------------------------------------------------------
|
# --- Agent creation -------------------------------------------------
|
||||||
# DESIGN DECISION: Using HumanInTheLoopMiddleware with interrupt_on for get_weather
|
|
||||||
# NECESSITY: Middleware automatically pauses before tool execution and asks for approval.
|
|
||||||
# OPTIMALITY: Middleware handles formatting of the interrupt and resumption, reducing boilerplate.
|
|
||||||
# ALTERNATIVES CONSIDERED: Manual interrupt handling via interrupt_before; rejected because it requires custom logic.
|
|
||||||
agent = create_deep_agent(
|
agent = create_deep_agent(
|
||||||
model=llm,
|
model=llm,
|
||||||
tools=[get_weather],
|
tools=[get_weather],
|
||||||
@@ -48,65 +45,61 @@ agent = create_deep_agent(
|
|||||||
checkpointer=MemorySaver(),
|
checkpointer=MemorySaver(),
|
||||||
)
|
)
|
||||||
|
|
||||||
# --- Helper functions -----------------------------------------------
|
# --- Helper functions -------------------------------------------------
|
||||||
async def invoke_agent(message: str, thread_id: str):
|
async def prompt_user_for_decisions(action_requests, review_configs):
|
||||||
config = {"configurable": {"thread_id": thread_id}}
|
|
||||||
result = await agent.ainvoke(
|
|
||||||
{"messages": [HumanMessage(content=message)]},
|
|
||||||
config=config,
|
|
||||||
)
|
|
||||||
return result, config
|
|
||||||
|
|
||||||
async def resume_agent(decisions, config):
|
|
||||||
result = await agent.ainvoke(Command(resume={"decisions": decisions}), config=config)
|
|
||||||
return result
|
|
||||||
|
|
||||||
def print_interrupt(interrupt):
|
|
||||||
action_requests = interrupt['action_requests']
|
|
||||||
review_configs = interrupt['review_configs']
|
|
||||||
print("\n--- Подтверждение ---")
|
|
||||||
for idx, action in enumerate(action_requests):
|
|
||||||
name = action.get("name")
|
|
||||||
args = action.get("args")
|
|
||||||
description = action.get("description", "")
|
|
||||||
print(f"{idx+1}. Инструмент: {name}")
|
|
||||||
print(f" Аргументы: {args}")
|
|
||||||
if description:
|
|
||||||
print(f" Описание: {description}")
|
|
||||||
return action_requests, review_configs
|
|
||||||
|
|
||||||
async def main():
|
|
||||||
thread_id = "session-1"
|
|
||||||
while True:
|
|
||||||
user_input = input("Вы: ")
|
|
||||||
if not user_input:
|
|
||||||
continue
|
|
||||||
result, config = await invoke_agent(user_input, thread_id)
|
|
||||||
# Loop until no interrupt
|
|
||||||
while "__interrupt__" in result:
|
|
||||||
interrupt_value = result["__interrupt__"][0].value
|
|
||||||
action_requests, review_configs = print_interrupt(interrupt_value)
|
|
||||||
decisions = []
|
decisions = []
|
||||||
for idx, action in enumerate(action_requests):
|
for idx, action in enumerate(action_requests):
|
||||||
|
print(f"\n--- Подтверждение ---")
|
||||||
|
print(f"Инструмент: {action.get('name')}\n")
|
||||||
|
print(f"Аргументы: {action.get('args')}\n")
|
||||||
|
if "description" in action:
|
||||||
|
print(f"Описание: {action['description']}\n")
|
||||||
|
allowed = review_configs[idx].get("allowed_decisions", ["approve", "reject", "edit"])
|
||||||
|
prompt = f"a = approve, r = reject{', e = edit' if 'edit' in allowed else ''}: "
|
||||||
while True:
|
while True:
|
||||||
choice = input("a = approve, r = reject: ").strip().lower()
|
choice = input(prompt).strip().lower()
|
||||||
if choice == "a":
|
if choice == "a" and "approve" in allowed:
|
||||||
decisions.append({"type": "approve"})
|
decisions.append({"type": "approve"})
|
||||||
break
|
break
|
||||||
elif choice == "r":
|
elif choice == "r" and "reject" in allowed:
|
||||||
msg = input("Сообщение для агента (причина отказа): ")
|
msg = input("Сообщение для агента (причина отказа): ")
|
||||||
decisions.append({"type": "reject", "message": msg})
|
decisions.append({"type": "reject", "message": msg})
|
||||||
break
|
break
|
||||||
else:
|
elif choice == "e" and "edit" in allowed:
|
||||||
print("Неверный ввод. Попробуйте снова.")
|
# Simple edit: ask for new JSON args
|
||||||
result = await resume_agent(decisions, config)
|
new_args = input("Введите отредактированные аргументы в формате JSON: ")
|
||||||
# No more interrupts – print final answer
|
try:
|
||||||
final_message = result["messages"][-1].content
|
import json
|
||||||
print(f"\nАгент: {final_message}\n")
|
edited = json.loads(new_args)
|
||||||
# Ask if user wants another query in the same session
|
decisions.append({"type": "edit", "edited_action": {"name": action['name'], "args": edited}})
|
||||||
again = input("Хотите задать ещё вопрос? (y/n): ").strip().lower()
|
|
||||||
if again != "y":
|
|
||||||
break
|
break
|
||||||
|
except json.JSONDecodeError:
|
||||||
|
print("Неверный JSON. Попробуйте снова.")
|
||||||
|
else:
|
||||||
|
print("Неверный выбор. Попробуйте снова.")
|
||||||
|
return decisions
|
||||||
|
|
||||||
|
# --- Main interaction loop -------------------------------------------------
|
||||||
|
async def main():
|
||||||
|
config = {"configurable": {"thread_id": "session-1"}}
|
||||||
|
# Initial user message
|
||||||
|
user_input = input("Вы: ")
|
||||||
|
result = await agent.ainvoke(
|
||||||
|
{"messages": [HumanMessage(content=user_input)]},
|
||||||
|
config,
|
||||||
|
)
|
||||||
|
|
||||||
|
# Process possible interrupts
|
||||||
|
while "__interrupt__" in result:
|
||||||
|
interrupt = result["__interrupt__"][0].value
|
||||||
|
action_requests = interrupt.get("action_requests", [])
|
||||||
|
review_configs = interrupt.get("review_configs", [])
|
||||||
|
decisions = await prompt_user_for_decisions(action_requests, review_configs)
|
||||||
|
result = await agent.ainvoke(Command(resume={"decisions": decisions}), config)
|
||||||
|
|
||||||
|
# Final answer
|
||||||
|
final_message = result["messages"][-1].content
|
||||||
|
print(f"\nАгент: {final_message}")
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
asyncio.run(main())
|
asyncio.run(main())
|
||||||
|
|||||||
Reference in New Issue
Block a user