๊ฒ์ ์ฆ๊ฐ ์์ฑ (RAG)์ LLM์ด ๊ฒ์ฆ ๊ฐ๋ฅํ ๋๋ต์ ์์ฑํ ์ ์๋๋ก ๊ด๋ จ ์ ๋ณด๋ฅผ ๊ฒ์ํ๊ณ ์ ๊ณตํ๋ ๋ฐ ์ฌ์ฉ๋๋ ๊ธฐ์ ์ ๋๋ค. ์ ๋ณด์๋ ์ต์ ์ ๋ณด, ์ฃผ์ ๋ฐ ์ปจํ ์คํธ ๋๋ ์ ๋ต์ด ํฌํจ๋ ์ ์์ต๋๋ค.
์ด ํ์ด์ง์์๋ RAG ์ฝํผ์ค์์ ์ ๋ณด๋ฅผ ์ง์ ํ๊ณ ๊ฒ์ํ ์ ์๋ Gemini Live API์ ํจ๊ป Vertex AI RAG Engine์ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ์ ๋ณด์ฌ์ค๋๋ค.
๊ธฐ๋ณธ ์๊ฑด
๋ฉํฐ๋ชจ๋ฌ Live API์ ํจ๊ป Vertex AI RAG Engine์ ์ฌ์ฉํ๋ ค๋ฉด ๋ค์ ์ ํ ์กฐ๊ฑด์ ์๋ฃํด์ผ ํฉ๋๋ค.
Vertex AI์์ RAG API๋ฅผ ์ฌ์ฉ ์ค์ ํฉ๋๋ค.
RAG ์ฝํผ์ค์ ํ์ผ์ ์ ๋ก๋ํ๋ ค๋ฉด RAG ํ์ผ ๊ฐ์ ธ์ค๊ธฐ ์์ API๋ฅผ ์ฐธ๊ณ ํ์ธ์.
์ค์
Vertex AI RAG Engine์ ๋๊ตฌ๋ก ์ง์ ํ์ฌ Live API์ ํจ๊ป Vertex AI RAG Engine์ ์ฌ์ฉํ ์ ์์ต๋๋ค. ๋ค์ ์ฝ๋ ์ํ์ Vertex AI RAG ์์ง์ ๋๊ตฌ๋ก ์ง์ ํ๋ ๋ฐฉ๋ฒ์ ๋ณด์ฌ์ค๋๋ค.
๋ค์ ๋ณ์๋ฅผ ๋ฐ๊ฟ๋๋ค.
- YOUR_PROJECT_ID: Google Cloud ํ๋ก์ ํธ์ ID.
- YOUR_CORPUS_ID: ์ฝํผ์ค ID์ ๋๋ค.
- YOUR_LOCATION: ์์ฒญ์ ์ฒ๋ฆฌํ๋ ๋ฆฌ์ .
PROJECT_ID = "YOUR_PROJECT_ID"
RAG_CORPUS_ID = "YOUR_CORPUS_ID"
LOCATION = "YOUR_LOCATION"
TOOLS = {
"retrieval": {
"vertex_rag_store": {
"rag_resources": {
"rag_corpus": "projects/${PROJECT_ID}/locations/${LOCATION}/ragCorpora/${RAG_CORPUS_ID}"
}
}
}
}
์ค์๊ฐ ์ปค๋ฎค๋์ผ์ด์
์ Websocket
์ฌ์ฉํ๊ธฐ
ํด๋ผ์ด์ธํธ์ ์๋ฒ ๊ฐ์ ์ค์๊ฐ ํต์ ์ ์ฌ์ฉ ์ค์ ํ๋ ค๋ฉด Websocket
๋ฅผ ์ฌ์ฉํด์ผ ํฉ๋๋ค. ์ด ์ฝ๋ ์ํ์ Python API์ Python SDK๋ฅผ ์ฌ์ฉํ์ฌ Websocket
๋ฅผ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ์ ๋ณด์ฌ์ค๋๋ค.
Python API
CONFIG = {"response_modalities": ["TEXT"], "speech_config": { "language_code": "en-US" }}
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {bearer_token[0]}",
}
HOST= "${LOCATION}-aiplatform.googleapis.com"
SERVICE_URL = f"wss://{HOST}/ws/google.cloud.aiplatform.v1beta1.LlmBidiService/BidiGenerateContent"
MODEL="gemini-2.0-flash-exp"
# Connect to the server
async with connect(SERVICE_URL, additional_headers=headers) as ws:
# Setup the session
await ws.send(
json.dumps(
{
"setup": {
"model": MODEL,
"generation_config": CONFIG,
# Setup RAG as a retrieval tool
"tools": TOOLS,
}
}
)
)
# Receive setup response
raw_response = await ws.recv(decode=False)
setup_response = json.loads(raw_response.decode("ascii"))
# Send text message
text_input = "What are popular LLMs?"
display(Markdown(f"**Input:** {text_input}"))
msg = {
"client_content": {
"turns": [{"role": "user", "parts": [{"text": text_input}]}],
"turn_complete": True,
}
}
await ws.send(json.dumps(msg))
responses = []
# Receive chunks of server response
async for raw_response in ws:
response = json.loads(raw_response.decode())
server_content = response.pop("serverContent", None)
if server_content is None:
break
model_turn = server_content.pop("modelTurn", None)
if model_turn is not None:
parts = model_turn.pop("parts", None)
if parts is not None:
display(Markdown(f"**parts >** {parts}"))
responses.append(parts[0]["text"])
# End of turn
turn_complete = server_content.pop("turnComplete", None)
if turn_complete:
grounding_metadata = server_content.pop("groundingMetadata", None)
if grounding_metadata is not None:
grounding_chunks = grounding_metadata.pop("groundingChunks", None)
if grounding_chunks is not None:
for chunk in grounding_chunks:
display(Markdown(f"**grounding_chunk >** {chunk}"))
break
# Print the server response
display(Markdown(f"**Response >** {''.join(responses)}"))
Python SDK
์์ฑํ AI SDK๋ฅผ ์ค์นํ๋ ๋ฐฉ๋ฒ์ ์์๋ณด๋ ค๋ฉด ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ค์น๋ฅผ ์ฐธ๊ณ ํ์ธ์.
from google import genai
from google.genai import types
from google.genai.types import (Content, LiveConnectConfig, HttpOptions, Modality, Part,)
from IPython import display
MODEL="gemini-2.0-flash-exp"
client = genai.Client(
vertexai=True,
project=PROJECT_ID,
location=LOCATION
)
async with client.aio.live.connect(
model=MODEL,
config=LiveConnectConfig(response_modalities=[Modality.TEXT],
tools=TOOLS),
) as session:
text_input = "\'What are core LLM techniques?\'"
print("> ", text_input, "\n")
await session.send_client_content(
turns=Content(role="user", parts=[Part(text=text_input)])
)
async for message in session.receive()
if message.text:
display.display(display.Markdown(message.text))
continue
Vertex AI RAG Engine์ ์ปจํ ์คํธ ์คํ ์ด๋ก ์ฌ์ฉ
Vertex AI RAG Engine์ Gemini Live API์ ์ปจํ ์คํธ ์คํ ์ด๋ก ์ฌ์ฉํ์ฌ ์ธ์ ์ปจํ ์คํธ๋ฅผ ์ ์ฅํ์ฌ ๋ํ์ ๊ด๋ จ๋ ์ด์ ์ปจํ ์คํธ๋ฅผ ํ์ฑํ๊ณ ๊ฒ์ํ๋ฉฐ ๋ชจ๋ธ ์์ฑ์ ์ํ ํ์ฌ ์ปจํ ์คํธ๋ฅผ ๋ณด๊ฐํ ์ ์์ต๋๋ค. ์ด ๊ธฐ๋ฅ์ ํ์ฉํ์ฌ ์ฌ๋ฌ Live API ์ธ์ ๊ฐ์ ์ปจํ ์คํธ๋ฅผ ๊ณต์ ํ ์๋ ์์ต๋๋ค.
Vertex AI RAG Engine์ ์ธ์ ์ปจํ ์คํธ์์ ๋ค์ ํ์์ ๋ฐ์ดํฐ๋ฅผ ์ ์ฅํ๊ณ ์์ธ์ ์์ฑํ๋ ๊ฒ์ ์ง์ํฉ๋๋ค.
- ํ ์คํธ
- ์ค๋์ค ์์ฑ
MemoryCorpus ์ ํ ์ฝํผ์ค ๋ง๋ค๊ธฐ
์ธ์
์ปจํ
์คํธ์ ๋ํ ํ
์คํธ๋ฅผ ์ ์ฅํ๊ณ ์์ธ์ ์์ฑํ๋ ค๋ฉด MemoryCorpus
์ ํ์ RAG ์ฝํผ์ค๋ฅผ ๋ง๋ค์ด์ผ ํฉ๋๋ค. ๋ํ ์์ธ ์์ฑ์ ์ํด Live API์์ ์ ์ฅ๋ ์ธ์
์ปจํ
์คํธ๋ฅผ ํ์ฑํ์ฌ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ๋น๋ํ๋ ๋ฐ ์ฌ์ฉ๋๋ ๋ฉ๋ชจ๋ฆฌ ์ฝํผ์ค ๊ตฌ์ฑ์์ LLM ํ์๋ฅผ ์ง์ ํด์ผ ํฉ๋๋ค.
์ด ์ฝ๋ ์ํ์ ์ฝํผ์ค๋ฅผ ๋ง๋๋ ๋ฐฉ๋ฒ์ ๋ณด์ฌ์ค๋๋ค. ํ์ง๋ง ๋จผ์ ๋ณ์๋ฅผ ๊ฐ์ผ๋ก ๋ฐ๊ฟ์ผ ํฉ๋๋ค.
# Currently supports Google first-party embedding models
EMBEDDING_MODEL = YOUR_EMBEDDING_MODEL # Such as "publishers/google/models/text-embedding-005"
MEMORY_CORPUS_DISPLAY_NAME = YOUR_MEMORY_CORPUS_DISPLAY_NAME
LLM_PARSER_MODEL_NAME = YOUR_LLM_PARSER_MODEL_NAME # Such as "projects/{project_id}/locations/{location}/publishers/google/models/gemini-2.5-pro-preview-05-06"
memory_corpus = rag.create_corpus(
display_name=MEMORY_CORPUS_DISPLAY_NAME,
corpus_type_config=rag.RagCorpusTypeConfig(
corpus_type_config=rag.MemoryCorpus(
llm_parser=rag.LlmParserConfig(
model_name=LLM_PARSER_MODEL_NAME,
)
)
),
backend_config=rag.RagVectorDbConfig(
rag_embedding_model_config=rag.RagEmbeddingModelConfig(
vertex_prediction_endpoint=rag.VertexPredictionEndpoint(
publisher_model=EMBEDDING_MODEL
)
)
),
)
์ปจํ ์คํธ๋ฅผ ์ ์ฅํ ๋ฉ๋ชจ๋ฆฌ ์ฝํผ์ค ์ง์
Live API์์ ๋ฉ๋ชจ๋ฆฌ ์ฝํผ์ค๋ฅผ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ ๋ฉ๋ชจ๋ฆฌ ์ฝํผ์ค๋ฅผ ๊ฒ์ ๋๊ตฌ๋ก ์ง์ ํ ๋ค์ store_context
๋ฅผ true
๋ก ์ค์ ํ์ฌ Live API๊ฐ ์ธ์
์ปจํ
์คํธ๋ฅผ ์ ์ฅํ๋๋ก ํ์ฉํด์ผ ํฉ๋๋ค.
์ด ์ฝ๋ ์ํ์ ์ปจํ ์คํธ๋ฅผ ์ ์ฅํ ๋ฉ๋ชจ๋ฆฌ ์ฝํผ์ค๋ฅผ ์ง์ ํ๋ ๋ฐฉ๋ฒ์ ๋ณด์ฌ์ค๋๋ค. ๋จ, ๋จผ์ ๋ณ์๋ฅผ ๊ฐ์ผ๋ก ๋ฐ๊ฟ์ผ ํฉ๋๋ค.
from google import genai
from google.genai import types
from google.genai.types import (Content, LiveConnectConfig, HttpOptions, Modality, Part)
from IPython import display
PROJECT_ID=YOUR_PROJECT_ID
LOCATION=YOUR_LOCATION
TEXT_INPUT=YOUR_TEXT_INPUT
MODEL_NAME=YOUR_MODEL_NAME # Such as "gemini-2.0-flash-exp"
client = genai.Client(
vertexai=True,
project=PROJECT_ID,
location=LOCATION,
)
memory_store=types.VertexRagStore(
rag_resources=[
types.VertexRagStoreRagResource(
rag_corpus=memory_corpus.name
)
],
store_context=True
)
async with client.aio.live.connect(
model=MODEL_NAME,
config=LiveConnectConfig(response_modalities=[Modality.TEXT],
tools=[types.Tool(
retrieval=types.Retrieval(
vertex_rag_store=memory_store))]),
) as session:
text_input=TEXT_INPUT
await session.send_client_content(
turns=Content(role="user", parts=[Part(text=text_input)])
)
async for message in session.receive():
if message.text:
display.display(display.Markdown(message.text))
continue
๋ค์ ๋จ๊ณ
- Vertex AI RAG Engine์ ์์ธํ ์์๋ณด๋ ค๋ฉด Vertex AI RAG Engine ๊ฐ์๋ฅผ ์ฐธ์กฐํ์ธ์.
- RAG API์ ๋ํด ์์ธํ ์์๋ณด๋ ค๋ฉด Vertex AI RAG Engine API๋ฅผ ์ฐธ๊ณ ํ์ธ์.
- RAG ์ฝํผ์ค๋ฅผ ๊ด๋ฆฌํ๋ ค๋ฉด ์ฝํผ์ค ๊ด๋ฆฌ๋ฅผ ์ฐธ๊ณ ํ์ธ์.
- RAG ํ์ผ์ ๊ด๋ฆฌํ๋ ค๋ฉด ํ์ผ ๊ด๋ฆฌ๋ฅผ ์ฐธ๊ณ ํ์ธ์.
- Vertex AI SDK๋ฅผ ์ฌ์ฉํ์ฌ Vertex AI RAG Engine ํ์คํฌ๋ฅผ ์คํํ๋ ๋ฐฉ๋ฒ์ Python์ฉ RAG ๋น ๋ฅธ ์์์ ์ฐธ์กฐํ์ธ์.