Join us Sept 17 at .local NYC! Use code WEB50 to save 50% on tickets. Learn more >
MongoDB Event
Docs Menu
Docs Home
/
Atlas
/ / /

Vertex AI 에이전트 엔진과 Atlas로 AI 에이전트 빌드하기

Vertex AI 에이전트 엔진은 Google Cloud 서비스로, 프로덕션 환경에서 AI 에이전트를 빌드하고 확장하는 데 도움을 줍니다. 에이전트 엔진을 MongoDB Atlas 및 선호하는 프레임워크와 함께 사용하여 에이전트 RAG를 비롯한 다양한 사용 사례를 위한 AI 에이전트를 빌드할 수 있습니다.

다음 튜토리얼에서는 Atlas와 함께 에이전트 엔진을 사용하여 샘플 데이터에 대한 질문에 답변할 수 있는 RAG 에이전트를 빌드하는 방법을 보여줍니다. 에이전트의 조회 도구를 구현하기 위해 LangChain과 Atlas Vector Search를 사용합니다.

시작하기 전에 다음이 준비되어 있는지 확인하세요.

  • 선호하는 Google Cloud 리전에 있는 Atlas 클러스터. 새 클러스터를 만들려면 클러스터 생성을 참조하세요. Google Cloud 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 컬렉션과 Atlas Vector Search 인덱스를 생성하려면 노트북에서 다음 코드를 실행합니다. <connection-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 인덱스의 필드에 대한 자세한 내용은 벡터 검색을 위한 필드 인덱싱 방법을 참조하세요.

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)

Star Wars와 Star Trek에 대한 샘플 데이터를 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에서 데이터를 볼 수 있습니다.

이 섹션에서는 Atlas Vector Search를 사용해 컬렉션을 쿼리할 수 있도록 에이전트가 사용할 도구를 정의하고, 대화 문맥을 유지할 수 있는 메모리 시스템을 생성한 다음, LangChain을 사용해 에이전트를 초기화합니다.

1

다음 두 가지 도구를 생성합니다.

다음 코드를 실행하여 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
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'를 무료로 마스터하세요!

자세한 내용을 알아보세요.

이 페이지의 내용