Created main.py
This commit is contained in:
@@ -1,104 +1,58 @@
|
|||||||
import os
|
|
||||||
import asyncio
|
import asyncio
|
||||||
from langchain_openai import ChatOpenAI
|
from langchain_openai import ChatOpenAI
|
||||||
|
from langchain_core.messages import HumanMessage
|
||||||
from langchain.tools import tool
|
from langchain.tools import tool
|
||||||
from langchain.agents.middleware import HumanInTheLoopMiddleware
|
from langgraph.checkpoint.memory import MemorySaver
|
||||||
from deepagents import create_deep_agent
|
from langgraph.graph import StateGraph
|
||||||
from deepagents.backends import CompositeBackend, LocalShellBackend, FilesystemBackend
|
|
||||||
from langgraph.types import Command
|
from langgraph.types import Command
|
||||||
|
|
||||||
# ---------- LLM ----------
|
# LLM
|
||||||
llm = ChatOpenAI(
|
llm = ChatOpenAI(model="gpt-4o-mini", base_url="https://openrouter.ai/api/v1", api_key=os.getenv("OPENAI_API_KEY"), temperature=0.0)
|
||||||
model="openai/gpt-oss-20b:free",
|
|
||||||
base_url="https://openrouter.ai/api/v1",
|
|
||||||
api_key=os.getenv("OPENAI_API_KEY"),
|
|
||||||
temperature=0.0,
|
|
||||||
)
|
|
||||||
|
|
||||||
# ---------- Backend ----------
|
# Simple tool
|
||||||
backend = CompositeBackend([
|
|
||||||
LocalShellBackend(workspace_dir="./workspace"),
|
|
||||||
FilesystemBackend(),
|
|
||||||
])
|
|
||||||
|
|
||||||
# ---------- Tool ----------
|
|
||||||
@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 f"Погода в {city} на {date}: солнечно, 25°C"
|
||||||
return f"The weather in {city} on {date} is sunny with a high of 25°C."
|
|
||||||
|
|
||||||
# ---------- Agent ----------
|
# Agent with middleware
|
||||||
agent = create_deep_agent(
|
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=memory,
|
||||||
)
|
)
|
||||||
|
|
||||||
# ---------- Helper to process interrupt ----------
|
|
||||||
async def handle_interrupt(result, config):
|
|
||||||
interrupt = result["__interrupt__"][0].value
|
|
||||||
action_requests = interrupt["action_requests"]
|
|
||||||
review_configs = interrupt["review_configs"]
|
|
||||||
decisions = []
|
|
||||||
for idx, action in enumerate(action_requests):
|
|
||||||
name = action["name"]
|
|
||||||
args = action["args"]
|
|
||||||
description = action.get("description", "")
|
|
||||||
print(f"\n--- Подтверждение ---")
|
|
||||||
print(f"Инструмент: {name}")
|
|
||||||
print(f"Аргументы: {args}")
|
|
||||||
if description:
|
|
||||||
print(f"Описание: {description}")
|
|
||||||
# Determine allowed decisions
|
|
||||||
allowed = review_configs[idx].get("allowed_decisions", ["approve", "reject", "edit"])
|
|
||||||
while True:
|
|
||||||
choice = input(f"a = approve, r = reject{', e = edit' if 'edit' in allowed else ''}: ").strip().lower()
|
|
||||||
if choice == "a" and "approve" in allowed:
|
|
||||||
decisions.append({"type": "approve"})
|
|
||||||
break
|
|
||||||
if choice == "r" and "reject" in allowed:
|
|
||||||
msg = input("Сообщение для агента (причина отказа): ")
|
|
||||||
decisions.append({"type": "reject", "message": msg})
|
|
||||||
break
|
|
||||||
if choice == "e" and "edit" in allowed:
|
|
||||||
# Simple edit: ask for new args as JSON
|
|
||||||
import json
|
|
||||||
new_args = input("Введите отредактированные аргументы в формате JSON: ")
|
|
||||||
try:
|
|
||||||
new_args_dict = json.loads(new_args)
|
|
||||||
decisions.append({"type": "edit", "edited_action": {"name": name, "args": new_args_dict}})
|
|
||||||
break
|
|
||||||
except json.JSONDecodeError:
|
|
||||||
print("Неверный JSON, попробуйте снова.")
|
|
||||||
print("Неверный выбор, попробуйте снова.")
|
|
||||||
# Resume agent
|
|
||||||
return await agent.ainvoke(Command(resume={"decisions": decisions}), config)
|
|
||||||
|
|
||||||
# ---------- Main loop ----------
|
|
||||||
async def main():
|
async def main():
|
||||||
config = {"configurable": {"thread_id": "session-1"}}
|
config = {"configurable": {"thread_id": "сессия-1"}}
|
||||||
while True:
|
result = await agent.ainvoke(
|
||||||
user_input = input("Вы: ")
|
{"messages": [{"role": "human", "content": "Какая погода в Казани сегодня?"}]},
|
||||||
if user_input.lower() in {"exit", "quit"}:
|
config=config,
|
||||||
break
|
)
|
||||||
# Initial invoke
|
# loop for interrupts
|
||||||
result = await agent.ainvoke(
|
while "__interrupt__" in result:
|
||||||
{"messages": [{"role": "human", "content": user_input}]},
|
interrupt = result["__interrupt__"][0].value
|
||||||
config,
|
decisions = []
|
||||||
)
|
for req in interrupt["action_requests"]:
|
||||||
# Process interrupts
|
print(f"Инструмент: {req['name']}")
|
||||||
while "__interrupt__" in result:
|
print(f"Аргументы: {req['args']}")
|
||||||
result = await handle_interrupt(result, config)
|
if "description" in req:
|
||||||
# Final answer
|
print(req["description"])
|
||||||
final_msg = result["messages"][-1].content
|
choice = input("a=approve, r=reject: ")
|
||||||
print(f"\nАгент: {final_msg}\n")
|
if choice.lower() == "a":
|
||||||
|
decisions.append({"type": "approve"})
|
||||||
|
else:
|
||||||
|
msg = input("Причина отказа: ")
|
||||||
|
decisions.append({"type": "reject", "message": msg})
|
||||||
|
result = await agent.ainvoke(Command(resume={"decisions": decisions}), config=config)
|
||||||
|
print("\nОтвет: ", result["messages"][-1]["content"])
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
asyncio.run(main())
|
asyncio.run(main())
|
||||||
|
|||||||
Reference in New Issue
Block a user