-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
529 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# Chapter 2: Principles of Agentic Systems\n", | ||
"---\n", | ||
"\n", | ||
"### Implementing Algorithm 1: Travel Booking Assistant Algorithm with Agency and Autonomy\n", | ||
"\n", | ||
"This is a simple Python implementation of Algorithm 1 in Chapter 2." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 17, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"Agent TripPlanner is booking travel from SAN to SEA\n", | ||
"Goal set: Book flight from SAN to SEA\n", | ||
"Knowledge updated with 3 flight options\n", | ||
"Decision made: Selected flight JetBlue\n", | ||
"Booking confirmed: BOOK-JetBlue-TRIPPLANNER\n", | ||
"\n", | ||
"----------- Final Agent State: -----------\n", | ||
"Name: TripPlanner\n", | ||
"Goals: ['Book flight from SAN to SEA']\n", | ||
"Knowledge Base: {'flight_options': [{'airline': 'Delta', 'departure_airport': 'SAN', 'destination_airport': 'SEA', 'flight_number': 'JB204', 'departure_time': datetime.datetime(2024, 9, 26, 4, 8, 40, 745425), 'arrival_time': datetime.datetime(2024, 9, 30, 8, 57, 27, 119850), 'price': 276.94, 'score': 3.610890445583881}, {'airline': 'United', 'departure_airport': 'SAN', 'destination_airport': 'SEA', 'flight_number': 'AA5319', 'departure_time': datetime.datetime(2024, 9, 8, 23, 9, 41, 529017), 'arrival_time': datetime.datetime(2024, 9, 26, 5, 22, 43, 911100), 'price': 275.79, 'score': 3.625947278726567}, {'airline': 'JetBlue', 'departure_airport': 'SAN', 'destination_airport': 'SEA', 'flight_number': 'DL188', 'departure_time': datetime.datetime(2024, 9, 20, 19, 44, 22, 403049), 'arrival_time': datetime.datetime(2024, 9, 26, 1, 50, 45, 132833), 'price': 202.88, 'score': 4.929022082018927}], 'booking_confirmation': 'BOOK-JetBlue-TRIPPLANNER'}\n", | ||
"Booking Confirmation: BOOK-JetBlue-TRIPPLANNER\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"from travel_provider import travel_provider\n", | ||
"from typing import List, Dict, Any\n", | ||
"\n", | ||
"class TravelAgent:\n", | ||
" def __init__(self, name: str):\n", | ||
" self.name = name\n", | ||
" self.goals: List[str] = []\n", | ||
" self.knowledge_base: Dict[str, Any] = {}\n", | ||
"\n", | ||
" def set_goal(self, goal: str):\n", | ||
" \"\"\"Agency: Defining objectives\"\"\"\n", | ||
" self.goals.append(goal)\n", | ||
" print(f\"Goal set: {goal}\")\n", | ||
"\n", | ||
" def update_knowledge(self, departure: str, destination: str):\n", | ||
" \"\"\"Agency: Acquiring information from an API, and scoring\"\"\"\n", | ||
" # Simulating API call to get flight options\n", | ||
" response = travel_provider.flight_lookup(departure, destination)\n", | ||
" if response['status_code'] == 200:\n", | ||
" flight_options = response['flight_options']\n", | ||
" # Simple scoring based on price (lower is better)\n", | ||
" scored_options = [\n", | ||
" {**flight, 'score': 1000 / flight['price']} \n", | ||
" for flight in flight_options\n", | ||
" ]\n", | ||
" self.knowledge_base['flight_options'] = scored_options\n", | ||
" print(f\"Knowledge updated with {len(scored_options)} flight options\")\n", | ||
" else:\n", | ||
" print(\"Failed to fetch flight information\")\n", | ||
"\n", | ||
" def make_decision(self) -> Dict[str, Any]:\n", | ||
" \"\"\"Autonomy: Independent decision-making\"\"\"\n", | ||
" if 'flight_options' not in self.knowledge_base:\n", | ||
" raise ValueError(\"No flight options available for decision-making\")\n", | ||
" best_option = max(self.knowledge_base['flight_options'], key=lambda x: x['score'])\n", | ||
" print(f\"Decision made: Selected flight {best_option['airline']}\")\n", | ||
" return best_option\n", | ||
"\n", | ||
" def book_travel(self, departure: str, destination: str):\n", | ||
" \"\"\"Agency: Execute action on behalf of user\"\"\"\n", | ||
" print(f\"Agent {self.name} is booking travel from {departure} to {destination}\")\n", | ||
" \n", | ||
" self.set_goal(f\"Book flight from {departure} to {destination}\")\n", | ||
" self.update_knowledge(departure, destination)\n", | ||
" \n", | ||
" try:\n", | ||
" best_flight = self.make_decision()\n", | ||
" # Simulating booking process\n", | ||
" booking_confirmation = f\"BOOK-{best_flight['airline']}-{self.name.upper()}\"\n", | ||
" self.knowledge_base['booking_confirmation'] = booking_confirmation\n", | ||
" print(f\"Booking confirmed: {booking_confirmation}\")\n", | ||
" except Exception as e:\n", | ||
" print(f\"Booking failed: {str(e)}\")\n", | ||
"\n", | ||
" return self\n", | ||
"\n", | ||
"# Usage example\n", | ||
"if __name__ == \"__main__\":\n", | ||
" agent = TravelAgent(\"TripPlanner\")\n", | ||
" agent.book_travel(\"SAN\", \"SEA\")\n", | ||
" print(\"\\n----------- Final Agent State: -----------\")\n", | ||
" print(f\"Name: {agent.name}\")\n", | ||
" print(f\"Goals: {agent.goals}\")\n", | ||
" print(f\"Knowledge Base: {agent.knowledge_base}\")\n", | ||
" if 'booking_confirmation' in agent.knowledge_base:\n", | ||
" print(f\"Booking Confirmation: {agent.knowledge_base['booking_confirmation']}\")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "packt_book", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.11.9" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
Oops, something went wrong.