(위 이미지를 클릭하면 이 수업의 비디오를 볼 수 있습니다)
AI 에이전트의 메타인지
메타인지에 관한 수업에 오신 것을 환영합니다! 이 장은 자신의 사고 과정을 성찰하는 방법에 대해 궁금해하는 초보자들을 위해 설계되었습니다. 이 수업이 끝나면 핵심 개념을 이해하고 AI 에이전트 설계에 메타인지를 적용할 수 있는 실용적인 예제를 활용할 수 있게 될 것입니다.
이 수업을 완료하면 다음을 할 수 있습니다:
메타인지는 자신의 사고에 대해 사고하는 고차원적 인지 과정을 말합니다. AI 에이전트의 경우, 이는 자기 인식과 과거 경험을 기반으로 행동을 평가하고 조정할 수 있음을 의미합니다. 메타인지는 “사고에 대한 사고”로, 에이전트형 AI 시스템 개발에서 중요한 개념입니다. 이는 AI 시스템이 자신의 내부 프로세스를 인식하고 모니터링, 조절 및 적응할 수 있게 합니다. 우리가 상황을 읽거나 문제를 바라볼 때 하는 것과 유사합니다. 이러한 자기 인식은 AI 시스템이 더 나은 결정을 내리고 오류를 식별하며 시간이 지남에 따라 성능을 향상시키는 데 도움을 줄 수 있습니다 — 다시 말해 튜링 테스트와 AI가 지배할 것인지에 대한 논쟁으로 되돌아갑니다.
에이전트형 AI 시스템의 맥락에서 메타인지는 다음과 같은 여러 과제를 해결하는 데 도움이 될 수 있습니다:
메타인지, 즉 “사고에 대한 사고”는 자신의 인지 과정을 자기 인식하고 자기 조절하는 고차원적 인지 과정입니다. AI 분야에서 메타인지는 에이전트가 자신의 전략과 행동을 평가하고 조정할 수 있게 하여 문제 해결 및 의사결정 능력을 향상시킵니다. 메타인지를 이해함으로써 더 지능적일 뿐만 아니라 더 적응력 있고 효율적인 AI 에이전트를 설계할 수 있습니다. 진정한 메타인지에서는 AI가 명시적으로 자신의 추론에 대해 추론하는 모습을 보게 됩니다.
예시: “나는 더 저렴한 항공권을 우선시했어… 직항을 놓치고 있을 수 있으니 다시 확인해볼게.”
어떤 경로를 선택했는지 또는 왜 그렇게 선택했는지를 기록합니다.

메타인지 프로세스에 들어가기 전에 AI 에이전트의 기본 구성 요소를 이해하는 것이 중요합니다. AI 에이전트는 일반적으로 다음으로 구성됩니다:
이러한 구성 요소는 특정 작업을 수행할 수 있는 “전문성 단위”를 함께 만듭니다.
예시: 여행을 계획할 뿐만 아니라 실시간 데이터와 과거 고객 여정 경험을 기반으로 경로를 조정하는 에이전트 서비스인 여행 에이전트를 고려해보세요.
여행 계획을 돕는 AI 기반의 여행 에이전트를 설계한다고 상상해보세요. 이 에이전트, “여행 에이전트”는 사용자에게 휴가 계획을 도와줍니다. 메타인지를 통합하려면 여행 에이전트는 자기 인식과 과거 경험을 기반으로 자신의 행동을 평가하고 조정해야 합니다. 메타인지가 어떻게 작용할 수 있는지 살펴보면 다음과 같습니다:
현재 작업은 사용자가 파리로 여행을 계획하도록 돕는 것입니다.
여행 에이전트는 메타인지를 사용하여 성과를 평가하고 과거 경험에서 학습합니다. 예를 들어:
메타인지를 통합할 때 여행 에이전트 코드가 어떻게 보일지에 대한 단순화된 예시는 다음과 같습니다:
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)
# 사용 예시
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)
메타인지를 통합함으로써 여행 에이전트는 보다 개인화되고 정확한 여행 추천을 제공하여 전체 사용자 경험을 향상시킬 수 있습니다.
계획 수립은 AI 에이전트 행동의 중요한 구성 요소입니다. 이는 현재 상태, 자원 및 가능한 장애물을 고려하여 목표를 달성하기 위해 필요한 단계를 개괄하는 것을 포함합니다.
예시: 다음은 여행 에이전트가 사용자의 여행 계획을 효과적으로 돕기 위해 수행해야 할 단계입니다:
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)
# 야유 요청 내의 사용 예
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)
먼저 RAG 도구와 선제적 컨텍스트 로드의 차이를 이해하는 것부터 시작합시다

RAG는 검색 시스템과 생성 모델을 결합합니다. 쿼리가 들어오면 검색 시스템은 외부 소스에서 관련 문서나 데이터를 가져오고, 이 검색된 정보는 생성 모델에 대한 입력을 보강하는 데 사용됩니다. 이는 모델이 보다 정확하고 문맥에 맞는 응답을 생성하는 데 도움이 됩니다.
RAG 시스템에서 에이전트는 지식 기반에서 관련 정보를 검색하고 이를 사용하여 적절한 응답이나 행동을 생성합니다.
교정형 RAG 접근법은 오류를 수정하고 AI 에이전트의 정확도를 향상시키기 위해 RAG 기법을 사용하는 데 중점을 둡니다. 여기에는 다음이 포함됩니다:
웹에서 정보를 검색하여 사용자 질의에 답하는 검색 에이전트를 고려해보세요. 교정형 RAG 접근법은 다음을 포함할 수 있습니다:
교정형 RAG(검색 증강 생성)는 AI가 정보를 검색하고 생성하는 능력을 향상시키는 동시에 부정확성을 교정할 수 있게 합니다. 여행 에이전트가 보다 정확하고 관련성 높은 여행 추천을 제공하기 위해 교정형 RAG 접근법을 사용할 수 있는 방법을 살펴봅시다.
이 접근법은 다음을 포함합니다:
예시:
preferences = {
"destination": "Paris",
"dates": "2025-04-01 to 2025-04-10",
"budget": "moderate",
"interests": ["museums", "cuisine"]
}
예시:
flights = search_flights(preferences)
hotels = search_hotels(preferences)
attractions = search_attractions(preferences)
예시:
itinerary = create_itinerary(flights, hotels, attractions)
print("Suggested Itinerary:", itinerary)
예시:
feedback = {
"liked": ["Louvre Museum"],
"disliked": ["Eiffel Tower (too crowded)"]
}
예시:
if "disliked" in feedback:
preferences["avoid"] = feedback["disliked"]
예시:
new_attractions = search_attractions(preferences)
new_itinerary = create_itinerary(flights, hotels, new_attractions)
print("Updated Itinerary:", new_itinerary)
예시:
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)
여행 에이전트에 교정형 RAG 접근법을 통합한 단순화된 Python 코드 예시는 다음과 같습니다:
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
# 사용 예
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)는 쿼리를 처리하기 전에 관련 컨텍스트나 배경 정보를 모델에 로드하는 것을 포함합니다. 이는 모델이 시작 시점부터 이 정보에 접근할 수 있게 하여, 처리 중에 추가 데이터를 검색할 필요 없이 더 정보에 기반한 응답을 생성하는 데 도움이 될 수 있습니다.
다음은 여행사 애플리케이션을 위한 파이썬에서의 사전 컨텍스트 로드가 어떻게 보일 수 있는지에 대한 단순화된 예입니다:
class TravelAgent:
def __init__(self):
# 인기 목적지와 해당 정보를 미리 로드
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):
# 사전 로드된 컨텍스트에서 목적지 정보를 가져오기
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}."
# 사용 예시
travel_agent = TravelAgent()
print(travel_agent.get_destination_info("Paris"))
print(travel_agent.get_destination_info("Tokyo"))
초기화(__init__ 메서드): TravelAgent 클래스는 파리, 도쿄, 뉴욕, 시드니와 같은 인기 목적지에 대한 정보를 포함한 사전(딕셔너리)을 미리 로드합니다. 이 딕셔너리에는 각 목적지의 국가, 통화, 언어 및 주요 명소와 같은 세부 정보가 포함되어 있습니다.
정보 검색(get_destination_info 메서드): 사용자가 특정 목적지에 대해 문의할 때, get_destination_info 메서드는 미리 로드된 컨텍스트 딕셔너리에서 관련 정보를 가져옵니다.
컨텍스트를 미리 로드하면 여행사 애플리케이션이 외부 소스에서 실시간으로 이 정보를 검색할 필요 없이 사용자 문의에 빠르게 응답할 수 있습니다. 이는 애플리케이션을 더 효율적이고 반응성이 좋게 만듭니다.
목표로 계획을 초기화(bootstrapping)하는 것은 명확한 목표나 원하는 결과를 처음부터 설정하는 것을 포함합니다. 이 목표를 사전에 정의하면 모델이 반복 과정 전반에 걸쳐 이를 지침으로 사용할 수 있습니다. 이렇게 하면 각 반복이 원하는 결과에 더 가까워지도록 하여 프로세스가 더 효율적이고 집중되도록 합니다.
다음은 여행사에서 계획을 반복하기 전에 목표로 여행 계획을 초기화하는 방법의 예입니다:
여행사는 고객을 위해 맞춤형 휴가를 계획하려고 합니다. 목표는 고객의 선호도와 예산을 기반으로 고객 만족도를 극대화하는 여행 일정을 만드는 것입니다.
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']
# 사용 예
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)
초기화(__init__ 메서드): TravelAgent 클래스는 이름, 비용, 활동 유형과 같은 속성을 가진 잠재적 목적지 목록으로 초기화됩니다.
계획 초기화(bootstrap_plan 메서드): 이 메서드는 고객의 선호도와 예산을 기반으로 초기 여행 계획을 생성합니다. 목적지 목록을 반복하면서 고객의 선호도와 예산에 맞는 경우 계획에 추가합니다.
선호도 일치(match_preferences 메서드): 이 메서드는 목적지가 고객의 선호도와 일치하는지 확인합니다.
계획 반복(iterate_plan 메서드): 이 메서드는 고객의 선호도와 예산 제약을 고려하여 계획에 있는 각 목적지를 더 나은 항목으로 교체하려 시도함으로써 초기 계획을 정교화합니다.
비용 계산(calculate_cost 메서드): 이 메서드는 잠재적 신규 목적지를 포함한 현재 계획의 총비용을 계산합니다.
명확한 목표(예: 고객 만족도 극대화)로 계획을 부트스트랩하고 반복적으로 계획을 정교화함으로써, 여행사는 고객의 선호도와 예산에 맞는 맞춤형 최적화된 여행 일정을 만들 수 있습니다. 이 접근 방식은 여행 계획이 처음부터 고객의 선호도와 예산에 부합하도록 하고 각 반복에서 개선되도록 보장합니다.
대형 언어 모델(LLM)은 검색된 문서나 생성된 응답의 관련성 및 품질을 평가하여 재순위 및 스코어링에 사용할 수 있습니다. 작동 방식은 다음과 같습니다:
검색( Retrieval ): 초기 검색 단계에서 쿼리를 기반으로 후보 문서 또는 응답 집합을 가져옵니다.
재순위( Re-ranking ): LLM이 이러한 후보들을 평가하고 관련성 및 품질에 따라 재순위를 매깁니다. 이 단계는 가장 관련성이 높고 품질이 좋은 정보가 먼저 제시되도록 보장합니다.
스코어링( Scoring ): LLM은 각 후보에 대해 관련성 및 품질을 반영한 점수를 부여합니다. 이는 사용자에게 가장 적합한 응답이나 문서를 선택하는 데 도움이 됩니다.
LLM을 재순위 및 스코어링에 활용하면 시스템이 더 정확하고 맥락적으로 적절한 정보를 제공할 수 있어 전반적인 사용자 경험을 향상시킵니다.
다음은 여행사가 사용자 선호도에 따라 여행 목적지를 재순위 및 스코어링하기 위해 대형 언어 모델(LLM)을 사용하는 방법의 예입니다(파이썬):
여행사는 고객의 선호도를 기반으로 최적의 여행 목적지를 추천하려고 합니다. LLM은 목적지들을 재순위 및 스코어링하여 가장 관련성 높은 옵션이 제시되도록 도와줍니다.
다음은 이전 예제를 Azure OpenAI 서비스를 사용하도록 업데이트하는 방법입니다:
import requests
import json
class TravelAgent:
def __init__(self, destinations):
self.destinations = destinations
def get_recommendations(self, preferences, api_key, endpoint):
# Azure OpenAI용 프롬프트를 생성합니다
prompt = self.generate_prompt(preferences)
# 요청을 위한 헤더와 페이로드를 정의합니다
headers = {
'Content-Type': 'application/json',
'Authorization': f'Bearer {api_key}'
}
payload = {
"prompt": prompt,
"max_tokens": 150,
"temperature": 0.7
}
# 재랭킹되고 점수가 매겨진 목적지를 얻기 위해 Azure OpenAI API를 호출합니다
response = requests.post(endpoint, headers=headers, json=payload)
response_data = response.json()
# 추천 항목을 추출하여 반환합니다
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
# 사용 예시
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)
초기화: TravelAgent 클래스는 이름과 설명과 같은 속성을 가진 잠재적 여행 목적지 목록으로 초기화됩니다.
권장사항 받기(get_recommendations 메서드): 이 메서드는 사용자 선호도를 기반으로 Azure OpenAI 서비스에 보낼 프롬프트를 생성하고 Azure OpenAI API에 HTTP POST 요청을 하여 재순위 및 스코어링된 목적지를 얻습니다.
프롬프트 생성(generate_prompt 메서드): 이 메서드는 사용자 선호도와 목적지 목록을 포함하여 Azure OpenAI에 보낼 프롬프트를 구성합니다. 프롬프트는 모델이 제공된 선호도에 따라 목적지를 재순위 및 스코어링하도록 안내합니다.
API 호출: requests 라이브러리를 사용하여 Azure OpenAI API 엔드포인트에 HTTP POST 요청을 합니다. 응답에는 재순위 및 스코어링된 목적지가 포함됩니다.
사용 예: 여행사는 관광 및 다양한 문화에 대한 관심과 같은 사용자 선호도를 수집하고 Azure OpenAI 서비스를 사용하여 여행 목적지에 대한 재순위 및 스코어링된 추천을 받습니다.
your_azure_openai_api_key를 실제 Azure OpenAI API 키로, https://your-endpoint.com/...을 Azure OpenAI 배포의 실제 엔드포인트 URL로 교체해야 합니다.
LLM을 재순위 및 스코어링에 활용함으로써 여행사는 고객에게 더 개인화되고 관련성 높은 여행 추천을 제공하여 전반적인 경험을 향상시킬 수 있습니다.
Retrieval-Augmented Generation(RAG)은 AI 에이전트 개발에서 프롬프트 기법이자 도구가 될 수 있습니다. 두 가지의 차이를 이해하면 프로젝트에서 RAG를 더 효과적으로 활용할 수 있습니다.
무엇인가?
작동 방식:
여행사 예시:
무엇인가?
작동 방식:
여행사 예시:
| 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. |
프롬프트 기법 예시:
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)
도구 예시:
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)는 AI 에이전트 성능의 중요한 측면입니다. 이는 에이전트가 검색하고 생성한 정보가 적절하고 정확하며 사용자에게 유용한지 확인합니다. 다음은 AI 에이전트에서 관련성을 평가하는 방법과 실용적인 예 및 기법입니다.
예시:
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
예시:
def filter_and_rank(items, query):
ranked_items = sorted(items, key=lambda item: relevance_score(item, query), reverse=True)
return ranked_items[:10] # 상위 10개 관련 항목 반환
예시:
def process_query(query):
# 사용자의 쿼리에서 핵심 정보를 추출하기 위해 NLP를 사용하세요
processed_query = nlp(query)
return processed_query
예시:
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
다음은 여행사가 여행 추천의 관련성을 평가하는 방법에 대한 실용적인 예입니다:
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] # 상위 10개의 관련 항목을 반환합니다
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
# 사용 예시
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)
의도를 고려한 검색은 사용자의 쿼리 뒤에 있는 근본적인 목적이나 목표를 이해하고 해석하여 가장 관련성 높고 유용한 정보를 검색하고 생성하는 것을 포함합니다. 이 접근 방식은 단순히 키워드를 일치시키는 것을 넘어 사용자의 실제 요구와 컨텍스트를 파악하는 데 중점을 둡니다.
Travel Agent를 예로 들어 의도를 고려한 검색을 어떻게 구현할 수 있는지 살펴보겠습니다.
사용자 선호도 수집
class Travel_Agent:
def __init__(self):
self.user_preferences = {}
def gather_preferences(self, preferences):
self.user_preferences = preferences
사용자 의도 이해
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):
# 현재 쿼리를 사용자 기록과 결합하여 문맥을 파악합니다
context = {
"current_query": query,
"user_history": user_history
}
return context
검색 및 결과 개인화
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):
# 정보성 의도에 대한 예시 검색 로직
results = search_web(f"best {preferences['interests']} in {preferences['destination']}")
return results
def search_navigation(query):
# 탐색 의도에 대한 예시 검색 로직
results = search_web(query)
return results
def search_transaction(query, preferences):
# 거래성 의도에 대한 예시 검색 로직
results = search_web(f"book {query} to {preferences['destination']}")
return results
def personalize_results(results, user_history):
# 개인화 예시 로직
personalized = [result for result in results if result not in user_history]
return personalized[:10] # 상위 10개의 개인화된 결과 반환
사용 예시
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)
코드 생성 에이전트는 AI 모델을 사용하여 코드를 작성하고 실행하여 복잡한 문제를 해결하고 작업을 자동화합니다.
코드 생성 에이전트는 생성형 AI 모델을 사용하여 코드를 작성하고 실행합니다. 이러한 에이전트는 다양한 프로그래밍 언어로 코드를 생성하고 실행함으로써 복잡한 문제를 해결하고 작업을 자동화하며 유용한 인사이트를 제공합니다.
코드 생성 에이전트를 설계한다고 가정해 보겠습니다. 작동 방식은 다음과 같습니다:
이 예에서는 코드 생성 에이전트인 Travel Agent를 설계하여 코드를 생성하고 실행함으로써 사용자의 여행 계획을 지원합니다. 이 에이전트는 여행 옵션 가져오기, 결과 필터링, 일정 작성 등 작업을 처리할 수 있습니다.
사용자 선호 수집
class Travel_Agent:
def __init__(self):
self.user_preferences = {}
def gather_preferences(self, preferences):
self.user_preferences = preferences
데이터를 가져오기 위한 코드 생성
def generate_code_to_fetch_data(preferences):
# 예: 사용자 선호에 따라 항공편을 검색하는 코드를 생성
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):
# 예: 호텔을 검색하는 코드를 생성
code = f"""
def search_hotels():
import requests
response = requests.get('https://api.example.com/hotels', params={preferences})
return response.json()
"""
return code
생성된 코드 실행
def execute_code(code):
# 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)
일정 생성
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)
피드백 기반 조정
def adjust_based_on_feedback(feedback, preferences):
# 사용자 피드백에 따라 환경설정을 조정
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)
# 업데이트된 환경설정으로 코드를 재생성하고 실행
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)
테이블의 스키마를 기반으로 하는 것은 환경 인식과 추론을 활용하여 쿼리 생성 과정을 향상시킬 수 있습니다.
다음은 이를 수행하는 방법의 예입니다:
다음은 이러한 개념을 통합한 업데이트된 Python 코드 예제입니다:
def adjust_based_on_feedback(feedback, preferences, schema):
# 사용자 피드백에 따라 선호도 조정
if "liked" in feedback:
preferences["favorites"] = feedback["liked"]
if "disliked" in feedback:
preferences["avoid"] = feedback["disliked"]
# 스키마를 기반으로 다른 관련 선호도를 조정하기 위한 추론
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):
# 스키마와 피드백을 기반으로 선호도를 조정하는 맞춤 로직
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):
# 업데이트된 선호도에 따라 항공편 데이터를 가져오는 코드 생성
return f"fetch_flights(preferences={preferences})"
def generate_code_to_fetch_hotels(preferences):
# 업데이트된 선호도에 따라 호텔 데이터를 가져오는 코드 생성
return f"fetch_hotels(preferences={preferences})"
def execute_code(code):
# 코드 실행을 시뮬레이션하고 모의 데이터를 반환
return {"data": f"Executed: {code}"}
def generate_itinerary(flights, hotels, attractions):
# 항공편, 호텔 및 명소를 기반으로 일정 생성
return {"flights": flights, "hotels": hotels, "attractions": attractions}
# 예시 스키마
schema = {
"favorites": {"positive_adjustment": "increase", "negative_adjustment": "decrease", "default": "neutral"},
"avoid": {"positive_adjustment": "decrease", "negative_adjustment": "increase", "default": "neutral"}
}
# 사용 예시
preferences = {"favorites": "sightseeing", "avoid": "crowded places"}
feedback = {"liked": ["Louvre Museum"], "disliked": ["Eiffel Tower (too crowded)"]}
updated_preferences = adjust_based_on_feedback(feedback, preferences, schema)
# 업데이트된 선호도에 따라 코드를 재생성하고 실행
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 딕셔너리는 피드백에 따라 선호를 어떻게 조정해야 하는지 정의합니다. 여기에는 favorites와 avoid와 같은 필드와 해당 조정이 포함되어 있습니다.adjust_based_on_feedback 메서드): 이 메서드는 사용자 피드백과 스키마에 따라 선호도를 조정합니다.adjust_based_on_environment 메서드): 이 메서드는 스키마와 피드백에 따라 조정을 맞춤화합니다.시스템을 환경 인식형으로 만들고 스키마 기반으로 추론하도록 하면 보다 정확하고 관련성 높은 쿼리를 생성하여 더 나은 여행 추천과 개인화된 사용자 경험을 제공할 수 있습니다.
SQL(Structured Query Language)은 데이터베이스와 상호작용하기 위한 강력한 도구입니다. 검색 증강 생성(RAG) 접근의 일부로 사용될 때, SQL은 데이터베이스에서 관련 데이터를 검색하여 AI 에이전트의 응답이나 행동을 정보 제공하고 생성하는 데 활용될 수 있습니다. Travel Agent 맥락에서 SQL을 RAG 기법으로 사용할 수 있는 방법을 살펴보겠습니다.
예시: 데이터 분석 에이전트:
사용자 선호 수집
class Travel_Agent:
def __init__(self):
self.user_preferences = {}
def gather_preferences(self, preferences):
self.user_preferences = preferences
SQL 쿼리 생성
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
SQL 쿼리 실행
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
추천 생성
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)
항공편 쿼리
SELECT * FROM flights WHERE destination='Paris' AND dates='2025-04-01 to 2025-04-10' AND budget='moderate';
호텔 쿼리
SELECT * FROM hotels WHERE destination='Paris' AND budget='moderate';
명소 쿼리
SELECT * FROM attractions WHERE destination='Paris' AND interests='museums, cuisine';
SQL을 검색 증강 생성(RAG) 기법의 일부로 활용함으로써, Travel Agent와 같은 AI 에이전트는 관련 데이터를 동적으로 검색하고 활용하여 정확하고 개인화된 추천을 제공할 수 있습니다.
메타인지 구현을 시연하기 위해, 문제를 해결하는 동안 자신의 의사결정 과정을 반성합니다 에이전트를 만들어 보겠습니다. 이 예에서는 에이전트가 호텔 선택을 최적화하려고 시도한 다음 자신의 추론을 평가하고 오류나 최적이 아닌 선택을 했을 때 전략을 조정하는 시스템을 구축합니다.
이것을 가격과 품질의 조합에 따라 호텔을 선택하는 기본 예제로 시뮬레이션하되, 에이전트가 자신의 결정을 ‘반성’하고 그에 따라 조정하는 방식을 사용합니다.
다음은 예시입니다:
class HotelRecommendationAgent:
def __init__(self):
self.previous_choices = [] # 이전에 선택한 호텔들을 저장합니다
self.corrected_choices = [] # 수정된 선택들을 저장합니다
self.recommendation_strategies = ['cheapest', 'highest_quality'] # 사용 가능한 전략들
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]
# 마지막 선택이 좋았는지 알려주는 사용자 피드백이 있다고 가정해봅시다
user_feedback = self.get_user_feedback(last_choice)
if user_feedback == "bad":
# 이전 선택이 만족스럽지 않으면 전략을 조정합니다
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"
# 호텔 목록(가격 및 품질)을 시뮬레이션합니다
hotels = [
{'name': 'Budget Inn', 'price': 80, 'quality': 6},
{'name': 'Comfort Suites', 'price': 120, 'quality': 8},
{'name': 'Luxury Stay', 'price': 200, 'quality': 9}
]
# 에이전트를 생성합니다
agent = HotelRecommendationAgent()
# 1단계: 에이전트가 "가장 저렴한" 전략을 사용하여 호텔을 추천합니다
recommended_hotel = agent.recommend_hotel(hotels, 'cheapest')
print(f"Recommended hotel (cheapest): {recommended_hotel['name']}")
# 2단계: 에이전트가 선택을 되돌아보고 필요하면 전략을 조정합니다
reflection_result = agent.reflect_on_choice()
print(reflection_result)
# 3단계: 에이전트가 조정된 전략을 사용하여 다시 추천합니다
adjusted_recommendation = agent.recommend_hotel(hotels, 'highest_quality')
print(f"Adjusted hotel recommendation (highest_quality): {adjusted_recommendation['name']}")
여기서 핵심은 에이전트의 능력입니다:
이는 시스템이 내부 피드백에 기반해 추론 과정을 조정할 수 있는 단순한 형태의 메타인지입니다.
메타인지는 AI 에이전트의 기능을 크게 향상시킬 수 있는 강력한 도구입니다. 메타인지적 과정을 통합함으로써 보다 지능적이고 적응력 있으며 효율적인 에이전트를 설계할 수 있습니다. 추가 자료를 활용하여 AI 에이전트의 흥미로운 메타인지 분야를 더 탐구해 보세요.
다른 학습자들과 만나고, 오피스 아워에 참석하며 AI 에이전트 관련 질문에 답을 얻으려면 Microsoft Foundry Discord에 참여하세요.
면책 고지: 이 문서는 AI 번역 서비스 Co-op Translator(https://github.com/Azure/co-op-translator)를 사용하여 번역되었습니다. 정확성을 위해 최선을 다하고 있으나 자동 번역에는 오류나 부정확성이 포함될 수 있음을 유의해 주십시오. 원문(원래 언어로 된 문서)이 권위 있는 출처로 간주되어야 합니다. 중요한 정보의 경우에는 전문 번역가에 의한 번역을 권장합니다. 이 번역의 사용으로 인해 발생하는 오해나 잘못된 해석에 대해서는 당사가 책임을 지지 않습니다.