question = "Which genre on average has the longest tracks?"config = {"configurable": {"thread_id": "1"}} for step in agent.stream( {"messages": [{"role": "user", "content": question}]}, config, stream_mode="values",): if "messages" in step: step["messages"][-1].pretty_print() elif "__interrupt__" in step: print("INTERRUPTED:") interrupt = step["__interrupt__"][0] for request in interrupt.value["action_requests"]: print(request["description"]) else: pass
复制
向 AI 提问
...INTERRUPTED:Tool execution pending approvalTool: sql_db_queryArgs: {'query': 'SELECT g.Name AS Genre, AVG(t.Milliseconds) AS AvgTrackLength FROM Track t JOIN Genre g ON t.GenreId = g.GenreId GROUP BY g.Name ORDER BY AvgTrackLength DESC LIMIT 1;'}
from langgraph.types import Command for step in agent.stream( Command(resume={"decisions": [{"type": "approve"}]}), config, stream_mode="values",): if "messages" in step: step["messages"][-1].pretty_print() elif "__interrupt__" in step: print("INTERRUPTED:") interrupt = step["__interrupt__"][0] for request in interrupt.value["action_requests"]: print(request["description"]) else: pass
复制
向 AI 提问
================================== Ai Message ==================================Tool Calls: sql_db_query (call_7oz86Epg7lYRqi9rQHbZPS1U) Call ID: call_7oz86Epg7lYRqi9rQHbZPS1U Args: query: SELECT Genre.Name, AVG(Track.Milliseconds) AS AvgDuration FROM Track JOIN Genre ON Track.GenreId = Genre.GenreId GROUP BY Genre.Name ORDER BY AvgDuration DESC LIMIT 5;================================= Tool Message =================================Name: sql_db_query[('Sci Fi & Fantasy', 2911783.0384615385), ('Science Fiction', 2625549.076923077), ('Drama', 2575283.78125), ('TV Shows', 2145041.0215053763), ('Comedy', 1585263.705882353)]================================== Ai Message ==================================The genre with the longest average track length is "Sci Fi & Fantasy" with an average duration of about 2,911,783 milliseconds, followed by "Science Fiction" and "Drama."
import { SystemMessage } from "langchain";const getSystemPrompt = async () => new SystemMessage(`You are a careful SQLite analyst.Authoritative schema (do not invent columns/tables):${await getSchema()}Rules:- Think step-by-step.- When you need data, call the tool \`execute_sql\` with ONE SELECT query.- Read-only only; no INSERT/UPDATE/DELETE/ALTER/DROP/CREATE/REPLACE/TRUNCATE.- Limit to 5 rows unless user explicitly asks otherwise.- If the tool returns 'Error:', revise the SQL and try again.- Limit the number of attempts to 5.- If you are not successful after 5 attempts, return a note to the user.- Prefer explicit column lists; avoid SELECT *.`);
human: Which genre, on average, has the longest tracks?ai:tool: [{"Genre":"Sci Fi & Fantasy","AvgMilliseconds":2911783.0384615385}]ai: Sci Fi & Fantasy — average track length ≈ 48.5 minutes (about 2,911,783 ms).