CREATE USER toolbox_user WITH PASSWORD 'my-password';CREATE DATABASE toolbox_db;GRANT ALL PRIVILEGES ON DATABASE toolbox_db TO toolbox_user;ALTER DATABASE toolbox_db OWNER TO toolbox_user;
使用新用户连接到刚创建的数据库:
\c toolbox_db toolbox_user
最后创建 hotels 表并插入数据:
CREATE TABLE hotels( id INTEGER NOT NULL PRIMARY KEY, name VARCHAR NOT NULL, location VARCHAR NOT NULL, price_tier VARCHAR NOT NULL, booked BIT NOT NULL);INSERT INTO hotels(id, name, location, price_tier, booked)VALUES (1, 'Hilton Basel', 'Basel', 'Luxury', B'0'), (2, 'Marriott Zurich', 'Zurich', 'Upscale', B'0'), (3, 'Hyatt Regency Basel', 'Basel', 'Upper Upscale', B'0');
sources: my-pg-source: kind: postgres host: 127.0.0.1 port: 5432 database: toolbox_db user: toolbox_user password: my-passwordtools: search-hotels-by-location: kind: postgres-sql source: my-pg-source description: Search for hotels based on location. parameters: - name: location type: string description: The location of the hotel. statement: SELECT id, name, location, price_tier FROM hotels WHERE location ILIKE '%' || $1 || '%'; book-hotel: kind: postgres-sql source: my-pg-source description: >- Book a hotel by its ID. If the hotel is successfully booked, returns a confirmation message. parameters: - name: hotel_id type: integer description: The ID of the hotel to book. statement: UPDATE hotels SET booked = B'1' WHERE id = $1;toolsets: hotel_toolset: - search-hotels-by-location - book-hotel
from langchain.agents import create_agentfrom langchain_google_vertexai import ChatVertexAIfrom langgraph.checkpoint.memory import MemorySaverfrom toolbox_langchain import ToolboxClientprompt = """You're a helpful hotel assistant. You handle hotel searching and booking.When the user searches for a hotel, list the full details for each hotel found: id, name, location, and price tier.Always use the hotel ID for booking operations.For any bookings, provide a clear confirmation message.Don't ask for clarification or confirmation from the user; perform the requested action directly."""async def run_queries(agent_executor): config = {"configurable": {"thread_id": "hotel-thread-1"}} # --- Query 1: Search for hotels --- query1 = "I need to find a hotel in Basel." print(f'\n--- USER: "{query1}" ---') inputs1 = {"messages": [("user", prompt + query1)]} async for event in agent_executor.astream_events( inputs1, config=config, version="v2" ): if event["event"] == "on_chat_model_end" and event["data"]["output"].content: print(f"--- AGENT: ---\n{event['data']['output'].content}") # --- Query 2: Book a hotel --- query2 = "Great, please book the Hyatt Regency Basel for me." print(f'\n--- USER: "{query2}" ---') inputs2 = {"messages": [("user", query2)]} async for event in agent_executor.astream_events( inputs2, config=config, version="v2" ): if event["event"] == "on_chat_model_end" and event["data"]["output"].content: print(f"--- AGENT: ---\n{event['data']['output'].content}")