在快节奏的现代生活中,旅行已成为人们放松身心、拓展视野的重要方式。然而,繁琐的旅行规划常常让人望而却步。面对海量的信息,手动搜索酒店、景点、交通方式,不仅耗时费力,还难以保证行程的个性化和高效性。幸运的是,人工智能(AI)的出现为我们带来了全新的解决方案。本文将介绍如何使用CrewAI、LangChain和Streamlit构建一个AI驱动的旅行助手,让你告别繁琐的手动搜索,轻松规划个性化的完美旅程。
AI旅行助手能够根据你的兴趣、偏好和预算,自动生成详细的旅行计划,包括最佳景点推荐、住宿安排、交通方式、当地美食推荐等。它就像一位贴心的旅行专家,为你量身定制行程,让你省时省力,尽情享受旅行的乐趣。借助CrewAI的多智能体协作能力,LangChain的强大语言模型支持,以及Streamlit的便捷用户界面,我们可以构建一个功能强大、易于使用的AI旅行助手,为用户提供个性化、高效的旅行规划服务。本文将深入探讨这些技术,并提供详细的步骤和示例,帮助你轻松打造属于自己的AI旅行助手,开启智能旅行新时代。
AI旅行助手关键点
- 利用CrewAI、LangChain和Streamlit构建AI驱动的旅行助手
- 告别手动搜索,实现个性化行程规划
- 多个AI智能体协同工作,提供全面旅行建议
- 根据用户兴趣和偏好,量身定制旅行计划
- 生成详细的旅行行程,包括景点、住宿、交通、美食等
- 使用Streamlit构建用户友好的交互界面
- 无需API密钥,轻松部署Llama 3.2模型
- 自动生成包含行程、住宿和本地餐饮推荐的Markdown报告
AI旅行助手构建指南
项目介绍:AI驱动的个性化旅行规划
你是否厌倦了每次旅行前都要花费大量时间在网上搜索信息,比较酒店和景点?现在,有了AI技术的加持,你可以轻松拥有一位个性化的旅行助手。这个旅行助手将结合CrewAI的智能代理协作能力、LangChain的语言模型处理能力以及Streamlit的用户界面构建能力,为你打造完美的旅行计划。
CrewAI是一个框架,用于编排由自主代理组成的AI智能体。它允许你定义不同的角色,每个角色都配备特定的工具和目标,然后让这些角色协同工作,共同完成复杂的任务。在我们的旅行助手项目中,我们将创建多个智能体,例如:
- 旅行信息收集专家:负责搜集目的地城市的景点、住宿、交通等信息。
- 旅行行程规划师:负责根据用户偏好和预算,制定个性化的旅行行程。
- 当地美食推荐专家:负责搜寻当地特色美食,为用户提供美食建议。
LangChain是一个强大的语言模型框架,可以帮助我们处理自然语言,并从文本中提取信息。我们将使用LangChain与AI模型进行交互,例如Llama 3.2,以便根据用户的输入生成旅行建议。
Streamlit是一个简单易用的Python库,用于快速构建交互式Web应用程序。我们将使用Streamlit创建一个用户友好的界面,用户可以在此输入他们的旅行信息,并查看AI旅行助手生成的行程计划。
通过结合这三个强大的工具,我们将创建一个AI旅行助手,它可以自动完成以下任务:
- 搜集旅行信息:自动搜索目的地城市的景点、住宿、交通等信息。
- 制定个性化行程:根据用户的兴趣、偏好和预算,生成详细的旅行计划。
- 提供当地美食推荐:搜寻当地特色美食,为用户提供美食建议。
- 生成旅行报告:将所有信息整合到一份易于阅读的Markdown报告中。
最终,用户将获得一份包含行程、住宿和本地餐饮推荐的Markdown报告,以及一份详细的旅行计划。让我们一起开始构建这个AI旅行助手,体验智能旅行的便捷与乐趣!
技术选型:CrewAI、LangChain和Streamlit
构建这个AI旅行助手需要以下关键技术:
- CrewAI:用于编排和管理多个AI智能体,实现协同工作。
- LangChain:用于与AI模型(如Llama 3.2)进行交互,处理自然语言并生成旅行建议。
- Streamlit:用于构建用户友好的交互界面,方便用户输入旅行信息和查看结果。
- Ollama:简化本地运行大型语言模型的过程。
这些技术的选择是基于它们各自的优势:CrewAI擅长多智能体协作,LangChain拥有强大的语言模型处理能力,Streamlit则能快速构建用户界面,而Ollama能让你在本地快速运行LLM。
为了确保项目的顺利进行,我们需要安装以下Python库:
pip install crewai crewai_tools langchain langchain_community langchain_ollama streamlit duckduckgo-search
这些库将提供构建AI旅行助手所需的各种功能,包括智能体管理、语言模型交互、用户界面构建和外部信息搜索。
Llama 3.2:本地部署AI大语言模型
要让我们的AI旅行助手能够理解用户的需求并生成旅行建议,我们需要一个强大的语言模型。在这里,我们选择Llama 3.2,这是一个由Meta AI开发的开源大型语言模型。与其他需要API密钥的语言模型不同,Llama 3.2可以在本地部署和运行,无需担心API费用和数据隐私问题。
为了在本地运行Llama 3.2,我们需要使用Ollama,这是一个用于简化大型语言模型部署的工具。Ollama可以帮助我们轻松下载、安装和运行Llama 3.2模型,无需复杂的配置。
首先,访问Ollama官网https://ollama.com/下载并安装适合你操作系统的版本。安装完成后,打开命令行工具,输入以下命令来运行Llama 3.2模型:
ollama run llama3.2
Ollama会自动下载Llama 3.2模型,并在本地启动一个服务器。接下来,我们可以在Python代码中使用LangChain与Llama 3.2模型进行交互。
from crewai import LLM
from langchain_ollama.llms import OllamaLLM
llm = LLM(
model="ollama/llama3.2",
base_url="http://localhost:11434"
)
这段代码将创建一个LangChain的OllamaLLM对象,用于连接到本地运行的Llama 3.2模型。现在,我们就可以使用llm对象来生成旅行建议了。
智能代理(Agents):旅行规划的核心动力
有了强大的语言模型,接下来我们需要创建一些AI智能体,让他们协同工作,完成旅行规划任务。在CrewAI中,每个智能体都有自己的角色、目标和工具。
首先,让我们创建一个“旅行信息收集专家”智能体:
from crewai import Agent
guide_expert = Agent(
role="City Local Guide Expert",
goal="Provides information on things to do in the city based on the user's interests.",
backstory="""A local expert with a passion for sharing the best experiences and hidden gems of their city.""",
tools=[search_web_tool],
verbose=True,
max_iter=5,
llm=LLM(
model="ollama/llama3.2",
base_url="http://localhost:11434",
),
allow_delegation=False
)
这个智能体的角色是“城市当地旅游专家”,它的目标是根据用户的兴趣提供城市旅游信息。它拥有一个强大的网络搜索工具,可以用来搜集各种旅行信息。
接下来,我们创建一个“旅行行程规划师”智能体:
location_expert = Agent(
role="Travel Trip Expert",
goal="Adapt to the user destination vity language (French if city in French Country. Gather helpful information about to the city and",
backstory="A seasoned traveler who has explored various destinations and knows the ins and outs of travel logistics.",
tools=[search_web_tool],
verbose=True,
max_iter=5,
llm=LLM(
model="ollama/llama3.2",
base_url="http://localhost:11434",
),
allow_delegation=False
)
这个智能体的角色是“旅行行程专家”,它的目标是适应用户目的地城市的语言,收集关于城市和旅行物流的有用信息。同样,它也拥有一个网络搜索工具。
最后,我们创建一个“旅行规划专家”智能体:
planner_expert = Agent(
role="Travel Planning Expert",
goal="Compiles all gathered information to provide a comprehensive travel plan.",
backstory="You are a professional guide with a passion for travel.An organizational wizard who can turn a list of possibilities into a seamless itinerary.",
tools=[search_ web_tool],
verbose=True,
max_iter=5,
llm=LLM(
model="ollama/llama3.2",
base_url="http://localhost:11434",
),
allow_delegation=False
)
这个智能体的角色是“旅行规划专家”,它的目标是编译所有收集到的信息,以提供全面的旅行计划。它同样拥有一个网络搜索工具,并能连接所有Agent。
定义任务(Tasks):智能体工作的蓝图
有了智能体,接下来我们需要定义他们的任务。在CrewAI中,任务是智能体需要完成的具体工作。
首先,我们定义“旅行信息收集”任务:
from crewai import Task
location_task = Task(
description=f"""In French : This task involves a comprehensive data collection process to provide the traveler with essential information about their eir destination. It includes researching and compiling details on various accommodations, ranging from budget-friendly hostels to luxury hotels, as well as estimating the cost of living in the area. The task also covers transportation options, visa requirements, and any travel advisories that may be relevant to the traveler during the trip period. Consider also the weather conditions forecast on the travel dates. and all the events that may be relevant to the traveler during the trip period.
Traveling from: {from_city}
Destination city: {destination_city}
Arrival Date: {date_from}
Departure Date: {date_to}
Follow this rules :1. If the {destination_city} is in a French country : Respond in FRENCH.""",
agent=location_expert,
output_file="city_report.md",
)
这个任务的描述是收集关于目的地城市的各种信息,包括住宿、交通、签证等。我们将这个任务分配给“旅行信息收集专家”智能体,并指定将结果保存到city_report.md
文件中。
接下来,我们定义“旅行行程规划”任务:
guide_task = Task(
description=f"""Tailored to the traveler's personal (interests), this task focuses on creating an engaging and informative guide to the city's attractions.
Destination city: {destination_city}
interests: {interests}
Arrival Date: {date_from}
Departure Date: {date_to}
Follow this rules :1. If the {destination_city} is in a French country : Respond in FRENCH.""",
agent=guide_expert,
output_file="guide_report.md",
)
这个任务的描述是根据用户的兴趣和偏好,创建一个引人入胜的城市旅游指南。我们将这个任务分配给“旅行行程规划师”智能体,并指定将结果保存到guide_report.md
文件中。
最后,我们定义“生成旅行计划”任务:
planner_task = Task(
description=f"""This task synthesizes all collected information into a detaileds introduction to the city (description of city and presentation, in 3 paragraphes) cohesive and practical travel plan. and takes into account the traveler's schedule, preferences, and budget to draft a day-by-day itinerary. The planner also provides insights into the city's layout and transportation system to facilitate easy navigation.
Destination city: {destination_city}
interests: {interests}
Arrival Date: {date_from}
Departure Date: {date_to}
Follow this rules :1. If the {destination_city} is in a French country : Respond in FRENCH.""",
agent=planner_expert,
output_file="travel_plan.md",
)
这个任务的描述是将所有收集到的信息整合到一个详细的旅行计划中。我们将这个任务分配给“旅行规划专家”智能体,并指定将结果保存到travel_plan.md
文件中。
组建团队(Crew):智能体的协作
有了智能体和任务,我们需要将他们组织成一个团队,让他们协同工作,完成旅行规划任务。在CrewAI中,我们可以使用Crew对象来创建一个团队:
from crewai import Crew, Process
crew = Crew(
agents=[location_expert, guide_expert, planner_expert],
tasks=[location_task, guide_task, planner_task],
process=Process.sequential,
full_output=True,
share_crew=False,
verbose=True
)
这段代码将创建一个Crew对象,并将我们之前创建的三个智能体添加到agents列表中。我们还指定了任务列表,并设置了进程类型为Process.sequential
,这意味着智能体将按照顺序执行任务。在这里可以更改顺序,甚至使用并行。
现在,我们可以运行这个Crew,让智能体们开始工作:
result = crew.kickoff()
运行完成后,我们将在travel_plan.md
文件中找到最终的旅行计划。
Streamlit打造用户界面:轻松交互,个性定制
虽然我们已经成功构建了AI旅行助手,但它目前只能在命令行中运行。为了方便用户使用,我们需要使用Streamlit构建一个用户友好的交互界面。
首先,导入Streamlit库:
import streamlit as st
接下来,我们可以使用Streamlit的各种组件来构建界面:
st.title("AI-Powered Trip Planner")
st.markdown("""Plan your next trip with AI!
Enter your travel details below, and our AI-powered travel assistant will create a personalized itinerary:
Best places to visit 🏡 Accommodation & budget planning 🍝 Local food recommendations ✈️ Transportation & visa details 💡""")
from_city = st.text_input("From City", "India")
destination_city = st.text_input("Destination City", "Rome")
date_from = st.date_input("Departure Date")
date_to = st.date_input("Return Date")
interests = st.text_area("Your Interests (e.g., sightseeing, food, adventure)", "sightseeing and good food")
这段代码将创建一个包含标题、描述和输入框的Streamlit应用程序。用户可以在输入框中输入他们的旅行信息,例如出发城市、目的地城市、旅行日期和兴趣爱好。
最后,我们可以添加一个按钮,当用户点击该按钮时,将运行CrewAI的智能体,生成旅行计划:
if st.button("Generate Travel Plan"):
if not from_city or not destination_city or not date_from or not date_to:
st.error("A Please fill in all fields before generating your travel plan.")
else:
st.write("AI is preparing your personalized travel itinerary... Please wait.")
location_task = location_task(
agent=location_expert,
from_city=from_city,
destination_city=destination_city,
date_from=date_from.strftime("%dth March %Y"),
date_to=date_to.strftime("%dth March %Y"),
)
guide_task = guide_task(
agent=guide_expert,
destination_city=destination_city,
interests=interests,
date_from=date_from.strftime("%dth March %Y"),
date_to=date_to.strftime("%dth March %Y"),
)
planner_task = planner_task(
context=context,
agent=planner_expert,
destination_city=destination_city,
interests=interests,
date_from=date_from.strftime("%dth March %Y"),
date_to=date_to.strftime("%dth March %Y"),
)
crew = Crew(
agents=[location_expert, guide_expert, planner_expert],
tasks=[location_task, guide_task, planner_task],
process=Process.sequential,
full_output=True,
share_crew=False,
verbose=True,
)
result = crew.kickoff()
st.write("Your AI-Powered