Vertex AI Agente motor is a Google Cloud service that helps you build and scale AI agents in production. You can use the Agent Engine with MongoDB Atlas and your preferred framework to build AI agents for a variety of use cases, including agentic RAG.
Empezar
The following tutorial demonstrates how you can use the Agent Engine with Atlas to build a RAG agent that can answer questions about sample data. It uses MongoDB Vector Search with LangChain to implement the retrieval tools for the agent.
Requisitos previos
Antes de comenzar, asegúrate de tener lo siguiente:
Un clúster de Atlas en tu región preferida de Google Cloud. Para crear un clúster, consulta "Crear un clúster". También puedes empezar a usar Atlas a través de Google Cloud Marketplace.
Un proyecto de Google Cloud con Vertex AI habilitado. Para configurar un proyecto, consulta "Configurar un proyecto y un entorno de desarrollo" en la documentación de Google Cloud.
Configura tu entorno
Create an interactive Python notebook by saving a file with the .ipynb extension in Google Colab. This notebook allows you to run Python code snippets individually, and you'll use it to run the code in this tutorial.
Instalar los paquetes necesarios.
En su entorno portátil, instale los paquetes necesarios:
!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
Cree los índices de búsqueda vectorial de MongoDB.
Ejecute el siguiente código en su notebook para crear las colecciones de MongoDB y los índices de MongoDB Vector Search que se usarán para almacenar y consultar sus datos en este tutorial. Reemplace <connection-string> con la cadena de conexión de su 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,consulte Conectarse a un clúster a través de bibliotecas de cliente.
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 de MongoDB, consulte Cómo indexar campos para la búsqueda vectorial.
Inicialice el SDK de Vertex AI.
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)
Ingerir datos en Atlas
Ejecute el siguiente código para extraer datos de muestra de Wikipedia sobre Star Wars y Star Trek, convertir el texto en incrustaciones vectoriales utilizando 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
Puede ver sus datos en la interfaz de usuario de Atlas navegando a la AGENT-ENGINE base de datos y seleccionando sample_starwars_embeddings las sample_startrek_embeddings colecciones y.
Crear el agente
En esta sección, define las herramientas que el agente puede usar para consultar sus colecciones mediante MongoDB Vector Search, crear un sistema de memoria para mantener el contexto de la conversación y luego inicializar el agente mediante LangChain.
Defina herramientas para el agente.
Crea las siguientes dos herramientas:
Ejecute 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
Ejecute 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
Crear un sistema de memoria.
Puede utilizar LangChain para crear memoria para su agente de modo que pueda mantener el contexto de la conversación en múltiples indicaciones:
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]
Inicializar el agente.
Cree el agente con LangChain. Este agente utiliza las herramientas y el sistema de memoria que usted definió.
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 consulta 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.
Implementar el agente
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.
Implemente su agente.
Ejecute el siguiente código para configurar e implementar el agente en 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", ], )
Recupere la URL del proyecto.
Ejecute el siguiente código para recuperar el número de proyecto asociado a su ID de proyecto. Este número se usará para construir el nombre completo del recurso para su 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
Pruebe el agente.
Ejecute el siguiente código para usar su agente. Reemplace el marcador de posición con el nombre completo del recurso de su 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 puedes 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.
Próximos pasos
También puede depurar y optimizar sus agentes habilitando el rastreo en el motor de agentes. Consulte la documentación del motor de agentes de Vertex AI para ver otras funciones y ejemplos.
Para obtener más información sobre la integración de LangChain con MongoDB, consulte Integrar MongoDB con LangChain.