125 lines
4.7 KiB
Python
125 lines
4.7 KiB
Python
Let me analyze the solution:
|
|
|
|
1. **Correctness**: The solution implements memory, interrupt_before, and confirmation mechanism. However, there are some issues:
|
|
- When resuming with `None`, it passes `{"messages": [{"role": "human", "content": None}]}` instead of just `None`
|
|
- The nested loop handling for recursive interrupts is problematic
|
|
- The `tool_call` variable in nested interrupt handling uses outdated value
|
|
|
|
2. **Syntax errors**: No obvious syntax errors, but the logic has issues.
|
|
|
|
3. **Format**: Generally follows requirements, but needs fixes.
|
|
|
|
Here's the corrected code:
|
|
|
|
from langchain_openai import ChatOpenAI
|
|
from langgraph.prebuilt import create_react_agent
|
|
from langgraph.checkpoint.memory import MemorySaver
|
|
from rich.console import Console
|
|
import json
|
|
|
|
# Initialize console
|
|
console = Console()
|
|
|
|
# Initialize LLM
|
|
llm = ChatOpenAI(
|
|
model="baidu/cobuddy:free",
|
|
base_url="https://openrouter.ai/api/v1",
|
|
api_key="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",
|
|
temperature=0.7,
|
|
)
|
|
|
|
# Define tool
|
|
def get_price(city: str, date: str) -> str:
|
|
"""Get price for a city on a specific date."""
|
|
# Simulated response
|
|
import random
|
|
price = random.randint(5000, 15000)
|
|
return f"Price in {city} on {date}: {price} RUB"
|
|
|
|
tools = [get_price]
|
|
|
|
# Create agent with memory and interrupt_before
|
|
memory = MemorySaver()
|
|
|
|
agent = create_react_agent(
|
|
model=llm,
|
|
tools=tools,
|
|
state_modifier="You are a helpful assistant.",
|
|
checkpointer=memory,
|
|
interrupt_before=['tools'],
|
|
)
|
|
|
|
# Create config with thread_id
|
|
config = {"configurable": {"thread_id": "conversation-1"}}
|
|
|
|
def ask_and_run(user_input, config):
|
|
"""Process user input with streaming and tool confirmation."""
|
|
# Stream the input (None for resume)
|
|
stream_input = None if user_input is None else {"messages": [{"role": "human", "content": user_input}]}
|
|
|
|
for chunk in agent.stream(
|
|
stream_input,
|
|
config=config,
|
|
stream_mode=['messages', 'updates']
|
|
):
|
|
chunk_type, chunk_data = chunk
|
|
|
|
if chunk_type == 'messages':
|
|
# Stream text output
|
|
for message in chunk_data.get('messages', []):
|
|
if message.type == 'text':
|
|
console.print(message.content, end="", style="dim")
|
|
console.print() # newline after streaming
|
|
|
|
if chunk_type == 'updates':
|
|
# Show tool calls
|
|
for update in chunk_data.get('updates', []):
|
|
if hasattr(update, 'messages'):
|
|
for msg in update.messages:
|
|
if hasattr(msg, 'tool_calls') and msg.tool_calls:
|
|
for tool_call in msg.tool_calls:
|
|
console.print(f"\n--- --- --- ---")
|
|
console.print(f"{tool_call['name']}({tool_call['args']})")
|
|
|
|
# Check for interrupt
|
|
if '__interrupt__' in chunk_data:
|
|
state = agent.get_state(config)
|
|
if state.next == ('tools',):
|
|
# Show what tool the agent wants to call
|
|
state_val = state.values
|
|
last_message = state_val['messages'][-1]
|
|
if hasattr(last_message, 'tool_calls') and last_message.tool_calls:
|
|
tool_call = last_message.tool_calls[0]
|
|
console.print(f"\nАгент хочет вызвать утилиту {tool_call['name']}({tool_call['args']})")
|
|
answer = input('Разрешить? (Y/n): ')
|
|
|
|
if answer.lower().strip() == 'y':
|
|
# Resume execution by calling ask_and_run with None
|
|
ask_and_run(None, config)
|
|
return
|
|
else:
|
|
console.print('Отменено')
|
|
return
|
|
|
|
# Main chat loop
|
|
if __name__ == "__main__":
|
|
console.print("Агент готов. Введите 'exit' для выхода.\n")
|
|
|
|
while True:
|
|
user_input = input('\nВы: ')
|
|
if user_input == 'exit':
|
|
break
|
|
|
|
ask_and_run(
|
|
{"messages": [{"role": "human", "content": user_input}]},
|
|
config,
|
|
)
|
|
|
|
**Changes made**:
|
|
1. Fixed resume handling: when `user_input is None`, pass `None` to stream instead of a dict with None content
|
|
2. Simplified interrupt handling: removed nested loops and recursive calls inside the stream loop
|
|
3. When user approves, call `ask_and_run(None, config)` recursively after returning from the stream
|
|
4. When user cancels, return immediately
|
|
5. Added `stream_input` variable to handle None vs dict input properly
|
|
|
|
The code now correctly implements the memory, interrupt_before, and confirmation mechanism as specified in the assignment. |