(Klik di pikja insait wey to see video for dis lesson)
Metacognition for AI Agents
Welcome to di lesson wey dey talk about metacognition for AI agents! Dis chapter na for beginners wey dey curious how AI agents fit think about dia own thinking process dem. By di time you finish dis lesson, you go sabi di important tins dem and go fit use beta example wey go help you do metacognition for AI agent design.
After you don complete dis lesson, you go fit:
Metacognition na di big-level thinking wey involve to think about how person dey think. For AI agents, dis mean say dem go fit check and change dia actions based on how dem sabi diaself and wetin dem don experience before. Metacognition, wey be “thinking about thinking,” na beta idea for how to develop agentic AI systems. E mean say AI systems dey aware of dia own inside process dem and fit watch, control, and change dia behavior the correct way. E be like how we dey read di room or check sometin wey get problem. Dis kind self-awareness fit help AI systems make beta decisions, find mistakes, and better dia work over time- e still join di Turing test and di kain talk about whether AI go ready take over.
For agentic AI systems, metacognition fit help solve many wahala like:
Metacognition, or “thinking about thinking,” na big-level thinking process of self-awareness and self-control about your own brain work. For AI matter, metacognition dey enable agents to check and change dia plans and actions, so dem fit solve problems and make better decisions. If you sabi metacognition, you fit create AI agents wey no just smart but also flexible and quick. For true metacognition, you go see AI reason clear about how e reason itself.
Example: “I choose cheaper flights because… I fit dey miss direct flights, so make I check again.”
Keep track of how or why e choose some route.
Metacognition get big role for AI agent design because:

Before we start to talk about metacognitive process, na important to sabi di basic parts of AI agent. AI agent usually get:
Di parts work together make “expertise unit” wey fit perform special work.
Example:
Think about travel agent, di kind agent wey no just dey plan your holiday but e sabi change dia path based on real-time data and past customer journey experience.
Imagine say you dey build travel agent wey AI power dey inside. Dis agent “Travel Agent,” dey help people plan dia vacation. To use metacognition, Travel Agent go need check and change dia moves based on how e sabi diaself and past experience. Na so metacognition fits work:
Di current work na to help user plan trip go Paris.
Travel Agent dey use metacognition to check how e perform and learn from past experience. For example:
Here na small example of how Travel Agent code fit look to use metacognition:
class Travel_Agent:
def __init__(self):
self.user_preferences = {}
self.experience_data = []
def gather_preferences(self, preferences):
self.user_preferences = preferences
def retrieve_information(self):
# Find flights, hotels, and attractions wey match your choice dem
flights = search_flights(self.user_preferences)
hotels = search_hotels(self.user_preferences)
attractions = search_attractions(self.user_preferences)
return flights, hotels, attractions
def generate_recommendations(self):
flights, hotels, attractions = self.retrieve_information()
itinerary = create_itinerary(flights, hotels, attractions)
return itinerary
def adjust_based_on_feedback(self, feedback):
self.experience_data.append(feedback)
# Look feedback well well and change how we go recommend tins for future
self.user_preferences = adjust_preferences(self.user_preferences, feedback)
# How e take dey use am example
travel_agent = Travel_Agent()
preferences = {
"destination": "Paris",
"dates": "2025-04-01 to 2025-04-10",
"budget": "moderate",
"interests": ["museums", "cuisine"]
}
travel_agent.gather_preferences(preferences)
itinerary = travel_agent.generate_recommendations()
print("Suggested Itinerary:", itinerary)
feedback = {"liked": ["Louvre Museum"], "disliked": ["Eiffel Tower (too crowded)"]}
travel_agent.adjust_based_on_feedback(feedback)
With metacognition, Travel Agent fit offer more personalize and correct travel suggestions, make user experience better.
Planning na serious part of AI agent behavior. E mean say you go list steps wey you need do to reach your goal, thinking about current situation, resources, and wahala wey fit show.
Example:
Dis na steps wey Travel Agent go do to help user plan trip well:
class Travel_Agent:
def __init__(self):
self.user_preferences = {}
self.experience_data = []
def gather_preferences(self, preferences):
self.user_preferences = preferences
def retrieve_information(self):
flights = search_flights(self.user_preferences)
hotels = search_hotels(self.user_preferences)
attractions = search_attractions(self.user_preferences)
return flights, hotels, attractions
def generate_recommendations(self):
flights, hotels, attractions = self.retrieve_information()
itinerary = create_itinerary(flights, hotels, attractions)
return itinerary
def adjust_based_on_feedback(self, feedback):
self.experience_data.append(feedback)
self.user_preferences = adjust_preferences(self.user_preferences, feedback)
# Example wey you fit use for booing request
travel_agent = Travel_Agent()
preferences = {
"destination": "Paris",
"dates": "2025-04-01 to 2025-04-10",
"budget": "moderate",
"interests": ["museums", "cuisine"]
}
travel_agent.gather_preferences(preferences)
itinerary = travel_agent.generate_recommendations()
print("Suggested Itinerary:", itinerary)
feedback = {"liked": ["Louvre Museum"], "disliked": ["Eiffel Tower (too crowded)"]}
travel_agent.adjust_based_on_feedback(feedback)
First make we sabi di difference between RAG Tool and Pre-emptive Context Load

RAG na mix of retrieval system plus one generative model. When you ask question, di retrieval system go find important documents or data from somewhere outside, then di info wey e find go help di generative model make better and accurate answers. Dis dey help model generate correct and context-based responses.
For RAG system, di agent go fetch info from knowledge base and use am to give correct answer or actions.
Corrective RAG approach na to use RAG technique to fix mistakes and make AI agents more accurate. Dis approach get:
Think about search agent wey dey find info from web to answer questions. Corrective RAG fit be like:
Corrective RAG (Retrieval-Augmented Generation) dey improve AI power to find and create info, and also fix mistakes. Make we see how Travel Agent fit use Corrective RAG to provide more accurate and correct travel recommendations.
Dis one get:
Example:
preferences = {
"destination": "Paris",
"dates": "2025-04-01 to 2025-04-10",
"budget": "moderate",
"interests": ["museums", "cuisine"]
}
Example:
flights = search_flights(preferences)
hotels = search_hotels(preferences)
attractions = search_attractions(preferences)
Example:
itinerary = create_itinerary(flights, hotels, attractions)
print("Suggested Itinerary:", itinerary)
Example:
feedback = {
"liked": ["Louvre Museum"],
"disliked": ["Eiffel Tower (too crowded)"]
}
Example:
if "disliked" in feedback:
preferences["avoid"] = feedback["disliked"]
Example:
new_attractions = search_attractions(preferences)
new_itinerary = create_itinerary(flights, hotels, new_attractions)
print("Updated Itinerary:", new_itinerary)
Example:
def adjust_preferences(preferences, feedback):
if "liked" in feedback:
preferences["favorites"] = feedback["liked"]
if "disliked" in feedback:
preferences["avoid"] = feedback["disliked"]
return preferences
preferences = adjust_preferences(preferences, feedback)
Here na small Python code example to add Corrective RAG in Travel Agent:
class Travel_Agent:
def __init__(self):
self.user_preferences = {}
self.experience_data = []
def gather_preferences(self, preferences):
self.user_preferences = preferences
def retrieve_information(self):
flights = search_flights(self.user_preferences)
hotels = search_hotels(self.user_preferences)
attractions = search_attractions(self.user_preferences)
return flights, hotels, attractions
def generate_recommendations(self):
flights, hotels, attractions = self.retrieve_information()
itinerary = create_itinerary(flights, hotels, attractions)
return itinerary
def adjust_based_on_feedback(self, feedback):
self.experience_data.append(feedback)
self.user_preferences = adjust_preferences(self.user_preferences, feedback)
new_itinerary = self.generate_recommendations()
return new_itinerary
# Example wey you fit use am
travel_agent = Travel_Agent()
preferences = {
"destination": "Paris",
"dates": "2025-04-01 to 2025-04-10",
"budget": "moderate",
"interests": ["museums", "cuisine"]
}
travel_agent.gather_preferences(preferences)
itinerary = travel_agent.generate_recommendations()
print("Suggested Itinerary:", itinerary)
feedback = {"liked": ["Louvre Museum"], "disliked": ["Eiffel Tower (too crowded)"]}
new_itinerary = travel_agent.adjust_based_on_feedback(feedback)
print("Updated Itinerary:", new_itinerary)
Pre-emptive Context Load mean say you dey load relevant context or background information inside di model before e start process query. Dis one mean say di model don get access to dis information from di start, and dis fit help am generate better response wey get more sense without e need come find extra data during di process.
Dis na simplified example of how pre-emptive context load fit be for travel agent application wey dem write for Python:
class TravelAgent:
def __init__(self):
# Pre-load popular destinations and their information
self.context = {
"Paris": {"country": "France", "currency": "Euro", "language": "French", "attractions": ["Eiffel Tower", "Louvre Museum"]},
"Tokyo": {"country": "Japan", "currency": "Yen", "language": "Japanese", "attractions": ["Tokyo Tower", "Shibuya Crossing"]},
"New York": {"country": "USA", "currency": "Dollar", "language": "English", "attractions": ["Statue of Liberty", "Times Square"]},
"Sydney": {"country": "Australia", "currency": "Dollar", "language": "English", "attractions": ["Sydney Opera House", "Bondi Beach"]}
}
def get_destination_info(self, destination):
# Fetch destination information from pre-loaded context
info = self.context.get(destination)
if info:
return f"{destination}:\nCountry: {info['country']}\nCurrency: {info['currency']}\nLanguage: {info['language']}\nAttractions: {', '.join(info['attractions'])}"
else:
return f"Sorry, we don't have information on {destination}."
# Example usage
travel_agent = TravelAgent()
print(travel_agent.get_destination_info("Paris"))
print(travel_agent.get_destination_info("Tokyo"))
Initialization (__init__ method): Di TravelAgent class dey pre-load dictionary wey get information about popular destinations like Paris, Tokyo, New York, and Sydney. Dis dictionary get details like di country, currency, language, and main attractions for each destination.
Retrieving Information (get_destination_info method): When person ask about one destination, di get_destination_info method go fetch di relevant information from di pre-loaded context dictionary.
By pre-loading di context, di travel agent application fit respond fast to user queries without e need find dis information from outside source during di time. Dis one make di application work well and dey quick.
Bootstrapping plan with goal mean say you start with clear objective or target wey you want achieve. If you define dis goal upfront, di model fit use am as guideline during di iterative process. Dis go help make sure say each time e go dey move closer to di goal wey you want achieve, and di process go dey more efficient and focused.
Dis example show how you fit bootstrap travel plan with goal before you start iterate for travel agent for Python:
One travel agent want plan custom vacation for client. Di goal na to create travel itinerary wey go maximize di client’s satisfaction based on dia preferences and budget.
class TravelAgent:
def __init__(self, destinations):
self.destinations = destinations
def bootstrap_plan(self, preferences, budget):
plan = []
total_cost = 0
for destination in self.destinations:
if total_cost + destination['cost'] <= budget and self.match_preferences(destination, preferences):
plan.append(destination)
total_cost += destination['cost']
return plan
def match_preferences(self, destination, preferences):
for key, value in preferences.items():
if destination.get(key) != value:
return False
return True
def iterate_plan(self, plan, preferences, budget):
for i in range(len(plan)):
for destination in self.destinations:
if destination not in plan and self.match_preferences(destination, preferences) and self.calculate_cost(plan, destination) <= budget:
plan[i] = destination
break
return plan
def calculate_cost(self, plan, new_destination):
return sum(destination['cost'] for destination in plan) + new_destination['cost']
# Example wawid for use
destinations = [
{"name": "Paris", "cost": 1000, "activity": "sightseeing"},
{"name": "Tokyo", "cost": 1200, "activity": "shopping"},
{"name": "New York", "cost": 900, "activity": "sightseeing"},
{"name": "Sydney", "cost": 1100, "activity": "beach"},
]
preferences = {"activity": "sightseeing"}
budget = 2000
travel_agent = TravelAgent(destinations)
initial_plan = travel_agent.bootstrap_plan(preferences, budget)
print("Initial Plan:", initial_plan)
refined_plan = travel_agent.iterate_plan(initial_plan, preferences, budget)
print("Refined Plan:", refined_plan)
Initialization (__init__ method): Di TravelAgent class dey initialized with list of potential destinations, wey each get attributes like name, cost, and type of activity.
Bootstrapping the Plan (bootstrap_plan method): Dis method dey create initial travel plan based on di client’s preferences and budget. E go check each destination for di list and add dem to di plan if dem match di client’s preferences and di plan fit for di budget.
Matching Preferences (match_preferences method): Dis method go check if destination go match di client’s preferences.
Iterating the Plan (iterate_plan method): Dis method dey refine di initial plan by trying swap each destination for better one, based on client’s preferences and budget limit.
Calculating Cost (calculate_cost method): Dis method dey calculate total cost of di current plan, including any new destination wey dem add.
By bootstrap di plan with clear goal (for example, maximize client satisfaction) and iterate to refine di plan, di travel agent fit create travel itinerary wey sure and customize for di client. Dis approach make di travel plan dey align well with di client’s preferences and budget from di start and e go improve each time una iterate.
Large Language Models (LLMs) fit dey used for re-ranking and scoring by evaluating how relevant and good di retrieved documents or generated responses be. Dis na how e dey work:
Retrieval: First step na to fetch candidate documents or responses wey base on di query.
Re-ranking: Di LLM go evaluate all di candidates and rank dem again based on how relevant and good dem be. Dis one make sure say di best and correct information dey come first.
Scoring: Di LLM go give score to each candidate to show how relevant and good each one be. Dis one help select di best response or document for di user.
By using LLM for re-ranking and scoring, di system fit give more accurate and correct information wey match di context well, and dis way di user experience go better.
Example how travel agent fit use Large Language Model (LLM) for re-ranking and scoring travel destinations based on user preferences for Python:
One travel agent want recommend di best travel destinations to client based on dia preferences. Di LLM go help do re-ranking and scoring to make sure only di best options show.
Here how you fit update di previous example to use Azure OpenAI Services:
import requests
import json
class TravelAgent:
def __init__(self, destinations):
self.destinations = destinations
def get_recommendations(self, preferences, api_key, endpoint):
# Make one prompt for the Azure OpenAI
prompt = self.generate_prompt(preferences)
# Set headers and payload for di request
headers = {
'Content-Type': 'application/json',
'Authorization': f'Bearer {api_key}'
}
payload = {
"prompt": prompt,
"max_tokens": 150,
"temperature": 0.7
}
# Call di Azure OpenAI API to get di place dem wey dem rank and score
response = requests.post(endpoint, headers=headers, json=payload)
response_data = response.json()
# Take out and return di recommendations
recommendations = response_data['choices'][0]['text'].strip().split('\n')
return recommendations
def generate_prompt(self, preferences):
prompt = "Here are the travel destinations ranked and scored based on the following user preferences:\n"
for key, value in preferences.items():
prompt += f"{key}: {value}\n"
prompt += "\nDestinations:\n"
for destination in self.destinations:
prompt += f"- {destination['name']}: {destination['description']}\n"
return prompt
# Example of how to use am
destinations = [
{"name": "Paris", "description": "City of lights, known for its art, fashion, and culture."},
{"name": "Tokyo", "description": "Vibrant city, famous for its modernity and traditional temples."},
{"name": "New York", "description": "The city that never sleeps, with iconic landmarks and diverse culture."},
{"name": "Sydney", "description": "Beautiful harbour city, known for its opera house and stunning beaches."},
]
preferences = {"activity": "sightseeing", "culture": "diverse"}
api_key = 'your_azure_openai_api_key'
endpoint = 'https://your-endpoint.com/openai/deployments/your-deployment-name/completions?api-version=2022-12-01'
travel_agent = TravelAgent(destinations)
recommendations = travel_agent.get_recommendations(preferences, api_key, endpoint)
print("Recommended Destinations:")
for rec in recommendations:
print(rec)
Initialization: Di TravelAgent class dey initialized with list of potential travel destinations, wey each get attributes like name and description.
Getting Recommendations (get_recommendations method): Dis method go generate prompt for Azure OpenAI service based on user preferences and go make HTTP POST request to Azure OpenAI API to get re-ranked and scored destinations.
Generating Prompt (generate_prompt method): Dis method dey build prompt for Azure OpenAI, including user preferences and list of destinations. Di prompt dey guide di model to re-rank and score destinations based on di preferences wey you give.
API Call: Dem dey use requests library to make HTTP POST request to Azure OpenAI API. Di response get di re-ranked and scored destinations.
Example Usage: Di travel agent collect user preferences (like interest for sightseeing and diverse culture) and use Azure OpenAI service to get better ranked and scored recommendations for travel destinations.
Make sure say you replace your_azure_openai_api_key with your real Azure OpenAI API key and https://your-endpoint.com/... with di actual endpoint URL for your Azure OpenAI deployment.
By taking advantage of LLM for re-ranking and scoring, di travel agent fit give more personalized and relevant travel recommendations to clients, to boost their overall experience.
Retrieval-Augmented Generation (RAG) fit be both prompting technique and tool for development of AI agents. To sabi di difference between di two go help you use RAG better for your projects.
Wetin e mean?
How e dey work:
Example for Travel Agent:
Wetin e mean?
How e dey work:
Example for Travel Agent:
| Aspect | Prompting Technique | Tool |
|---|---|---|
| Manual vs Automatic | Manual formulation of prompts for each query. | Automated process for retrieval and generation. |
| Control | Offers more control over the retrieval process. | Streamlines and automates the retrieval and generation. |
| Flexibility | Allows for customized prompts based on specific needs. | More efficient for large-scale implementations. |
| Complexity | Requires crafting and tweaking of prompts. | Easier to integrate within an AI agent’s architecture. |
Prompting Technique Example:
def search_museums_in_paris():
prompt = "Find top museums in Paris"
search_results = search_web(prompt)
return search_results
museums = search_museums_in_paris()
print("Top Museums in Paris:", museums)
Tool Example:
class Travel_Agent:
def __init__(self):
self.rag_tool = RAGTool()
def get_museums_in_paris(self):
user_input = "I want to visit museums in Paris."
response = self.rag_tool.retrieve_and_generate(user_input)
return response
travel_agent = Travel_Agent()
museums = travel_agent.get_museums_in_paris()
print("Top Museums in Paris:", museums)
Evaluating relevancy na very important part for AI agent performance. E dey make sure say information wey di agent retrieve and generate dey appropriate, correct, and useful to di user. Make we check how to evaluate relevancy for AI agents, including examples and techniques.
Example:
def relevance_score(item, query):
score = 0
if item['category'] in query['interests']:
score += 1
if item['price'] <= query['budget']:
score += 1
if item['location'] == query['destination']:
score += 1
return score
Example:
def filter_and_rank(items, query):
ranked_items = sorted(items, key=lambda item: relevance_score(item, query), reverse=True)
return ranked_items[:10] # Return di top 10 items wey make sense
Example:
def process_query(query):
# Use NLP to comot main tins from di user question
processed_query = nlp(query)
return processed_query
Example:
def adjust_based_on_feedback(feedback, items):
for item in items:
if item['name'] in feedback['liked']:
item['relevance'] += 1
if item['name'] in feedback['disliked']:
item['relevance'] -= 1
return items
Dis na practical example of how Travel Agent fit evaluate relevancy of travel recommendations:
class Travel_Agent:
def __init__(self):
self.user_preferences = {}
self.experience_data = []
def gather_preferences(self, preferences):
self.user_preferences = preferences
def retrieve_information(self):
flights = search_flights(self.user_preferences)
hotels = search_hotels(self.user_preferences)
attractions = search_attractions(self.user_preferences)
return flights, hotels, attractions
def generate_recommendations(self):
flights, hotels, attractions = self.retrieve_information()
ranked_hotels = self.filter_and_rank(hotels, self.user_preferences)
itinerary = create_itinerary(flights, ranked_hotels, attractions)
return itinerary
def filter_and_rank(self, items, query):
ranked_items = sorted(items, key=lambda item: self.relevance_score(item, query), reverse=True)
return ranked_items[:10] # Return di top 10 tings wey relate
def relevance_score(self, item, query):
score = 0
if item['category'] in query['interests']:
score += 1
if item['price'] <= query['budget']:
score += 1
if item['location'] == query['destination']:
score += 1
return score
def adjust_based_on_feedback(self, feedback, items):
for item in items:
if item['name'] in feedback['liked']:
item['relevance'] += 1
if item['name'] in feedback['disliked']:
item['relevance'] -= 1
return items
# Example how to use am
travel_agent = Travel_Agent()
preferences = {
"destination": "Paris",
"dates": "2025-04-01 to 2025-04-10",
"budget": "moderate",
"interests": ["museums", "cuisine"]
}
travel_agent.gather_preferences(preferences)
itinerary = travel_agent.generate_recommendations()
print("Suggested Itinerary:", itinerary)
feedback = {"liked": ["Louvre Museum"], "disliked": ["Eiffel Tower (too crowded)"]}
updated_items = travel_agent.adjust_based_on_feedback(feedback, itinerary['hotels'])
print("Updated Itinerary with Feedback:", updated_items)
Searching with intent mean say you understand and interpret di underlying purpose or goal behind di user’s query to find and generate the most relevant and useful info. Dis approach pass just matching keywords, e dey try grasp di user’s true needs and context.
Make we use Travel Agent as example to see how searching with intent fit work.
Gathering User Preferences
class Travel_Agent:
def __init__(self):
self.user_preferences = {}
def gather_preferences(self, preferences):
self.user_preferences = preferences
Understanding User Intent
def identify_intent(query):
if "book" in query or "purchase" in query:
return "transactional"
elif "website" in query or "official" in query:
return "navigational"
else:
return "informational"
def analyze_context(query, user_history):
# Join wetin user don yan before with dis query to sabi wetin e mean for inside
context = {
"current_query": query,
"user_history": user_history
}
return context
Search and Personalize Results
def search_with_intent(query, preferences, user_history):
intent = identify_intent(query)
context = analyze_context(query, user_history)
if intent == "informational":
search_results = search_information(query, preferences)
elif intent == "navigational":
search_results = search_navigation(query)
elif intent == "transactional":
search_results = search_transaction(query, preferences)
personalized_results = personalize_results(search_results, user_history)
return personalized_results
def search_information(query, preferences):
# Example search logic for informational intent
results = search_web(f"best {preferences['interests']} in {preferences['destination']}")
return results
def search_navigation(query):
# Example search logic for navigational intent
results = search_web(query)
return results
def search_transaction(query, preferences):
# Example search logic for transactional intent
results = search_web(f"book {query} to {preferences['destination']}")
return results
def personalize_results(results, user_history):
# Example personalization logic
personalized = [result for result in results if result not in user_history]
return personalized[:10] # Return top 10 personalized results
Example Usage
travel_agent = Travel_Agent()
preferences = {
"destination": "Paris",
"interests": ["museums", "cuisine"]
}
travel_agent.gather_preferences(preferences)
user_history = ["Louvre Museum website", "Book flight to Paris"]
query = "best museums in Paris"
results = search_with_intent(query, preferences, user_history)
print("Search Results:", results)
Code generating agents dey use AI models to write and run code, wen dem dey solve complex problems and dey automate tasks.
Code generating agents dey use generative AI models to write and run code. Dem fit solve complex problems, automate tasks, and provide beta insights by generating and running code for different programming languages.
Imagine sey you dey design code generating agent. Dis na how e fit work:
For this example, we go design a code generating agent, Travel Agent, wey go help users plan their travel by generating and running code. Dis agent fit do things like fetch travel options, filter results, and compile itinerary using generative AI.
Gathering User Preferences
class Travel_Agent:
def __init__(self):
self.user_preferences = {}
def gather_preferences(self, preferences):
self.user_preferences = preferences
Generating Code to Fetch Data
def generate_code_to_fetch_data(preferences):
# Example: Make code wey go find flights based on wetin person like
code = f"""
def search_flights():
import requests
response = requests.get('https://api.example.com/flights', params={preferences})
return response.json()
"""
return code
def generate_code_to_fetch_hotels(preferences):
# Example: Make code wey go find hotels
code = f"""
def search_hotels():
import requests
response = requests.get('https://api.example.com/hotels', params={preferences})
return response.json()
"""
return code
Executing Generated Code
def execute_code(code):
# Run the code wey dem generate wit exec
exec(code)
result = locals()
return result
travel_agent = Travel_Agent()
preferences = {
"destination": "Paris",
"dates": "2025-04-01 to 2025-04-10",
"budget": "moderate",
"interests": ["museums", "cuisine"]
}
travel_agent.gather_preferences(preferences)
flight_code = generate_code_to_fetch_data(preferences)
hotel_code = generate_code_to_fetch_hotels(preferences)
flights = execute_code(flight_code)
hotels = execute_code(hotel_code)
print("Flight Options:", flights)
print("Hotel Options:", hotels)
Generating Itinerary
def generate_itinerary(flights, hotels, attractions):
itinerary = {
"flights": flights,
"hotels": hotels,
"attractions": attractions
}
return itinerary
attractions = search_attractions(preferences)
itinerary = generate_itinerary(flights, hotels, attractions)
print("Suggested Itinerary:", itinerary)
Adjusting Based on Feedback
def adjust_based_on_feedback(feedback, preferences):
# Change di settings based on wetin di user talk
if "liked" in feedback:
preferences["favorites"] = feedback["liked"]
if "disliked" in feedback:
preferences["avoid"] = feedback["disliked"]
return preferences
feedback = {"liked": ["Louvre Museum"], "disliked": ["Eiffel Tower (too crowded)"]}
updated_preferences = adjust_based_on_feedback(feedback, preferences)
# Make code again and run am with di new settings
updated_flight_code = generate_code_to_fetch_data(updated_preferences)
updated_hotel_code = generate_code_to_fetch_hotels(updated_preferences)
updated_flights = execute_code(updated_flight_code)
updated_hotels = execute_code(updated_hotel_code)
updated_itinerary = generate_itinerary(updated_flights, updated_hotels, attractions)
print("Updated Itinerary:", updated_itinerary)
Based on how the table schema be fit make query generation better by using environmental awareness and reasoning.
Here na example how you fit do am:
Here be updated Python code example wey join these things:
def adjust_based_on_feedback(feedback, preferences, schema):
# Change how e go be based on wetin user talk
if "liked" in feedback:
preferences["favorites"] = feedback["liked"]
if "disliked" in feedback:
preferences["avoid"] = feedback["disliked"]
# Reasoning based on schema to change other related preferences
for field in schema:
if field in preferences:
preferences[field] = adjust_based_on_environment(feedback, field, schema)
return preferences
def adjust_based_on_environment(feedback, field, schema):
# Custom logic to change preferences based on schema and feedback
if field in feedback["liked"]:
return schema[field]["positive_adjustment"]
elif field in feedback["disliked"]:
return schema[field]["negative_adjustment"]
return schema[field]["default"]
def generate_code_to_fetch_data(preferences):
# Make code to collect flight data based on updated preferences
return f"fetch_flights(preferences={preferences})"
def generate_code_to_fetch_hotels(preferences):
# Make code to collect hotel data based on updated preferences
return f"fetch_hotels(preferences={preferences})"
def execute_code(code):
# Try run code for play and return fake data
return {"data": f"Executed: {code}"}
def generate_itinerary(flights, hotels, attractions):
# Make itinerary based on flights, hotels, and places wey people like
return {"flights": flights, "hotels": hotels, "attractions": attractions}
# Example schema
schema = {
"favorites": {"positive_adjustment": "increase", "negative_adjustment": "decrease", "default": "neutral"},
"avoid": {"positive_adjustment": "decrease", "negative_adjustment": "increase", "default": "neutral"}
}
# Example usage
preferences = {"favorites": "sightseeing", "avoid": "crowded places"}
feedback = {"liked": ["Louvre Museum"], "disliked": ["Eiffel Tower (too crowded)"]}
updated_preferences = adjust_based_on_feedback(feedback, preferences, schema)
# Make code again and run am with updated preferences
updated_flight_code = generate_code_to_fetch_data(updated_preferences)
updated_hotel_code = generate_code_to_fetch_hotels(updated_preferences)
updated_flights = execute_code(updated_flight_code)
updated_hotels = execute_code(updated_hotel_code)
updated_itinerary = generate_itinerary(updated_flights, updated_hotels, feedback["liked"])
print("Updated Itinerary:", updated_itinerary)
schema dictionary dey define how to adjust preferences based on feedback. E get fields like favorites and avoid with their adjustment dem.adjust_based_on_feedback method): This method dey change preferences based on user feedback and the schema.adjust_based_on_environment method): This method dey customize adjustments based on schema and feedback.If system sabi environment well and reason based on schema, e go fit generate more correct and relevant queries, wey go make travel recommendations beta and user experience more personalized.
SQL (Structured Query Language) na strong tool for interacting with databases. When you use am as part of Retrieval-Augmented Generation (RAG) approach, SQL fit collect relevant data from databases to help generate responses or actions for AI agents. Make we check how SQL fit take act as RAG technique inside Travel Agent.
Example: Data analysis agent:
Gathering User Preferences
class Travel_Agent:
def __init__(self):
self.user_preferences = {}
def gather_preferences(self, preferences):
self.user_preferences = preferences
Generating SQL Queries
def generate_sql_query(table, preferences):
query = f"SELECT * FROM {table} WHERE "
conditions = []
for key, value in preferences.items():
conditions.append(f"{key}='{value}'")
query += " AND ".join(conditions)
return query
Executing SQL Queries
import sqlite3
def execute_sql_query(query, database="travel.db"):
connection = sqlite3.connect(database)
cursor = connection.cursor()
cursor.execute(query)
results = cursor.fetchall()
connection.close()
return results
Generating Recommendations
def generate_recommendations(preferences):
flight_query = generate_sql_query("flights", preferences)
hotel_query = generate_sql_query("hotels", preferences)
attraction_query = generate_sql_query("attractions", preferences)
flights = execute_sql_query(flight_query)
hotels = execute_sql_query(hotel_query)
attractions = execute_sql_query(attraction_query)
itinerary = {
"flights": flights,
"hotels": hotels,
"attractions": attractions
}
return itinerary
travel_agent = Travel_Agent()
preferences = {
"destination": "Paris",
"dates": "2025-04-01 to 2025-04-10",
"budget": "moderate",
"interests": ["museums", "cuisine"]
}
travel_agent.gather_preferences(preferences)
itinerary = generate_recommendations(preferences)
print("Suggested Itinerary:", itinerary)
Flight Query
SELECT * FROM flights WHERE destination='Paris' AND dates='2025-04-01 to 2025-04-10' AND budget='moderate';
Hotel Query
SELECT * FROM hotels WHERE destination='Paris' AND budget='moderate';
Attraction Query
SELECT * FROM attractions WHERE destination='Paris' AND interests='museums, cuisine';
By using SQL as part of Retrieval-Augmented Generation (RAG) technique, AI agents like Travel Agent fit dynamically fetch and use relevant data to provide accurate and personalized recommendations.
Make we show implementation of metacognition by creating simple agent wey reflect on how e dey make decisions while e dey solve problem. For this example, we go build system wey agent dey try optimize how e take choose hotel, but e go first evaluate im own reasoning and change strategy when e make mistake or bad choices.
We go simulate dis using simple example where agent dey select hotels based on combination of price and quality, but e go “reflect” on decisions and change am well.
Here na example:
class HotelRecommendationAgent:
def __init__(self):
self.previous_choices = [] # De store di hotels wey dem bin choose before
self.corrected_choices = [] # De store di correct choices
self.recommendation_strategies = ['cheapest', 'highest_quality'] # Strategies wey dey available
def recommend_hotel(self, hotels, strategy):
"""
Recommend a hotel based on the chosen strategy.
The strategy can either be 'cheapest' or 'highest_quality'.
"""
if strategy == 'cheapest':
recommended = min(hotels, key=lambda x: x['price'])
elif strategy == 'highest_quality':
recommended = max(hotels, key=lambda x: x['quality'])
else:
recommended = None
self.previous_choices.append((strategy, recommended))
return recommended
def reflect_on_choice(self):
"""
Reflect on the last choice made and decide if the agent should adjust its strategy.
The agent considers if the previous choice led to a poor outcome.
"""
if not self.previous_choices:
return "No choices made yet."
last_choice_strategy, last_choice = self.previous_choices[-1]
# Make we assume say we get some user feedback wey tell us if di last choice good or no
user_feedback = self.get_user_feedback(last_choice)
if user_feedback == "bad":
# Change strategy if di last choice no satisfy
new_strategy = 'highest_quality' if last_choice_strategy == 'cheapest' else 'cheapest'
self.corrected_choices.append((new_strategy, last_choice))
return f"Reflecting on choice. Adjusting strategy to {new_strategy}."
else:
return "The choice was good. No need to adjust."
def get_user_feedback(self, hotel):
"""
Simulate user feedback based on hotel attributes.
For simplicity, assume if the hotel is too cheap, the feedback is "bad".
If the hotel has quality less than 7, feedback is "bad".
"""
if hotel['price'] < 100 or hotel['quality'] < 7:
return "bad"
return "good"
# Make list of hotels (price and quality) like for example
hotels = [
{'name': 'Budget Inn', 'price': 80, 'quality': 6},
{'name': 'Comfort Suites', 'price': 120, 'quality': 8},
{'name': 'Luxury Stay', 'price': 200, 'quality': 9}
]
# Make one agent
agent = HotelRecommendationAgent()
# Step 1: Di agent dey recommend hotel wit di "cheapest" strategy
recommended_hotel = agent.recommend_hotel(hotels, 'cheapest')
print(f"Recommended hotel (cheapest): {recommended_hotel['name']}")
# Step 2: Di agent dey think for di choice and e change strategy if e need
reflection_result = agent.reflect_on_choice()
print(reflection_result)
# Step 3: Di agent go recommend again, dis time e go use di adjusted strategy
adjusted_recommendation = agent.recommend_hotel(hotels, 'highest_quality')
print(f"Adjusted hotel recommendation (highest_quality): {adjusted_recommendation['name']}")
Wetin important here na the agent ability to:
Dis na simple form of metacognition where system fit adjust reasoning based on internal feedback.
Metacognition na powerful tool wey fit improve how AI agents dey operate. If you add metacognitive processes, you fit design agents wey smarter, adaptable, and efficient. Use the extra materials to sabi more about the beta world of metacognition for AI agents.
Join Microsoft Foundry Discord to meet other learners, attend office hours and make you fit ask all your AI Agents questions.
Disclaimer:
Dis document don translate wit AI translation service Co-op Translator. Even tho we dey try make am correct, abeg sabi say automated translation fit get errors or wahala. Di original document wey e dey for im correct language na di main pipo suppose trust. For important mata, e better make person wey sabi translate am well do am. We no go responsible for any misunderstanding or wrong meaning wey fit happen based on dis translation.