Make the MongoDB docs better! We value your opinion. Share your feedback for a chance to win $100.
MongoDB Branding Shape
Click here >
Docs Menu

Compilar agentes de IA con Vertex AI Agent Engine y Atlas

Vertex AI Agent Engine es un servicio de Google Cloud que te ayuda a compilar y escalar agentes de IA en producción. Puedes utilizar el Motor de agente con MongoDB Atlas y tu framework preferido para compilar agentes de IA para diversos casos de uso, incluido el RAG agente.

El siguiente tutorial demuestra cómo puedes utilizar el Motor de Agente con Atlas para construir un agente RAG que pueda responder preguntas sobre datos de muestra. Utiliza MongoDB Vector Search con LangChain para implementar las herramientas de recuperación para el agente.

Antes de comenzar, asegúrate de tener lo siguiente:

Crea un cuaderno interactivo de Python guardando un archivo con la extensión .ipynb en Google Colab. Este notebook te permite ejecutar snippets de código Python de forma individual, y lo usarás para ejecutar el código en este tutorial.

1

En tu entorno de notebook, instala los paquetes requeridos:

!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

Ejecuta el siguiente código en tu notebook para crear las colecciones de MongoDB y los índices de MongoDB Vector Search que se utilizan para almacenar y hacer query de tus datos en este tutorial. Reemplaza <connection-string> por la cadena de conexión.de tu clúster

Nota

Se debe sustituir <connection-string> por la cadena de conexión del clúster Atlas o de la implementación local de Atlas.

Su cadena de conexión debe usar el siguiente formato:

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

Para obtener más información, consulta Conectar a un clúster a través de bibliotecas de clientes.

Su cadena de conexión debe usar el siguiente formato:

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

Para obtener más información, consulta Cadenas de conexión.

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)

Para obtener más información sobre cómo crear un índice de búsqueda vectorial en MongoDB, consulte Cómo indexar campos para la búsqueda vectorial.

3

Ejecute el siguiente código en su notebook, reemplazando los valores del marcador de posición con su ID del proyecto de Google Cloud, región y bucket de staging:

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)

Ejecute el siguiente código para extraer datos de muestra de Wikipedia sobre Star Wars y Star Trek, convertir el texto en incrustaciones vectoriales usando el modelo text-embedding-005 y luego almacenar estos datos en las colecciones correspondientes en 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")

Tip

Puedes ver tus datos en la interfaz de usuario de Atlas navegando hasta la base de datos AGENT-ENGINE y seleccionando las colecciones sample_starwars_embeddings y sample_startrek_embeddings.

En esta sección, definas las herramientas que el agente puede utilizar para consultar tus colecciones usando MongoDB Vector Search, crear un sistema de memoria para mantener el contexto de la conversación y luego inicializar el agente usando LangChain.

1

Cree las siguientes dos herramientas:

Ejecuta el siguiente código para crear una herramienta que utilice MongoDB Vector Search para consultar la colección 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

Ejecuta el siguiente código para crear una herramienta que utilice MongoDB Vector Search para consultar la colección 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

Puedes usar LangChain para crear memoria para tu agente, de modo que pueda mantener el contexto de la conversación a lo largo de varios avisos:

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

Crea el agente usando LangChain. Este agente utiliza las herramientas y el sistema de memoria que definiste.

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},
)

Para probar el agente con una query de muestra:

# 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.

En esta sección, implementa tu agente en el motor Vertex AI Agent como un servicio gestionado. Esto permite escalar el agente y usarlo en producción sin gestionar la infraestructura subyacente.

1

Ejecuta el siguiente código para configurar e implementar el agente en el motor de Vertex AI Agent:

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

Ejecuta el siguiente código para recuperar el número de Proyecto asociado con la ID del grupo. Este número de proyecto se usará para construir el nombre completo del recurso de tu agente implementado:

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

Ejecuta el siguiente código para utilizar tu agente. Reemplaza el espacio reservado con el nombre de recurso completo de tu agente:

Nota

Después de la implementación, tu agente tendrá un nombre de recurso único en el siguiente formato:

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.

También puede preguntarle al agente sobre Star Trek usando la misma sesión:

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.

También puede depurar y optimizar sus agentes activando el trazado en Agent motor. Consulte la Documentación del Motor de agente de Vertex IA para otras funcionalidades y ejemplos.

Para obtener más información sobre la integración de LangChain con MongoDB, consulte Integrar MongoDB con LangChain.