From 1a9ea5ff9b4110342b0dc2b5ebc2d716ff1be0fa Mon Sep 17 00:00:00 2001 From: Eugene Yurtsev Date: Mon, 19 May 2025 16:12:48 -0400 Subject: [PATCH] qxqx --- langgraph_tutorials/customer_support/cars.py | 7 ++++- .../customer_support/excursions.py | 6 ++++ .../customer_support/flights.py | 4 +++ .../customer_support/policy.py | 28 ------------------- 4 files changed, 16 insertions(+), 29 deletions(-) diff --git a/langgraph_tutorials/customer_support/cars.py b/langgraph_tutorials/customer_support/cars.py index 255c7fb..5712ff2 100644 --- a/langgraph_tutorials/customer_support/cars.py +++ b/langgraph_tutorials/customer_support/cars.py @@ -1,8 +1,11 @@ from datetime import date, datetime +from langchain_core.tools import tool + from langgraph_tutorials.customer_support.db import DB +@tool def search_car_rentals( location: str | None = None, name: str | None = None, @@ -43,6 +46,7 @@ def search_car_rentals( ] +@tool def book_car_rental(rental_id: int) -> str: """Book a car rental by its ID. @@ -59,6 +63,7 @@ def book_car_rental(rental_id: int) -> str: return f"No car rental found with ID {rental_id}." +@tool def update_car_rental( rental_id: int, start_date: datetime | date | None = None, @@ -93,7 +98,7 @@ def update_car_rental( return f"Car rental {rental_id} successfully updated." return f"No car rental found with ID {rental_id}." - +@tool def cancel_car_rental(rental_id: int) -> str: """Cancel a car rental by its ID. diff --git a/langgraph_tutorials/customer_support/excursions.py b/langgraph_tutorials/customer_support/excursions.py index 11634ea..f7c95dc 100644 --- a/langgraph_tutorials/customer_support/excursions.py +++ b/langgraph_tutorials/customer_support/excursions.py @@ -1,8 +1,11 @@ """Information about excursions.""" +from langchain_core.tools import tool + from langgraph_tutorials.customer_support.db import DB +@tool def search_trip_recommendations( location: str | None = None, name: str | None = None, @@ -43,6 +46,7 @@ def search_trip_recommendations( ] +@tool def book_excursion(recommendation_id: int) -> str: """Book an excursion by its recommendation ID. @@ -62,6 +66,7 @@ def book_excursion(recommendation_id: int) -> str: return f"No trip recommendation found with ID {recommendation_id}." +@tool def update_excursion(recommendation_id: int, details: str) -> str: """Update a trip recommendation's details by its ID. @@ -82,6 +87,7 @@ def update_excursion(recommendation_id: int, details: str) -> str: return f"No trip recommendation found with ID {recommendation_id}." +@tool def cancel_excursion(recommendation_id: int) -> str: """Cancel a trip recommendation by its ID. diff --git a/langgraph_tutorials/customer_support/flights.py b/langgraph_tutorials/customer_support/flights.py index d82342d..1f872da 100644 --- a/langgraph_tutorials/customer_support/flights.py +++ b/langgraph_tutorials/customer_support/flights.py @@ -36,6 +36,7 @@ def fetch_user_flight_information(config: RunnableConfig) -> list[dict]: return [dict(zip(column_names, row, strict=False)) for row in rows] +@tool def search_flights( departure_airport: str | None = None, arrival_airport: str | None = None, @@ -73,6 +74,7 @@ def search_flights( return [dict(zip(column_names, row, strict=False)) for row in rows] +@tool def update_ticket_to_new_flight( ticket_no: str, new_flight_id: int, *, config: RunnableConfig ) -> str: @@ -131,6 +133,7 @@ def update_ticket_to_new_flight( return "Ticket successfully updated to new flight." +@tool def cancel_ticket(ticket_no: str, *, config: RunnableConfig) -> str: """Cancel the user's ticket and remove it from the database.""" configuration = config.get("configurable", {}) @@ -158,3 +161,4 @@ def cancel_ticket(ticket_no: str, *, config: RunnableConfig) -> str: cursor.execute("DELETE FROM ticket_flights WHERE ticket_no = ?", (ticket_no,)) return "Ticket successfully cancelled." + diff --git a/langgraph_tutorials/customer_support/policy.py b/langgraph_tutorials/customer_support/policy.py index 9440705..ee61cc5 100644 --- a/langgraph_tutorials/customer_support/policy.py +++ b/langgraph_tutorials/customer_support/policy.py @@ -10,7 +10,6 @@ import numpy as np import requests from langchain.embeddings import init_embeddings from langchain_core.embeddings import Embeddings -from langchain_core.tools import BaseTool, tool class PolicyRetriever: @@ -89,30 +88,3 @@ class PolicyRetriever: return [ {**self._docs[idx], "similarity": scores[idx]} for idx in top_k_idx_sorted ] - - def as_tool(self, k: int = 2) -> BaseTool: - """Return a LangChain tool for looking up policy information. - - Args: - k (int, optional): Number of top documents to return in the tool. - Defaults to 2. - - Returns: - Callable: A LangChain tool function. - """ - retriever = self # capture self in closure - - @tool - def lookup_policy(query: str) -> str: - """Consult company policies to check whether certain options are permitted. - - Args: - query (str): The user query about policy information. - - Returns: - str: Concatenated content of the most relevant policy documents. - """ - top_docs = retriever.query(query, k=k) - return "\n\n".join(doc["page_content"] for doc in top_docs) - - return lookup_policy