Docs 菜单
Docs 主页
/
Atlas
/ / /

使用 Vertex AI 代理引擎和 Atlas 构建 AI 代理

Vertex AI 代理引擎 是一项 Google Cloud 服务,帮助您在生产中构建和扩展 AI 代理。您可以将 AI 代理引擎与 MongoDB Atlas 和您首选的框架结合使用,以构建适用于各种用例的 AI 代理,包括代理 RAG。

以下教程演示如何将代理引擎与Atlas结合使用来构建可以回答有关示例数据的问题的 RAG代理。它使用MongoDB Vector Search 和 LangChain 来实现代理的检索工具。

在开始之前,请确保您具备以下内容:

  • 您的首选Google Cloud Platform地区中的Atlas 集群。 要创建新集群,请参阅创建集群。 您还可以通过Google Cloud Platform Marketplace 开始使用Atlas 。

  • 一个启用了 Vertex AI 的 Google Cloud 项目。如要设立项目,请参阅 Google Cloud 文档中的设立项目和开发环境

Google Colab 中保存扩展名为.ipynb 的文件,创建交互式 Python 笔记本。该笔记本允许您单独运行 Python 代码片段,您将使用它来运行本教程中的代码。

1

在您的笔记本环境中安装所需的包:

!pip install --upgrade --quiet \
"google-cloud-aiplatform[langchain,agent_engines]" requests datasets pymongo langchain langchain-community langchain-mongodb langchain-google-vertexai google-cloud-aiplatform langchain_google_genai requests beautifulsoup4
2

在笔记本中运行以下代码,创建MongoDB集合和MongoDB Vector Search 索引,用于存储和查询本教程的数据。将 <connection-string> 替换为集群的连接字符串。

注意

<connection-string> 替换为您的 Atlas 集群或本地部署的连接字符串。

连接字符串应使用以下格式:

mongodb+srv://<db_username>:<db_password>@<clusterName>.<hostname>.mongodb.net

要学习;了解更多信息,请参阅通过驱动程序连接到集群。

连接字符串应使用以下格式:

mongodb://localhost:<port-number>/?directConnection=true

要学习;了解更多信息,请参阅连接字符串。

from pymongo import MongoClient
from pymongo.operations import SearchIndexModel
client = MongoClient("<connection-string>") # Replace with your connection string
db = client["AGENT-ENGINE"]
stars_wars_collection = db["sample_starwars_embeddings"]
stars_trek_collection = db["sample_startrek_embeddings"]
# Create your index model, then create the search index
search_index_model = SearchIndexModel(
definition={
"fields": [
{
"type": "vector",
"path": "embedding",
"numDimensions": 768,
"similarity": "cosine"
}
]
},
name="vector_index",
type="vectorSearch"
)
# Create the indexes
stars_wars_collection.create_search_index(model=search_index_model)
stars_trek_collection.create_search_index(model=search_index_model)

要学习;了解有关创建MongoDB Vector Search索引的更多信息,请参阅如何为向量搜索的字段创建索引。

3

在您的笔记本中运行以下代码,将占位符值替换为您的 Google Cloud 项目 ID、区域和暂存存储桶:

PROJECT_ID = "<your-project-id>" # Replace with your project ID
LOCATION = "<gcp-region>" # Replace with your preferred region, e.g. "us-central1"
STAGING_BUCKET = "gs://<your-bucket-name>" # Replace with your bucket
import vertexai
vertexai.init(project=PROJECT_ID, location=LOCATION, staging_bucket=STAGING_BUCKET)

运行以下代码,从 Wikipedia 抓取关于《星球大战》和《星际迷航》的示例数据,使用 text-embedding-005 模型将文本转换为向量嵌入,然后将这些数据存储在 Atlas 的相应集合中。

import requests
from bs4 import BeautifulSoup
from pymongo import MongoClient
import certifi
from vertexai.language_models import TextEmbeddingModel
# Scrape the website content
def scrape_website(url):
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
content = ' '.join([p.text for p in soup.find_all('p')])
return content
# Split the content into chunks of 1000 characters
def split_into_chunks(text, chunk_size=1000):
return [text[i:i+chunk_size] for i in range(0, len(text), chunk_size)]
def get_text_embeddings(chunks):
model = TextEmbeddingModel.from_pretrained("text-embedding-005")
embeddings = model.get_embeddings(chunks)
return [embedding.values for embedding in embeddings]
def write_to_mongoDB(embeddings, chunks, db_name, coll_name):
client = MongoClient("<connection-string>", tlsCAFile=certifi.where()) # Replace placeholder with your Atlas connection string
db = client[db_name]
collection = db[coll_name]
for i in range(len(chunks)):
collection.insert_one({
"chunk": chunks[i],
"embedding": embeddings[i]
})
# Process Star Wars data
content = scrape_website("https://en.wikipedia.org/wiki/Star_Wars")
chunks = split_into_chunks(content)
embeddings_starwars = get_text_embeddings(chunks)
write_to_mongoDB(embeddings_starwars, chunks, "AGENT-ENGINE", "sample_starwars_embeddings")
# Process Star Trek data
content = scrape_website("https://en.wikipedia.org/wiki/Star_Trek")
chunks = split_into_chunks(content)
embeddings_startrek = get_text_embeddings(chunks)
write_to_mongoDB(embeddings_startrek, chunks, "AGENT-ENGINE", "sample_startrek_embeddings")

提示

您可以通过导航到AGENT-ENGINE 数据库并选择sample_starwars_embeddingssample_startrek_embeddings 集合,在 Atlas UI 查看数据。

在本节中,您将定义代理可用于使用MongoDB Vector Search查询集合的工具,创建内存系统以维护对话上下文,然后使用 LangChain 初始化代理。

1

创建以下两个工具:

运行以下代码以创建一个使用MongoDB Vector Search查询sample_starwars_embeddings集合的工具:

def star_wars_query_tool(
query: str
):
"""
Retrieves vectors from a MongoDB database and uses them to answer a question related to Star wars.
Args:
query: The question to be answered about star wars.
Returns:
A dictionary containing the response to the question.
"""
from langchain.chains import ConversationalRetrievalChain, RetrievalQA
from langchain_mongodb import MongoDBAtlasVectorSearch
from langchain_google_vertexai import VertexAIEmbeddings, ChatVertexAI
from langchain.memory import ConversationBufferMemory, ConversationBufferWindowMemory
from langchain.prompts import PromptTemplate
prompt_template = """Use the following pieces of context to answer the question at the end. If you don't know the answer, just say that you don't know, don't try to make up an answer. Do not return any answers from your own knowledge. Respond only in 2 or 3 sentences.
{context}
Question: {question}
"""
PROMPT = PromptTemplate(
template=prompt_template, input_variables=["context", "question"]
)
# Replace with your connection string to your Atlas cluster
connection_string = "<connection-string>"
embeddings = VertexAIEmbeddings(model_name="text-embedding-005")
vs = MongoDBAtlasVectorSearch.from_connection_string(
connection_string=connection_string,
namespace="AGENT-ENGINE.sample_starwars_embeddings",
embedding=embeddings,
index_name="vector_index",
embedding_key="embedding",
text_key="chunk",
)
llm = ChatVertexAI(
model_name="gemini-pro",
convert_system_message_to_human=True,
max_output_tokens=1000,
)
retriever = vs.as_retriever(
search_type="mmr", search_kwargs={"k": 10, "lambda_mult": 0.25}
)
memory = ConversationBufferWindowMemory(
memory_key="chat_history", k=5, return_messages=True
)
conversation_chain = ConversationalRetrievalChain.from_llm(
llm=llm,
retriever=retriever,
memory=memory,
combine_docs_chain_kwargs={"prompt": PROMPT},
)
response = conversation_chain({"question": query})
return response

运行以下代码以创建一个使用MongoDB Vector Search查询sample_startrek_embeddings集合的工具:

def star_trek_query_tool(
query: str
):
"""
Retrieves vectors from a MongoDB database and uses them to answer a question related to star trek.
Args:
query: The question to be answered about star trek.
Returns:
A dictionary containing the response to the question.
"""
from langchain.chains import ConversationalRetrievalChain, RetrievalQA
from langchain_mongodb import MongoDBAtlasVectorSearch
from langchain_google_vertexai import VertexAIEmbeddings, ChatVertexAI
from langchain.memory import ConversationBufferMemory, ConversationBufferWindowMemory
from langchain.prompts import PromptTemplate
prompt_template = """Use the following pieces of context to answer the question at the end. If you don't know the answer, just say that you don't know, don't try to make up an answer. Do not return any answers from your own knowledge. Respond only in 2 or 3 sentences.
{context}
Question: {question}
"""
PROMPT = PromptTemplate(
template=prompt_template, input_variables=["context", "question"]
)
# Replace with your connection string to your Atlas cluster
connection_string = "<connection-string>"
embeddings = VertexAIEmbeddings(model_name="text-embedding-005")
vs = MongoDBAtlasVectorSearch.from_connection_string(
connection_string=connection_string,
namespace="AGENT-ENGINE.sample_startrek_embeddings",
embedding=embeddings,
index_name="vector_index",
embedding_key="embedding",
text_key="chunk",
)
llm = ChatVertexAI(
model_name="gemini-pro",
convert_system_message_to_human=True,
max_output_tokens=1000,
)
retriever = vs.as_retriever(
search_type="mmr", search_kwargs={"k": 10, "lambda_mult": 0.25}
)
memory = ConversationBufferWindowMemory(
memory_key="chat_history", k=5, return_messages=True
)
conversation_chain = ConversationalRetrievalChain.from_llm(
llm=llm,
retriever=retriever,
memory=memory,
combine_docs_chain_kwargs={"prompt": PROMPT},
)
response = conversation_chain({"question": query})
return response
2

您可以使用 LangChain 为您的代理创建记忆,以便它能够在多个提示中保持对话上下文:

from langchain.memory import ChatMessageHistory
# Initialize session history
store = {}
def get_session_history(session_id: str):
if session_id not in store:
store[session_id] = ChatMessageHistory()
return store[session_id]
3

使用 LangChain 创建代理。此代理使用您定义的工具和内存系统。

from vertexai.preview.reasoning_engines import LangchainAgent
# Specify the language model
model = "gemini-1.5-pro-001"
# Initialize the agent with your tools
agent = LangchainAgent(
model=model,
chat_history=get_session_history,
model_kwargs={"temperature": 0},
tools=[star_wars_query_tool, star_trek_query_tool],
agent_executor_kwargs={"return_intermediate_steps": True},
)

如要使用示例查询来测试代理:

# Test your agent
response = agent.query(
input="Who was the antagonist in Star wars and who played them? ",
config={"configurable": {"session_id": "demo"}},
)
display(Markdown(response["output"]))
The main antagonist in the Star Wars series is Darth Vader, a dark lord of the Sith. He was originally played by David Prowse in the original trilogy, and later voiced by James Earl Jones. In the prequel trilogy, he appears as Anakin Skywalker, and was played by Hayden Christensen.

在本节中,您会将您的代理作为托管服务部署到 Vertex AI 代理引擎中。这使您能够扩展您的代理并在生产中使用它,无需管理底层的基础设施。

1

运行以下代码以在 Vertex AI Agent Engine 中配置和部署代理:

from vertexai import agent_engines
remote_agent = agent_engines.create(
agent,
requirements=[
"google-cloud-aiplatform[agent_engines,langchain]",
"cloudpickle==3.0.0",
"pydantic>=2.10",
"requests",
"langchain-mongodb",
"pymongo",
"langchain-google-vertexai",
],
)
2

运行以下代码以检索与您的项目 ID 关联的项目编号。此项目编号将用于构建已部署代理的完整资源名称:

from googleapiclient import discovery
from IPython.display import display, Markdown
# Retrieve the project number associated with your project ID
service = discovery.build("cloudresourcemanager", "v1")
request = service.projects().get(projectId=PROJECT_ID)
response = request.execute()
project_number = response["projectNumber"]
print(f"Project Number: {project_number}")
# The deployment creates a unique ID for your agent that you can find in the output
3

运行以下代码以使用您的代理。将占位符替换为您的代理的完整资源名称:

注意

部署后,您的代理将拥有一个唯一的资源名称,格式如下:

projects/<project-number>/locations/<gcp-region>/reasoningEngines/<unique-id>

from vertexai.preview import reasoning_engines
# Replace with your agent's full resource name from the previous step
REASONING_ENGINE_RESOURCE_NAME = "<resource-name>"
remote_agent = reasoning_engines.ReasoningEngine(REASONING_ENGINE_RESOURCE_NAME)
response = remote_agent.query(
input="tell me about episode 1 of star wars",
config={"configurable": {"session_id": "demo"}},
)
print(response["output"])
response = remote_agent.query(
input="Who was the main character in this series",
config={"configurable": {"session_id": "demo"}},
)
print(response["output"])
Star Wars: Episode I - The Phantom Menace was the first film installment released as part of the prequel trilogy. It was released on May 19, 1999. The main plot lines involve the return of Darth Sidious, the Jedi's discovery of young Anakin Skywalker, and the invasion of Naboo by the Trade Federation.
The main character in Star Wars is Luke Skywalker. He is a young farm boy who dreams of adventure and becomes a Jedi Knight. He fights against the evil Galactic Empire alongside his friends, Princess Leia and Han Solo.

您还可以使用相同的会话向代理询问有关《星际迷航》的信息:

response = remote_agent.query(
input="what is episode 1 of star trek?",
config={"configurable": {"session_id": "demo"}},
)
print(response["output"])
Episode 1 of Star Trek is called "The Man Trap". It was first aired on September 8, 1966. The story involves the Enterprise crew investigating the disappearance of a crew on a scientific outpost. It turns out that the crew members were killed by a creature that can take on someone else's form after it kills them.

您还可以通过在代理引擎中启用跟踪来调试和优化您的代理。请参阅 Vertex AI 代理引擎文档以了解其他功能和示例。

要学习;了解有关 LangChain MongoDB集成的更多信息,请参阅将MongoDB与 LangChain 集成。

后退

Vertex AI 扩展

来年

MCP 服务器

获得技能徽章

免费掌握“Gen AI”!

了解详情

在此页面上