LlamaIndex 与 Klavis AI 集成指南¶
本教程展示如何利用 LlamaIndex 的智能体框架结合 Klavis MCP(模型上下文协议)服务器构建功能增强的 AI 智能体。
In [ ]:
Copied!
# Install the required packages
%pip install -qU llama-index llama-index-tools-mcp klavis
# Install the required packages
%pip install -qU llama-index llama-index-tools-mcp klavis
[notice] A new release of pip is available: 25.0 -> 25.1.1 [notice] To update, run: pip install --upgrade pip Note: you may need to restart the kernel to use updated packages.
In [ ]:
Copied!
import os
from klavis import Klavis
from klavis.types import McpServerName, ConnectionType
from llama_index.llms.openai import OpenAI
from llama_index.tools.mcp import BasicMCPClient
# Set environment variables
os.environ[
"OPENAI_API_KEY"
] = "YOUR_OPENAI_API_KEY" # Replace with your actual OpenAI API key
os.environ[
"KLAVIS_API_KEY"
] = "YOUR_KLAVIS_API_KEY" # Replace with your actual Klavis API key
import os
from klavis import Klavis
from klavis.types import McpServerName, ConnectionType
from llama_index.llms.openai import OpenAI
from llama_index.tools.mcp import BasicMCPClient
# Set environment variables
os.environ[
"OPENAI_API_KEY"
] = "YOUR_OPENAI_API_KEY" # Replace with your actual OpenAI API key
os.environ[
"KLAVIS_API_KEY"
] = "YOUR_KLAVIS_API_KEY" # Replace with your actual Klavis API key
步骤 1 - 使用 Klavis 创建 YouTube MCP 服务器¶
In [ ]:
Copied!
klavis_client = Klavis(api_key=os.getenv("KLAVIS_API_KEY"))
# Create a YouTube MCP server and get the server URL
youtube_mcp_instance = klavis_client.mcp_server.create_server_instance(
server_name=McpServerName.YOUTUBE,
user_id="1234",
platform_name="Klavis",
connection_type=ConnectionType.STREAMABLE_HTTP,
)
youtube_mcp_server_url = youtube_mcp_instance.server_url
# print(f"🔗 YouTube MCP server created at: {youtube_mcp_server_url}")
klavis_client = Klavis(api_key=os.getenv("KLAVIS_API_KEY"))
# Create a YouTube MCP server and get the server URL
youtube_mcp_instance = klavis_client.mcp_server.create_server_instance(
server_name=McpServerName.YOUTUBE,
user_id="1234",
platform_name="Klavis",
connection_type=ConnectionType.STREAMABLE_HTTP,
)
youtube_mcp_server_url = youtube_mcp_instance.server_url
# print(f"🔗 YouTube MCP server created at: {youtube_mcp_server_url}")
🔗 YouTube MCP server created at: https://youtube-mcp-server.klavis.ai/sse?instance_id=270cbd51-e737-407d-85ce-6e6162248671
步骤 2 - 使用 Llamaindex 创建基于 MCP 服务器的 AI 代理¶
In [ ]:
Copied!
from llama_index.core.agent.workflow import FunctionAgent
from llama_index.tools.mcp import (
get_tools_from_mcp_url,
aget_tools_from_mcp_url,
)
llm = OpenAI(model="gpt-4o-mini", api_key=os.getenv("OPENAI_API_KEY"))
youtube_tools = await aget_tools_from_mcp_url(
youtube_mcp_server_url, client=BasicMCPClient(youtube_mcp_server_url)
)
youtube_agent = FunctionAgent(
name="youtube_agent",
description="Agent using MCP-based tools",
tools=youtube_tools,
llm=llm,
system_prompt="You are an AI assistant that uses MCP tools.",
)
from llama_index.core.agent.workflow import FunctionAgent
from llama_index.tools.mcp import (
get_tools_from_mcp_url,
aget_tools_from_mcp_url,
)
llm = OpenAI(model="gpt-4o-mini", api_key=os.getenv("OPENAI_API_KEY"))
youtube_tools = await aget_tools_from_mcp_url(
youtube_mcp_server_url, client=BasicMCPClient(youtube_mcp_server_url)
)
youtube_agent = FunctionAgent(
name="youtube_agent",
description="Agent using MCP-based tools",
tools=youtube_tools,
llm=llm,
system_prompt="You are an AI assistant that uses MCP tools.",
)
步骤 3 - 运行你的 AI 智能体来总结你最喜爱的视频!¶
In [ ]:
Copied!
YOUTUBE_VIDEO_URL = "https://www.youtube.com/watch?v=MmiveeGxfX0&t=528s" # pick a video you like!
response = await youtube_agent.run(
f"Summarize this video: {YOUTUBE_VIDEO_URL}"
)
print(response)
YOUTUBE_VIDEO_URL = "https://www.youtube.com/watch?v=MmiveeGxfX0&t=528s" # pick a video you like!
response = await youtube_agent.run(
f"Summarize this video: {YOUTUBE_VIDEO_URL}"
)
print(response)
The video titled "Introducing AgentWorkflow, a way to easily create multi-agent systems in Llamaindex" presents a new system called AgentWorkflow designed for building and orchestrating AI agent systems. It emphasizes the ability to coordinate multiple AI agents while maintaining state and context, making it suitable for both single specialized agents and teams working together. ### Key Features: - **Flexible Agent Types**: Includes FunctionAgent and ReActAgent. - **Built-in State Management**: Helps in managing the state of agents effectively. - **Real-time Monitoring**: Allows users to monitor the agents in action. - **Human-in-the-loop Capabilities**: Facilitates human oversight in the agent processes. The video encourages viewers to explore comprehensive tutorials and documentation to learn how to build everything from simple assistants to complex multi-agent systems. ### Additional Resources: - Basic Tutorial: [Basic Tutorial](https://docs.llamaindex.ai/en/stable/examples/agent/agent_workflow_basic/) - Full Documentation: [Full docs](https://docs.llamaindex.ai/en/stable/understanding/agent/multi_agents/) - Introductory Blog Post: [Blog Post](https://www.llamaindex.ai/blog/introducing-agentworkflow-a-powerful-system-for-building-ai-agent-systems) - Discord Community: [Join Discord](https://discord.com/invite/eN6D2HQ4aX) The video has a duration of approximately 16 minutes and 38 seconds and has garnered over 7,161 views.
✅ 干得漂亮!你已成功外包眼球工作并总结了你最爱的YouTube视频!
步骤 1 - 使用 Klavis 创建 YouTube 和 Gmail MCP 服务器¶
In [ ]:
Copied!
import webbrowser
klavis_client = Klavis(api_key=os.getenv("KLAVIS_API_KEY"))
# Create YouTube MCP server
youtube_mcp_instance = klavis_client.mcp_server.create_server_instance(
server_name=McpServerName.YOUTUBE,
user_id="1234",
platform_name="Klavis",
connection_type=ConnectionType.STREAMABLE_HTTP,
)
# Create Gmail MCP server with OAuth authorization
gmail_mcp_instance = klavis_client.mcp_server.create_server_instance(
server_name=McpServerName.GMAIL,
user_id="1234",
platform_name="Klavis",
connection_type=ConnectionType.STREAMABLE_HTTP,
)
print("✅ Created YouTube and Gmail MCP instances")
# Open Gmail OAuth authorization
webbrowser.open(gmail_mcp_instance.oauth_url)
print(
f"🔐 Opening OAuth authorization for Gmail, if you are not redirected, please open the following URL in your browser: {gmail_mcp_instance.oauth_url}"
)
import webbrowser
klavis_client = Klavis(api_key=os.getenv("KLAVIS_API_KEY"))
# Create YouTube MCP server
youtube_mcp_instance = klavis_client.mcp_server.create_server_instance(
server_name=McpServerName.YOUTUBE,
user_id="1234",
platform_name="Klavis",
connection_type=ConnectionType.STREAMABLE_HTTP,
)
# Create Gmail MCP server with OAuth authorization
gmail_mcp_instance = klavis_client.mcp_server.create_server_instance(
server_name=McpServerName.GMAIL,
user_id="1234",
platform_name="Klavis",
connection_type=ConnectionType.STREAMABLE_HTTP,
)
print("✅ Created YouTube and Gmail MCP instances")
# Open Gmail OAuth authorization
webbrowser.open(gmail_mcp_instance.oauth_url)
print(
f"🔐 Opening OAuth authorization for Gmail, if you are not redirected, please open the following URL in your browser: {gmail_mcp_instance.oauth_url}"
)
✅ Created YouTube and Gmail MCP instances 🔐 Opening OAuth authorization for Gmail, if you are not redirected, please open the following URL in your browser: https://api.klavis.ai/oauth/gmail/authorize?instance_id=d9d482b3-433a-4330-9a8b-9548c0b0a326
第二步 - 使用LlamaIndex创建基于MCP服务器的多智能体工作流¶
In [ ]:
Copied!
from llama_index.llms.openai import OpenAI
from llama_index.core.agent.workflow import FunctionAgent, AgentWorkflow
from llama_index.tools.mcp import (
BasicMCPClient,
get_tools_from_mcp_url,
aget_tools_from_mcp_url,
)
llm = OpenAI(model="gpt-4o-mini", api_key=os.getenv("OPENAI_API_KEY"))
# Get MCP server URLs
youtube_mcp_server_url = youtube_mcp_instance.server_url
gmail_mcp_server_url = gmail_mcp_instance.server_url
# Get tools from both MCP servers
youtube_tools = await aget_tools_from_mcp_url(
youtube_mcp_server_url, client=BasicMCPClient(youtube_mcp_server_url)
)
gmail_tools = await aget_tools_from_mcp_url(
gmail_mcp_server_url, client=BasicMCPClient(gmail_mcp_server_url)
)
# Create specialized agents
youtube_agent = FunctionAgent(
name="youtube_agent",
description="Agent that can summarize YouTube videos",
tools=youtube_tools,
llm=llm,
system_prompt="You are a YouTube video summarization expert. Use MCP tools to analyze and summarize videos.",
can_handoff_to=["gmail_agent"],
)
gmail_agent = FunctionAgent(
name="gmail_agent",
description="Agent that can send emails via Gmail",
tools=gmail_tools,
llm=llm,
system_prompt="You are an email assistant. Use MCP tools to send emails via Gmail.",
)
# Create multi-agent workflow
workflow = AgentWorkflow(
agents=[youtube_agent, gmail_agent],
root_agent="youtube_agent",
)
print("🤖 Multi-agent workflow created with YouTube and Gmail agents!")
from llama_index.llms.openai import OpenAI
from llama_index.core.agent.workflow import FunctionAgent, AgentWorkflow
from llama_index.tools.mcp import (
BasicMCPClient,
get_tools_from_mcp_url,
aget_tools_from_mcp_url,
)
llm = OpenAI(model="gpt-4o-mini", api_key=os.getenv("OPENAI_API_KEY"))
# Get MCP server URLs
youtube_mcp_server_url = youtube_mcp_instance.server_url
gmail_mcp_server_url = gmail_mcp_instance.server_url
# Get tools from both MCP servers
youtube_tools = await aget_tools_from_mcp_url(
youtube_mcp_server_url, client=BasicMCPClient(youtube_mcp_server_url)
)
gmail_tools = await aget_tools_from_mcp_url(
gmail_mcp_server_url, client=BasicMCPClient(gmail_mcp_server_url)
)
# Create specialized agents
youtube_agent = FunctionAgent(
name="youtube_agent",
description="Agent that can summarize YouTube videos",
tools=youtube_tools,
llm=llm,
system_prompt="You are a YouTube video summarization expert. Use MCP tools to analyze and summarize videos.",
can_handoff_to=["gmail_agent"],
)
gmail_agent = FunctionAgent(
name="gmail_agent",
description="Agent that can send emails via Gmail",
tools=gmail_tools,
llm=llm,
system_prompt="You are an email assistant. Use MCP tools to send emails via Gmail.",
)
# Create multi-agent workflow
workflow = AgentWorkflow(
agents=[youtube_agent, gmail_agent],
root_agent="youtube_agent",
)
print("🤖 Multi-agent workflow created with YouTube and Gmail agents!")
🤖 Multi-agent workflow created with YouTube and Gmail agents!
步骤 3 - 运行工作流!¶
In [ ]:
Copied!
YOUTUBE_VIDEO_URL = "https://www.youtube.com/watch?v=MmiveeGxfX0&t=528s" # pick a video you like!
EMAIL_RECIPIENT = "zihaolin@klavis.ai" # Replace with your email
resp = await workflow.run(
user_msg=f"Summarize this video {YOUTUBE_VIDEO_URL} and send it to {EMAIL_RECIPIENT}"
)
print("\n✅ Report:\n", resp.response.content)
YOUTUBE_VIDEO_URL = "https://www.youtube.com/watch?v=MmiveeGxfX0&t=528s" # pick a video you like!
EMAIL_RECIPIENT = "zihaolin@klavis.ai" # Replace with your email
resp = await workflow.run(
user_msg=f"Summarize this video {YOUTUBE_VIDEO_URL} and send it to {EMAIL_RECIPIENT}"
)
print("\n✅ Report:\n", resp.response.content)
✅ Report: The summary of the video "Introducing AgentWorkflow, a way to easily create multi-agent systems in Llamaindex" has been successfully sent to zihaolin@klavis.ai. If you need anything else, feel free to ask!
总结¶
在本教程中,我们探索了如何将LlamaIndex与Klavis AI集成,通过MCP(模型上下文协议)服务器构建强大的AI智能体。以下是我们的成果:
核心要点:¶
- 单智能体配置:创建了一个YouTube AI智能体,能够使用Klavis YouTube MCP服务器来总结视频内容
- 多智能体工作流:构建了一个结合YouTube和Gmail智能体的复杂工作流,可总结视频内容并通过邮件自动发送摘要
- MCP集成:学习了如何将Klavis MCP服务器与LlamaIndex的智能体框架结合使用,以实现增强功能
这种集成方式为构建AI智能体开辟了无限可能,这些智能体可以通过Klavis MCP服务器与各种服务和平台交互。现在您可以创建支持YouTube、Gmail、GitHub、Slack等Klavis所支持服务的智能体了。
祝您构建愉快!🚀