Initial implementation of Human-in-the-Loop agent

This commit is contained in:
+62 -83
View File
@@ -1,105 +1,84 @@
import os import json
import asyncio from langchain.agents import create_agent
from langchain_openai import ChatOpenAI
from langchain_core.messages import HumanMessage
from langchain.tools import tool
from deepagents import create_deep_agent
from deepagents.backends import FilesystemBackend, LocalShellBackend, CompositeBackend
from langchain.agents.middleware import HumanInTheLoopMiddleware from langchain.agents.middleware import HumanInTheLoopMiddleware
from langgraph.checkpoint.memory import MemorySaver from langgraph.checkpoint.memory import MemorySaver
from langgraph.types import Command from langchain_openai import ChatOpenAI
from langchain.tools import tool
# --- LLM initialization (OpenRouter) ------------------------------------------------- # Define a simple get_weather tool
llm = ChatOpenAI(
model="openai/gpt-oss-20b:free",
base_url="https://openrouter.ai/api/v1",
api_key=os.getenv("OPENAI_API_KEY"),
temperature=0.0,
)
# --- Backend setup -------------------------------------------------
backend = CompositeBackend([
LocalShellBackend(workspace_dir="./workspace"),
FilesystemBackend(),
])
# --- Tool definition -------------------------------------------------
@tool @tool
def get_weather(city: str, date: str) -> str: def get_weather(city: str, date: str = "today") -> str:
"""Return a mock weather report for the given city and date.""" """Return a mock weather 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 creation ------------------------------------------------- # Initialize LLM (replace with your OpenRouter key if needed)
agent = create_deep_agent( llm = ChatOpenAI(model="gpt-4o", temperature=0)
memory = MemorySaver()
agent = create_agent(
model=llm, model=llm,
tools=[get_weather], tools=[get_weather],
backend=backend, system_prompt="Ты полезный ассистент",
system_prompt="You are a helpful assistant.",
middleware=[ middleware=[
HumanInTheLoopMiddleware( HumanInTheLoopMiddleware(
interrupt_on={"get_weather": True}, interrupt_on={
"get_weather": True,
},
description_prefix="Подтвердите вызов инструмента", description_prefix="Подтвердите вызов инструмента",
), ),
], ],
checkpointer=MemorySaver(), checkpointer=memory,
) )
# --- Helper functions ------------------------------------------------- if __name__ == "__main__":
async def prompt_user_for_decisions(action_requests, review_configs): import sys
decisions = [] from langgraph.types import Command
for idx, action in enumerate(action_requests):
print(f"\n--- Подтверждение ---") # Simple CLI loop
print(f"Инструмент: {action.get('name')}\n") thread_id = "session-1"
print(f"Аргументы: {action.get('args')}\n") config = {"configurable": {"thread_id": thread_id}}
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:
choice = input(prompt).strip().lower()
if choice == "a" and "approve" in allowed:
decisions.append({"type": "approve"})
break
elif choice == "r" and "reject" in allowed:
msg = input("Сообщение для агента (причина отказа): ")
decisions.append({"type": "reject", "message": msg})
break
elif choice == "e" and "edit" in allowed:
# Simple edit: ask for new JSON args
new_args = input("Введите отредактированные аргументы в формате JSON: ")
try:
import json
edited = json.loads(new_args)
decisions.append({"type": "edit", "edited_action": {"name": action['name'], "args": edited}})
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 # Initial user message
user_input = input("Вы: ") user_msg = " ".join(sys.argv[1:]) or "Какая погода в Казани сегодня?"
result = await agent.ainvoke( result = agent.invoke({"messages": [{"role": "human", "content": user_msg}]}, config=config)
{"messages": [HumanMessage(content=user_input)]},
config,
)
# Process possible interrupts
while "__interrupt__" in result: while "__interrupt__" in result:
interrupt = result["__interrupt__"][0].value interrupt = result["__interrupt__"][0].value
action_requests = interrupt.get("action_requests", []) action_requests = interrupt["action_requests"]
review_configs = interrupt.get("review_configs", []) review_configs = interrupt["review_configs"]
decisions = await prompt_user_for_decisions(action_requests, review_configs) decisions = []
result = await agent.ainvoke(Command(resume={"decisions": decisions}), config) for idx, action in enumerate(action_requests):
print(f"\n--- Подтверждение ---")
print(f"Инструмент: {action['name']}")
print(f"Аргументы: {action['args']}")
if "description" in action:
print(f"Описание: {action['description']}")
# Determine allowed decisions
allowed = review_configs.get(action['name'], {}).get("allowed_decisions", ["approve", "reject", "edit"]) if isinstance(review_configs.get(action['name']), dict) else ["approve", "reject", "edit"]
# Simple input handling
while True:
inp = input("a = approve, r = reject, e = edit: ").strip().lower()
if inp == "a" and "approve" in allowed:
decisions.append({"type": "approve"})
break
if inp == "r" and "reject" in allowed:
msg = input("Введите причину отказа: ")
decisions.append({"type": "reject", "message": msg})
break
if inp == "e" and "edit" in allowed:
# For simplicity, ask for new args as JSON
new_args = input("Введите новые аргументы в формате JSON: ")
try:
new_args_dict = json.loads(new_args)
decisions.append({"type": "edit", "edited_action": {"name": action['name'], "args": new_args_dict}})
break
except Exception as exc:
print("Неверный JSON, попробуйте снова.")
print("Недопустимый ввод. Попробуйте снова.")
# Resume
result = agent.invoke(Command(resume={"decisions": decisions}), config=config)
# Final answer # Final answer
final_message = result["messages"][-1].content final_msg = result["messages"][-1]["content"]
print(f"\nАгент: {final_message}") print("\nАгент: " + final_msg)
if __name__ == "__main__":
asyncio.run(main())