Quick Start: Travel Assistant
Example of an AI agent using Agent Protocol to book and pay for a flight.
from agent_protocol import AgentProtocol
travel_ai = TravelAI()
# Retrieve the API key from the Agent Protocol website
ap = AgentProtocol(api_key="YOUR_API_KEY")
# The agent developer generates each user a unique Agent Protocol ID
user_ap_id = ...
new_agent = ap.init_agent(
name="TravelAssistant",
user_agent_protocol_id=user_ap_id,
description="An AI agent for booking flights and managing travel"
)
agent_id = new_agent.id
print(f"New AI Travel Assistant created with ID: {agent_id}")
# The agent developer needs to retrieve relevant info
# from the user in order to initialize the agent. The agent developer
# also implements relevant callbacks to handle transaction failures
# and other errors.
await call_agent_developer_init_fns(agent_id)
# User makes a request
user_request = "I need a round trip flight to Japan for 10 days sometime in September"
print(f"User request: {user_request}")
# Travel AI processes the request and selects flight
parsed_request = travel_ai.parse_travel_request(user_request)
available_flights = travel_ai.search_flights(parsed_request)
selected_flight = travel_ai.optimize_itinerary(available_flights, parsed_request)
print(f"Travel AI selected flight: {selected_flight['airline']} for ${selected_flight['price']/100:.2f}")
# The use case filters for the relevant cards
cards = ap.retrieve_authorized_cards(use_case=...)
# Agent developer defined function to purchase the flight
c = cards[0]
txn_id = ap.describe_expense(c, estimated_txn_size, txn_description)
purchase_result = purchase_flight(
c.card_number,
c.exp_date,
c.cvc
)
print(f"Purchase status: {purchase_result['status']}")
if purchase_result['status'] == 'approved':
# The purchase had an issue
issue_report = ap.report_purchase_issue(
card_id=c.id,
transaction_id=purchase_result['transaction_id'],
issue_description="Flight ticket not received after payment"
)
print(f"Issue investigation status: {issue_report['status']}")
if issue_report['status'] != 'resolved':
dispute_result = ap.initiate_buyer_dispute(
card_id=c.id,
transaction_id=purchase_result['transaction_id'],
dispute_reason="Paid for flight ticket but did not receive it"
)
print(f"Dispute initiated. Status: {dispute_result['status']}")Last updated