Vertex AI Agent Engine は、Google Cloud のサービスであり、本番環境で AI エージェントを構築・スケーリングするのに役立ちます。Agent Engine は MongoDB Atlas とお好みの フレームワークと組み合わせて使用でき、エージェント型 RAG を含むさまざまなユースケースに対応した AI エージェントを構築できます。
はじめる
以下のチュートリアルでは、Agent Engine と Atlas を使用して、サンプルデータに関する質問に回答できる RAG エージェントを構築する方法を示します。LangChain と Atlas Vector Search を使用してエージェントの検索ツールを実装します。
前提条件
開始する前に、次のものをお持ちであることを確認してください。
お好みの Google Cloud リージョンにある Atlas クラスター。新しいクラスターを作成するには、クラスターの作成を参照してください。また、Google Cloud Marketplace から Atlas を使い始めることもできます。
Vertex AI が有効化された Google Cloud プロジェクト。プロジェクトを設定するには、「プロジェクトと開発環境の設定」を Google Cloud ドキュメントで参照してください。
環境を設定する
Google Colab で.ipynb
拡張子のファイルを保存することで、インタラクティブな Python ノートブックを作成できます。このノートブックでは、Python のコードスニペットを個別に実行できます。このチュートリアルのコードを実行するためにも使用します。
必要なパッケージをインストールします。
ノートブック環境で必要なパッケージをインストールします。
!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
Atlas Vector Search インデックスを作成します。
このチュートリアルでデータの保存とクエリに使用する MongoDB コレクションおよび Atlas Vector Search インデックスを作成するには、ノートブックで次のコードを実行します。<connection-string>
を、お客様のクラスターの接続文字列に置き換えてください。
注意
接続stringには、次の形式を使用する必要があります。
mongodb+srv://<db_username>:<db_password>@<clusterName>.<hostname>.mongodb.net
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)
Atlas Vector Search インデックスの作成について詳しくは、「ベクトル検索用のフィールドをインデックスする方法」を参照してください。
Vertex AI SDK を初期化してください。
次のコードをノートブックで実行し、プレースホルダーの値を 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)
Atlas にデータを取り込む
次のコードを実行すると、Wikipedia から Star Wars と Star Trek に関するサンプル データをスクレイピングし、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")
Tip
AGENT-ENGINE
データベースに移動し、sample_starwars_embeddings
および sample_startrek_embeddings
コレクションを選択すると、Atlas UI でデータを表示できます。
エージェントを作成する
このセクションでは、エージェントが Atlas Vector Search を使ってコレクションにクエリを実行できるようにするツールを定義し、会話のコンテキストを維持するためのメモリ システムを作成し、最後に LangChain を使ってエージェントを初期化します。
エージェントのツールを定義します。
次の 2 つのツールを作成します。
次のコードを実行して、Atlas 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
次のコードを実行して、Atlas 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
メモリシステムを作成してください。
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]
エージェントを初期化してください。
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 Agent Engine にマネージド サービスとしてデプロイします。これにより、基盤となるインフラを管理することなく、エージェントをスケーリングして本番環境で利用できるようになります。
エージェントをデプロイします。
次のコードを実行して、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", ], )
プロジェクトの URL を検索します。
次のコードを実行して、プロジェクト 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
エージェントをテストします。
次のコードを実行して、エージェントを使用します。プレースホルダーをエージェントの完全なリソース名に置き換えます。
注意
デプロイ後、エージェントには以下の形式の一意のリソース名が割り当てられます。
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.
同じセッションを使用して、エージェントに「Star Trek」について質問することもできます。
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.
次のステップ
Agent Engine で トレーシングを有効にすることで、エージェントのデバッグや最適化も行えます。その他の機能や使用例については、Vertex AI Agent Engine のドキュメントを参照してください。
LgChuin MongoDB統合の詳細については、 MongoDBと LgDB を統合する を参照してください。